mirror of https://codeberg.org/tom79/Fedilab
parent
16962a127e
commit
159ddaea80
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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>
|
@ -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>
|
@ -1,4 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in new issue