manage tags

This commit is contained in:
Thomas 2023-02-09 15:02:31 +01:00
parent 16962a127e
commit 159ddaea80
13 changed files with 352 additions and 3 deletions

View file

@ -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;
}

View file

@ -0,0 +1,98 @@
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;
}
}

View file

@ -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;
}
}

View file

@ -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) {

View file

@ -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()) {

View file

@ -119,6 +119,7 @@ import app.fedilab.android.mastodon.client.entities.api.Poll;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.api.Tag;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.client.entities.app.CamelTag;
import app.fedilab.android.mastodon.client.entities.app.Languages;
import app.fedilab.android.mastodon.client.entities.app.Quotes;
import app.fedilab.android.mastodon.client.entities.app.StatusDraft;
@ -818,13 +819,24 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
} else if (matcherTag.matches()) {
String searchGroup = matcherTag.group(3);
if (searchGroup != null) {
List<String> camelTags = new CamelTag(context).getBy(searchGroup);
searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, searchGroup, null,
"hashtags", false, true, false, 0,
null, null, 10).observe((LifecycleOwner) context,
results -> {
if (results == null) {
if (results == null || results.hashtags == null || results.hashtags.size() == 0) {
return;
}
if (camelTags != null && camelTags.size() > 0) {
for (String camelTag : camelTags) {
Tag tag = new Tag();
tag.name = camelTag;
if (!results.hashtags.contains(tag)) {
results.hashtags.add(0, tag);
}
}
}
int currentCursorPosition = holder.binding.content.getSelectionStart();
TagsSearchAdapter tagsSearchAdapter = new TagsSearchAdapter(context, results.hashtags);
holder.binding.content.setThreshold(1);

View file

@ -0,0 +1,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;
}
}
}

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/fab_margin"
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/account_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/tag_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:inputType="text"
android:padding="20dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/save_tag"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="@string/set_save_changes" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tag_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/close"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="24dp"
android:layout_marginBottom="12dp"
android:text="@string/close" />
</androidx.appcompat.widget.LinearLayoutCompat>

View file

@ -77,7 +77,7 @@
android:focusable="true"
android:gravity="top|start"
android:inputType="textMultiLine|textCapSentences"
android:minLines="8"
android:minLines="10"
app:layout_constraintEnd_toStartOf="@id/button_emoji"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/content_spoiler" />

View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/account_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/tag_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:inputType="text"
android:padding="20dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/save_tag"
style="@style/Widget.Material3.Button.OutlinedButton.Icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:contentDescription="@string/set_save_changes"
android:gravity="center"
app:icon="@drawable/ic_baseline_save_24" />
<com.google.android.material.button.MaterialButton
android:id="@+id/delete_tag"
style="@style/Widget.Material3.Button.OutlinedButton.Icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:contentDescription="@string/set_save_changes"
android:gravity="center"
app:icon="@drawable/ic_baseline_delete_24" />
</androidx.appcompat.widget.LinearLayoutCompat>

View file

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>

View file

@ -21,4 +21,10 @@
android:icon="@drawable/ic_baseline_schedule_send_24"
android:title="@string/schedule"
app:showAsAction="never" />
<item
android:id="@+id/action_tags"
android:icon="@drawable/ic_tl_tag"
android:title="@string/manage_tags"
app:showAsAction="never" />
</menu>

View file

@ -2256,4 +2256,9 @@
<string name="number_of_media">Number of media</string>
<string name="number_of_replies">Number of replies</string>
<string name="update_date">Update date</string>
<string name="tags_renamed">The tag has been changed!</string>
<string name="tags_deleted">The tag has been removed!</string>
<string name="tags_stored">he tag has been stored!</string>
<string name="manage_tags">Manage tags</string>
</resources>