summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/TagsEditAdapter.java
blob: 5bbf2d39e609ca51201e056033f84fae19c12f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package app.fedilab.android.mastodon.ui.drawer;
/* Copyright 2023 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import app.fedilab.android.R;
import app.fedilab.android.databinding.DrawerTagEditBinding;
import app.fedilab.android.mastodon.client.entities.app.CamelTag;
import app.fedilab.android.mastodon.exception.DBException;
import es.dmoral.toasty.Toasty;

public class TagsEditAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<String> tags;
    private final TagsEditAdapter tagsEditAdapter;
    private Context context;

    public TagsEditAdapter(List<String> tags) {
        this.tags = tags;
        tagsEditAdapter = this;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        context = parent.getContext();
        DrawerTagEditBinding itemBinding = DrawerTagEditBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new TagCaheViewHolder(itemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        final String[] tag = {tags.get(viewHolder.getLayoutPosition())};
        TagCaheViewHolder holder = (TagCaheViewHolder) viewHolder;
        holder.binding.tagName.setText(String.format("#%s", tag[0]));
        holder.binding.saveTag.setOnClickListener(v -> {
            if (holder.binding.tagName.getText() != null && holder.binding.tagName.getText().toString().trim().replaceAll("#", "").length() > 0) {
                String tagToInsert = holder.binding.tagName.getText().toString().trim().replaceAll("#", "");
                try {
                    boolean isPresent = new CamelTag(context).tagExists(tagToInsert);
                    if (isPresent)
                        Toasty.warning(context, context.getString(R.string.tags_already_stored), Toast.LENGTH_LONG).show();
                    else {
                        new CamelTag(context).update(tag[0], tagToInsert);
                        Toasty.success(context, context.getString(R.string.tags_renamed), Toast.LENGTH_LONG).show();
                    }
                } catch (DBException e) {
                    throw new RuntimeException(e);
                }

            }
        });

        holder.binding.deleteTag.setOnClickListener(v -> {
            holder.binding.tagName.clearFocus();
            new CamelTag(context).removeTag(tag[0]);
            tags.remove(tag[0]);
            tagsEditAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
            Toasty.success(context, context.getString(R.string.tags_deleted), Toast.LENGTH_LONG).show();
        });

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return tags.size();
    }


    static class TagCaheViewHolder extends RecyclerView.ViewHolder {
        DrawerTagEditBinding binding;

        public TagCaheViewHolder(@NonNull DrawerTagEditBinding drawerTagEditBinding) {
            super(drawerTagEditBinding.getRoot());
            binding = drawerTagEditBinding;
        }
    }
}