]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java
make transaction header row always have Id of 0
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / templates / TemplateViewHolder.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.templates;
19
20 import android.view.View;
21
22 import androidx.annotation.NonNull;
23 import androidx.appcompat.app.AlertDialog;
24 import androidx.recyclerview.widget.RecyclerView;
25
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;
30
31 abstract class BaseTemplateViewHolder extends RecyclerView.ViewHolder {
32     public BaseTemplateViewHolder(@NonNull View itemView) {
33         super(itemView);
34     }
35     abstract void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem item);
36     static class TemplateDividerViewHolder extends BaseTemplateViewHolder {
37         public TemplateDividerViewHolder(@NonNull TemplatesFallbackDividerBinding binding) {
38             super(binding.getRoot());
39         }
40         @Override
41         void bindToItem(TemplatesRecyclerViewAdapter.BaseTemplateItem item) {
42             // nothing
43         }
44     }
45
46     static class TemplateViewHolder extends BaseTemplateViewHolder {
47         final TemplateListTemplateItemBinding b;
48         public TemplateViewHolder(@NonNull TemplateListTemplateItemBinding binding) {
49             super(binding.getRoot());
50             b = binding;
51         }
52         @Override
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());
66                     }
67                     else if (which == 1) { // duplicate
68                         activity.onDuplicateTemplate(item.getId());
69                     }
70                     else if (which == 2) { // delete
71                         activity.onDeleteTemplate(item.getId());
72                     }
73                     else {
74                         throw new RuntimeException(
75                                 String.format("Unknown menu item id (%d)", which));
76                     }
77                     dialog.dismiss();
78                 });
79                 builder.show();
80                 return true;
81             });
82         }
83     }
84 }