2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.templates;
20 import android.view.View;
22 import androidx.annotation.NonNull;
23 import androidx.appcompat.app.AlertDialog;
24 import androidx.recyclerview.widget.RecyclerView;
26 import net.ktnx.mobileledger.R;
27 import net.ktnx.mobileledger.databinding.TemplateListTemplateItemBinding;
28 import net.ktnx.mobileledger.databinding.TemplatesFallbackDividerBinding;
29 import net.ktnx.mobileledger.db.TemplateHeader;
31 abstract class BaseTemplateViewHolder extends RecyclerView.ViewHolder {
32 public BaseTemplateViewHolder(@NonNull View itemView) {
35 abstract void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem item);
36 static class TemplateDividerViewHolder extends BaseTemplateViewHolder {
37 public TemplateDividerViewHolder(@NonNull TemplatesFallbackDividerBinding binding) {
38 super(binding.getRoot());
41 void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem item) {
46 static class TemplateViewHolder extends BaseTemplateViewHolder {
47 final TemplateListTemplateItemBinding b;
48 public TemplateViewHolder(@NonNull TemplateListTemplateItemBinding binding) {
49 super(binding.getRoot());
53 public void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem baseItem) {
54 TemplateHeader item = ((TemplatesRecyclerViewAdapter.TemplateItem) baseItem).template;
55 b.templateName.setText(item.getName());
56 b.templateName.setOnClickListener(
57 v -> ((TemplatesActivity) v.getContext()).onEditTemplate(item.getId()));
58 b.templateName.setOnLongClickListener((v) -> {
59 TemplatesActivity activity = (TemplatesActivity) v.getContext();
60 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
61 final String templateName = item.getName();
62 builder.setTitle(templateName);
63 builder.setItems(R.array.templates_ctx_menu, (dialog, which) -> {
64 if (which == 0) { // edit
65 activity.onEditTemplate(item.getId());
67 else if (which == 1) { // duplicate
68 activity.onDuplicateTemplate(item.getId());
70 else if (which == 2) { // delete
71 activity.onDeleteTemplate(item.getId());
74 throw new RuntimeException(
75 String.format("Unknown menu item id (%d)", which));