From d47797bf7ac8cff3f9ba1cfee219a1bb2af21da6 Mon Sep 17 00:00:00 2001 From: sk Date: Tue, 10 Jan 2023 13:56:37 +0100 Subject: [PATCH] implement editing lists re: sk22#30 --- .../fragments/ListTimelineFragment.java | 46 +++++++++++++++++-- .../fragments/ListTimelinesFragment.java | 23 ++++++---- .../android/ui/views/ListTimelineEditor.java | 11 ++--- .../main/res/layout/list_timeline_editor.xml | 10 ++-- mastodon/src/main/res/values/strings_sk.xml | 7 +-- 5 files changed, 69 insertions(+), 28 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelineFragment.java index 83a16f3007..5423d65ea5 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelineFragment.java @@ -10,13 +10,20 @@ import android.widget.ImageButton; import org.joinmastodon.android.R; +import org.joinmastodon.android.api.requests.lists.CreateList; +import org.joinmastodon.android.api.requests.lists.UpdateList; import org.joinmastodon.android.api.requests.timelines.GetListTimeline; +import org.joinmastodon.android.model.ListTimeline; import org.joinmastodon.android.model.Status; +import org.joinmastodon.android.ui.M3AlertDialogBuilder; import org.joinmastodon.android.ui.utils.UiUtils; +import org.joinmastodon.android.ui.views.ListTimelineEditor; import java.util.List; import me.grishka.appkit.Nav; +import me.grishka.appkit.api.Callback; +import me.grishka.appkit.api.ErrorResponse; import me.grishka.appkit.api.SimpleCallback; import me.grishka.appkit.utils.V; @@ -24,6 +31,7 @@ public class ListTimelineFragment extends StatusListFragment { private String listID; private String listTitle; + private ListTimeline.RepliesPolicy repliesPolicy; private ImageButton fab; public ListTimelineFragment() { @@ -33,8 +41,11 @@ public ListTimelineFragment() { @Override public void onAttach(Activity activity){ super.onAttach(activity); - listID=getArguments().getString("listID"); - listTitle=getArguments().getString("listTitle"); + Bundle args = getArguments(); + listID = args.getString("listID"); + listTitle = args.getString("listTitle"); + repliesPolicy = ListTimeline.RepliesPolicy.values()[args.getInt("repliesPolicy", 0)]; + setTitle(listTitle); setHasOptionsMenu(true); } @@ -47,12 +58,37 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @Override public boolean onOptionsItemSelected(MenuItem item) { + Bundle args = new Bundle(); + args.putString("listID", listID); if (item.getItemId() == R.id.edit) { - + ListTimelineEditor editor = new ListTimelineEditor(getContext()); + editor.applyList(listTitle, repliesPolicy); + new M3AlertDialogBuilder(getActivity()) + .setTitle(R.string.sk_edit_list_title) + .setView(editor) + .setPositiveButton(R.string.save, (d, which) -> { + new UpdateList(listID, editor.getTitle(), editor.getRepliesPolicy()).setCallback(new Callback<>() { + @Override + public void onSuccess(ListTimeline list) { + setTitle(list.title); + listTitle = list.title; + repliesPolicy = list.repliesPolicy; + args.putString("listTitle", listTitle); + args.putInt("repliesPolicy", repliesPolicy.ordinal()); + setResult(true, args); + } + + @Override + public void onError(ErrorResponse error) { + error.showToast(getContext()); + } + }).exec(accountID); + }) + .setNegativeButton(R.string.cancel, (d, which) -> {}) + .show(); } else if (item.getItemId() == R.id.delete) { UiUtils.confirmDeleteList(getActivity(), accountID, listID, () -> { - Bundle args = new Bundle(); - args.putString("listID", listID); + args.putBoolean("deleted", true); setResult(true, args); Nav.finish(this); }); diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelinesFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelinesFragment.java index 8c9a184704..7942161485 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelinesFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ListTimelinesFragment.java @@ -36,7 +36,7 @@ import me.grishka.appkit.views.UsableRecyclerView; public class ListTimelinesFragment extends BaseRecyclerFragment implements ScrollableToTop { - private static final int LIST_DELETED_RESULT = 987; + private static final int LIST_CHANGED_RESULT = 987; private String accountId; private String profileAccountId; @@ -147,14 +147,20 @@ public void onSuccess(List allLists) { } @Override - public void onFragmentResult(int reqCode, boolean listDeleted, Bundle result){ - if (reqCode == LIST_DELETED_RESULT && listDeleted) { + public void onFragmentResult(int reqCode, boolean listChanged, Bundle result){ + if (reqCode == LIST_CHANGED_RESULT && listChanged) { String listID = result.getString("listID"); - for (int i = 0; i < data.size(); i++) { - if (data.get(i).id.equals(listID)) { - data.remove(i); - adapter.notifyItemRemoved(i); + ListTimeline item = data.get(i); + if (item.id.equals(listID)) { + if (result.getBoolean("deleted")) { + data.remove(i); + adapter.notifyItemRemoved(i); + } else { + item.title = result.getString("listTitle", item.title); + item.repliesPolicy = ListTimeline.RepliesPolicy.values()[result.getInt("repliesPolicy")]; + adapter.notifyItemChanged(i); + } break; } } @@ -223,7 +229,8 @@ public void onClick() { args.putString("account", accountId); args.putString("listID", item.id); args.putString("listTitle", item.title); - Nav.goForResult(getActivity(), ListTimelineFragment.class, args, LIST_DELETED_RESULT, ListTimelinesFragment.this); + args.putInt("repliesPolicy", item.repliesPolicy.ordinal()); + Nav.goForResult(getActivity(), ListTimelineFragment.class, args, LIST_CHANGED_RESULT, ListTimelinesFragment.this); } } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/views/ListTimelineEditor.java b/mastodon/src/main/java/org/joinmastodon/android/ui/views/ListTimelineEditor.java index 3d4f289b7c..d1f2fbf06c 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/views/ListTimelineEditor.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/views/ListTimelineEditor.java @@ -15,8 +15,8 @@ public class ListTimelineEditor extends LinearLayout { private ListTimeline.RepliesPolicy policy = null; - private TextInputFrameLayout input; - private Button button; + private final TextInputFrameLayout input; + private final Button button; @SuppressLint("ClickableViewAccessibility") public ListTimelineEditor(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { @@ -37,10 +37,9 @@ public ListTimelineEditor(Context context, AttributeSet attrs, int defStyleAttr, setRepliesPolicy(ListTimeline.RepliesPolicy.LIST); } - public void applyList(ListTimeline list) { - policy = list.repliesPolicy; - input.getEditText().setText(list.title); - setRepliesPolicy(list.repliesPolicy); + public void applyList(String title, ListTimeline.RepliesPolicy policy) { + input.getEditText().setText(title); + setRepliesPolicy(policy); } public String getTitle() { diff --git a/mastodon/src/main/res/layout/list_timeline_editor.xml b/mastodon/src/main/res/layout/list_timeline_editor.xml index 9f1415d9f5..2a1c6a3857 100644 --- a/mastodon/src/main/res/layout/list_timeline_editor.xml +++ b/mastodon/src/main/res/layout/list_timeline_editor.xml @@ -4,7 +4,6 @@ android:layout_width="match_parent" android:layout_height="match_parent"> @@ -25,11 +25,9 @@ android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="32dp" - android:layout_weight="0" - android:maxWidth="140dp" android:background="@drawable/bg_inline_button" android:elevation="0dp" - android:ellipsize="middle" + android:ellipsize="none" android:fontFamily="sans-serif-medium" android:singleLine="true" android:stateListAnimator="@null" diff --git a/mastodon/src/main/res/values/strings_sk.xml b/mastodon/src/main/res/values/strings_sk.xml index efca7e814e..ff7d7072a1 100644 --- a/mastodon/src/main/res/values/strings_sk.xml +++ b/mastodon/src/main/res/values/strings_sk.xml @@ -133,9 +133,10 @@ Create list List name Show replies to - List members - Any followed user - No one + list members + followed users + no one Delete list Are you sure you want to delete this list? + Edit list \ No newline at end of file