Commit 159ddaea authored by Thomas's avatar Thomas
Browse files

manage tags

parent 16962a12
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -399,6 +399,9 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
            } else {
                Toasty.info(ComposeActivity.this, getString(R.string.toot_error_no_content), Toasty.LENGTH_SHORT).show();
            }
        } else if (item.getItemId() == R.id.action_tags) {
            TagCacheActivity tagCacheActivity = new TagCacheActivity();
            tagCacheActivity.show(getSupportFragmentManager(), null);
        }
        return true;
    }
+98 −0
Original line number Diff line number Diff line
package app.fedilab.android.mastodon.activities;
/* 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.app.Dialog;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.util.ArrayList;
import java.util.List;

import app.fedilab.android.R;
import app.fedilab.android.databinding.ActivityCamelTagBinding;
import app.fedilab.android.mastodon.client.entities.app.CamelTag;
import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.ui.drawer.TagsEditAdapter;
import es.dmoral.toasty.Toasty;

public class TagCacheActivity extends DialogFragment {

    private List<String> tags;
    private TagsEditAdapter tagsEditAdapter;

    private ActivityCamelTagBinding binding;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        binding = ActivityCamelTagBinding.inflate(getLayoutInflater());

        MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(requireContext());
        materialAlertDialogBuilder.setView(binding.getRoot());

        Dialog dialog = materialAlertDialogBuilder.create();
        tags = new ArrayList<>();

        binding.saveTag.setOnClickListener(v -> {
            if (binding.tagAdd.getText() != null && (binding.tagAdd.getText().toString().trim().replaceAll("#", "").length() > 0)) {
                String tagToInsert = binding.tagAdd.getText().toString().trim().replaceAll("#", "");
                try {
                    boolean isPresent = new CamelTag(requireActivity()).tagExists(tagToInsert);
                    if (isPresent)
                        Toasty.warning(requireActivity(), getString(R.string.tags_already_stored), Toast.LENGTH_LONG).show();
                    else {
                        new CamelTag(requireActivity()).insert(tagToInsert);
                        int position = tags.size();
                        tags.add(tagToInsert);
                        Toasty.success(requireActivity(), getString(R.string.tags_stored), Toast.LENGTH_LONG).show();
                        binding.tagAdd.setText("");
                        tagsEditAdapter.notifyItemInserted(position);
                    }
                } catch (DBException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        dialog.setTitle(R.string.manage_tags);

        new Thread(() -> {
            List<String> tagsTemp = new CamelTag(requireActivity()).getAll();
            requireActivity().runOnUiThread(() -> {
                if (tagsTemp != null)
                    tags = tagsTemp;
                if (tags != null) {
                    tagsEditAdapter = new TagsEditAdapter(tags);
                    binding.tagList.setAdapter(tagsEditAdapter);
                    LinearLayoutManager mLayoutManager = new LinearLayoutManager(requireActivity());
                    binding.tagList.setLayoutManager(mLayoutManager);
                }
            });
        }).start();

        binding.close.setOnClickListener(v -> requireDialog().dismiss());
        return dialog;
    }


}
+11 −0
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ package app.fedilab.android.mastodon.client.entities.api;
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import androidx.annotation.Nullable;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
@@ -42,4 +44,13 @@ public class Tag implements Serializable {
        }
        return weight;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        boolean same = false;
        if (obj instanceof Tag) {
            same = this.name.equals(((Tag) obj).name);
        }
        return same;
    }
}
+15 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ public class CamelTag {
        return cursorToTag(c);
    }

    private boolean tagExists(String name) throws DBException {
    public boolean tagExists(String name) throws DBException {
        Cursor c = db.query(Sqlite.TABLE_CACHE_TAGS, null, Sqlite.COL_TAG + " = \"" + name + "\"", null, null, null, null, null);
        boolean isPresent = (c != null && c.getCount() > 0);
        assert c != null;
@@ -98,6 +98,20 @@ public class CamelTag {
        }
    }

    /**
     * Returns all tags in db
     *
     * @return string tags List<String>
     */
    public List<String> getAll() {
        try {
            Cursor c = db.query(Sqlite.TABLE_CACHE_TAGS, null, null, null, null, null, Sqlite.COL_TAG + " ASC", null);
            return cursorToTag(c);
        } catch (Exception e) {
            return null;
        }
    }

    private List<String> cursorToTag(Cursor c) {
        //No element found
        if (c.getCount() == 0) {
+1 −0
Original line number Diff line number Diff line
@@ -234,6 +234,7 @@ public class ComposeWorker extends Worker {
                if (statuses.get(i).local_only) {
                    statuses.get(i).text += " \uD83D\uDC41";
                }
                //Record tags
                if (statuses.get(i).text != null && statuses.get(i).text.length() > 0) {
                    Matcher matcher = Helper.hashtagPattern.matcher(statuses.get(i).text);
                    while (matcher.find()) {
Loading