From fb2d072ef47f806238968a9bc73fc7972d1c6bbc Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 2 Mar 2021 23:12:27 +0200 Subject: [PATCH] add divider before fallback templates --- .../ui/templates/TemplateViewHolder.java | 86 +++++++++------ .../TemplatesRecyclerViewAdapter.java | 103 +++++++++++++++--- .../layout/template_list_template_item.xml | 2 +- .../res/layout/templates_fallback_divider.xml | 52 +++++++++ app/src/main/res/values-bg/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 195 insertions(+), 50 deletions(-) create mode 100644 app/src/main/res/layout/templates_fallback_divider.xml diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java index c8c197f4..aeb1dc13 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java @@ -17,46 +17,68 @@ package net.ktnx.mobileledger.ui.templates; +import android.view.View; + import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.databinding.TemplateListTemplateItemBinding; +import net.ktnx.mobileledger.databinding.TemplatesFallbackDividerBinding; import net.ktnx.mobileledger.db.TemplateHeader; -class TemplateViewHolder extends RecyclerView.ViewHolder { - final TemplateListTemplateItemBinding b; - public TemplateViewHolder(@NonNull TemplateListTemplateItemBinding binding) { - super(binding.getRoot()); - b = binding; +abstract class BaseTemplateViewHolder extends RecyclerView.ViewHolder { + public BaseTemplateViewHolder(@NonNull View itemView) { + super(itemView); + } + abstract void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem item); + static class TemplateDividerViewHolder extends BaseTemplateViewHolder { + public TemplateDividerViewHolder(@NonNull TemplatesFallbackDividerBinding binding) { + super(binding.getRoot()); + } + @Override + void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem item) { + // nothing + } } - public void bindToItem(TemplateHeader item) { - b.templateName.setText(item.getName()); - b.templateName.setOnClickListener( - v -> ((TemplatesActivity) v.getContext()).onEditTemplate(item.getId())); - b.templateName.setOnLongClickListener((v) -> { - TemplatesActivity activity = (TemplatesActivity) v.getContext(); - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - final String templateName = item.getName(); - builder.setTitle(templateName); - builder.setItems(R.array.templates_ctx_menu, (dialog, which) -> { - if (which == 0) { // edit - activity.onEditTemplate(item.getId()); - } - else if (which == 1) { // duplicate - activity.onDuplicateTemplate(item.getId()); - } - else if (which == 2) { // delete - activity.onDeleteTemplate(item.getId()); - } - else { - throw new RuntimeException(String.format("Unknown menu item id (%d)", which)); - } - dialog.dismiss(); + + static class TemplateViewHolder extends BaseTemplateViewHolder { + final TemplateListTemplateItemBinding b; + public TemplateViewHolder(@NonNull TemplateListTemplateItemBinding binding) { + super(binding.getRoot()); + b = binding; + } + @Override + public void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem baseItem) { + TemplateHeader item = ((TemplatesRecyclerViewAdapter.TemplateItem) baseItem).template; + b.templateName.setText(item.getName()); + b.templateName.setOnClickListener( + v -> ((TemplatesActivity) v.getContext()).onEditTemplate(item.getId())); + b.templateName.setOnLongClickListener((v) -> { + TemplatesActivity activity = (TemplatesActivity) v.getContext(); + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + final String templateName = item.getName(); + builder.setTitle(templateName); + builder.setItems(R.array.templates_ctx_menu, (dialog, which) -> { + if (which == 0) { // edit + activity.onEditTemplate(item.getId()); + } + else if (which == 1) { // duplicate + activity.onDuplicateTemplate(item.getId()); + } + else if (which == 2) { // delete + activity.onDeleteTemplate(item.getId()); + } + else { + throw new RuntimeException( + String.format("Unknown menu item id (%d)", which)); + } + dialog.dismiss(); + }); + builder.show(); + return true; }); - builder.show(); - return true; - }); + } } -} +} \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesRecyclerViewAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesRecyclerViewAdapter.java index 6c1632ea..320adc57 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesRecyclerViewAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesRecyclerViewAdapter.java @@ -26,49 +26,118 @@ import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.databinding.TemplateListTemplateItemBinding; +import net.ktnx.mobileledger.databinding.TemplatesFallbackDividerBinding; import net.ktnx.mobileledger.db.TemplateHeader; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.List; -public class TemplatesRecyclerViewAdapter extends RecyclerView.Adapter { - private final AsyncListDiffer listDiffer; +public class TemplatesRecyclerViewAdapter extends RecyclerView.Adapter { + private static final int ITEM_TYPE_TEMPLATE = 1; + private static final int ITEM_TYPE_DIVIDER = 2; + private final AsyncListDiffer listDiffer; + public TemplatesRecyclerViewAdapter() { - listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback() { + listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback() { @Override - public boolean areItemsTheSame(@NotNull TemplateHeader oldItem, - @NotNull TemplateHeader newItem) { - return oldItem.getId() - .equals(newItem.getId()); + public boolean areItemsTheSame( + @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem oldItem, + @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem newItem) { + return oldItem.getId() == newItem.getId(); } @Override - public boolean areContentsTheSame(@NotNull TemplateHeader oldItem, - @NotNull TemplateHeader newItem) { + public boolean areContentsTheSame( + @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem oldItem, + @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem newItem) { return oldItem.equals(newItem); } }); } @NonNull @Override - public TemplateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - TemplateListTemplateItemBinding b = - TemplateListTemplateItemBinding.inflate(LayoutInflater.from(parent.getContext()), - parent, false); - - return new TemplateViewHolder(b); + public BaseTemplateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + final LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + switch (viewType) { + case ITEM_TYPE_TEMPLATE: + TemplateListTemplateItemBinding b = + TemplateListTemplateItemBinding.inflate(inflater, parent, false); + return new BaseTemplateViewHolder.TemplateViewHolder(b); + case ITEM_TYPE_DIVIDER: + return new BaseTemplateViewHolder.TemplateDividerViewHolder( + TemplatesFallbackDividerBinding.inflate(inflater, parent, false)); + default: + throw new RuntimeException("Can't handle " + viewType); + } } @Override - public void onBindViewHolder(@NonNull TemplateViewHolder holder, int position) { + public void onBindViewHolder(@NonNull BaseTemplateViewHolder holder, int position) { holder.bindToItem(listDiffer.getCurrentList() .get(position)); } @Override + public int getItemViewType(int position) { + BaseTemplateItem item = getItem(position); + if (item instanceof TemplateItem) + return ITEM_TYPE_TEMPLATE; + if (item instanceof TemplateDivider) + return ITEM_TYPE_DIVIDER; + + throw new RuntimeException("Can't handle " + item); + } + @Override public int getItemCount() { return listDiffer.getCurrentList() .size(); } public void setTemplates(List newList) { - listDiffer.submitList(newList); + List itemList = new ArrayList<>(); + + boolean reachedFallbackItems = false; + + for (TemplateHeader item : newList) { + if (!reachedFallbackItems && item.isFallback()) { + itemList.add(new TemplateDivider()); + reachedFallbackItems = true; + } + itemList.add(new TemplateItem(item)); + } + + listDiffer.submitList(itemList); + } + public BaseTemplateItem getItem(int position) { + return listDiffer.getCurrentList() + .get(position); + } + + static abstract class BaseTemplateItem { + abstract long getId(); + + abstract boolean equals(BaseTemplateItem other); + } + + static class TemplateItem extends BaseTemplateItem { + final TemplateHeader template; + TemplateItem(TemplateHeader template) {this.template = template;} + @Override + long getId() { + return template.getId(); + } + @Override + boolean equals(BaseTemplateItem other) { + return template.equals(((TemplateItem) other).template); + } + } + + static class TemplateDivider extends BaseTemplateItem { + @Override + long getId() { + return -1; + } + @Override + boolean equals(BaseTemplateItem other) { + return true; + } } } diff --git a/app/src/main/res/layout/template_list_template_item.xml b/app/src/main/res/layout/template_list_template_item.xml index fb5e2431..0c9c21bd 100644 --- a/app/src/main/res/layout/template_list_template_item.xml +++ b/app/src/main/res/layout/template_list_template_item.xml @@ -24,7 +24,7 @@ android:id="@+id/template_name" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_marginStart="@dimen/text_margin" + android:layout_marginHorizontal="@dimen/text_margin" android:gravity="center_vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" diff --git a/app/src/main/res/layout/templates_fallback_divider.xml b/app/src/main/res/layout/templates_fallback_divider.xml new file mode 100644 index 00000000..acff1962 --- /dev/null +++ b/app/src/main/res/layout/templates_fallback_divider.xml @@ -0,0 +1,52 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 03806e8a..1c8b51b3 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -225,4 +225,5 @@ Резервен макет Макетът ще се предлага за избор само ако няма друг макет, който да пасва и да не е маркиран като резервен Макетът не е резервен + Резервни макети diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5713de10..2c478181 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -235,4 +235,5 @@ Fallback template Template will be offered for selection only when no templates match that aren\'t marked as fallback templates Template is a primary, high priority one, not a catch-all + Fallback templates -- 2.39.5