From 23d2626370c521482bf888a661b07a02c7d00716 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 25 Nov 2022 10:08:57 +0100 Subject: [PATCH] Fix issue #529 - Lists are ordered alphabetically 'ASC' by default can be reverted with a button --- .../activities/MastodonListActivity.java | 38 +++++++++++++++++-- .../app/fedilab/android/helper/Helper.java | 2 +- .../drawable/ic_baseline_filter_asc_24.xml | 15 ++++++++ .../drawable/ic_baseline_filter_desc_24.xml | 10 +++++ app/src/main/res/menu/menu_main_list.xml | 6 +++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_filter_asc_24.xml create mode 100644 app/src/main/res/drawable/ic_baseline_filter_desc_24.xml diff --git a/app/src/main/java/app/fedilab/android/activities/MastodonListActivity.java b/app/src/main/java/app/fedilab/android/activities/MastodonListActivity.java index ba64cd173..4b9d35d76 100644 --- a/app/src/main/java/app/fedilab/android/activities/MastodonListActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/MastodonListActivity.java @@ -15,8 +15,6 @@ package app.fedilab.android.activities; * see . */ -import static app.fedilab.android.helper.PinnedTimelineHelper.sortListPositionAsc; - import android.content.Intent; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; @@ -39,6 +37,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import app.fedilab.android.BaseMainActivity; @@ -78,6 +77,7 @@ public class MastodonListActivity extends BaseActivity implements MastodonListAd private boolean flagLoading; private String max_id; private FragmentMastodonTimeline fragmentMastodonTimeline; + private boolean orderASC; @Override protected void onCreate(Bundle savedInstanceState) { @@ -92,6 +92,7 @@ public class MastodonListActivity extends BaseActivity implements MastodonListAd getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.cyanea_primary))); } flagLoading = false; + orderASC = true; max_id = null; accountsVM = new ViewModelProvider(MastodonListActivity.this).get(AccountsVM.class); timelinesVM = new ViewModelProvider(MastodonListActivity.this).get(TimelinesVM.class); @@ -114,7 +115,7 @@ public class MastodonListActivity extends BaseActivity implements MastodonListAd } } } - sortListPositionAsc(mastodonListList); + sortAsc(mastodonListList); mastodonListAdapter = new MastodonListAdapter(mastodonListList); mastodonListAdapter.actionOnList = this; binding.notContent.setVisibility(View.GONE); @@ -127,6 +128,19 @@ public class MastodonListActivity extends BaseActivity implements MastodonListAd }); } + + private void sortAsc(List mastodonLists) { + Collections.sort(mastodonLists, (obj1, obj2) -> obj1.title.compareToIgnoreCase(obj2.title)); + orderASC = true; + invalidateOptionsMenu(); + } + + private void sortDesc(List mastodonLists) { + Collections.sort(mastodonLists, (obj1, obj2) -> obj2.title.compareToIgnoreCase(obj1.title)); + orderASC = false; + invalidateOptionsMenu(); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { @@ -374,6 +388,16 @@ public class MastodonListActivity extends BaseActivity implements MastodonListAd }); dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); dialogBuilder.create().show(); + } else if (item.getItemId() == R.id.action_order) { + if (mastodonListList != null && mastodonListList.size() > 0 && mastodonListAdapter != null) { + if (orderASC) { + sortDesc(mastodonListList); + } else { + sortAsc(mastodonListList); + } + invalidateOptionsMenu(); + mastodonListAdapter.notifyItemRangeChanged(0, mastodonListList.size()); + } } return super.onOptionsItemSelected(item); } @@ -403,6 +427,14 @@ public class MastodonListActivity extends BaseActivity implements MastodonListAd public boolean onCreateOptionsMenu(@NonNull Menu menu) { if (!canGoBack) { getMenuInflater().inflate(R.menu.menu_main_list, menu); + MenuItem order = menu.findItem(R.id.action_order); + if (order != null) { + if (orderASC) { + order.setIcon(R.drawable.ic_baseline_filter_asc_24); + } else { + order.setIcon(R.drawable.ic_baseline_filter_desc_24); + } + } } else { getMenuInflater().inflate(R.menu.menu_list, menu); } diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index c644a779c..f533c3ab6 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -1031,7 +1031,7 @@ public class Helper { final Activity activity = (Activity) context; return !activity.isDestroyed() && !activity.isFinishing(); } - return false; + return true; } /** diff --git a/app/src/main/res/drawable/ic_baseline_filter_asc_24.xml b/app/src/main/res/drawable/ic_baseline_filter_asc_24.xml new file mode 100644 index 000000000..0d01f28ed --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_filter_asc_24.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_baseline_filter_desc_24.xml b/app/src/main/res/drawable/ic_baseline_filter_desc_24.xml new file mode 100644 index 000000000..b311f427f --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_filter_desc_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/menu/menu_main_list.xml b/app/src/main/res/menu/menu_main_list.xml index 01b82f787..a3e9b56af 100644 --- a/app/src/main/res/menu/menu_main_list.xml +++ b/app/src/main/res/menu/menu_main_list.xml @@ -1,6 +1,12 @@ + + New sign-up (moderators) New report (moderators) Open with another account + Order lists \ No newline at end of file