From 82367c771e5fb81e52218d291b8943b8eb0cdb74 Mon Sep 17 00:00:00 2001 From: oakkitten Date: Fri, 15 Mar 2024 19:26:45 +0000 Subject: [PATCH] Add an option to not put hot buffers on top MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes the boolean preference “Sort buffer list” to a list preference, with an option to only sort buffers by number. This does NOT migrate the old setting! Also, this changes `sortByHotAndMessageCountComparator` to also sort by number. Not sorting by number could result in buffers appearing out of order if they were moved. Not a big issue. Closes #541 --- .../adapters/BufferListAdapter.kt | 27 ++++++++++++++----- .../ubergeek42/WeechatAndroid/service/P.java | 6 ++--- .../WeechatAndroid/utils/Constants.java | 8 ++++-- app/src/main/res/values/strings.xml | 9 ++++--- app/src/main/res/values/values.xml | 6 +++++ app/src/main/res/xml/preferences.xml | 12 +++++---- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/ubergeek42/WeechatAndroid/adapters/BufferListAdapter.kt b/app/src/main/java/com/ubergeek42/WeechatAndroid/adapters/BufferListAdapter.kt index 28fb93d6..8750d7a0 100644 --- a/app/src/main/java/com/ubergeek42/WeechatAndroid/adapters/BufferListAdapter.kt +++ b/app/src/main/java/com/ubergeek42/WeechatAndroid/adapters/BufferListAdapter.kt @@ -32,6 +32,9 @@ import com.ubergeek42.WeechatAndroid.relay.BufferList import com.ubergeek42.WeechatAndroid.relay.BufferSpec import com.ubergeek42.WeechatAndroid.service.P import com.ubergeek42.WeechatAndroid.upload.main +import com.ubergeek42.WeechatAndroid.utils.Constants.PREF_SORT_BUFFER_LIST_BY_HOT_MESSAGES_THEN_BY_NUMBER +import com.ubergeek42.WeechatAndroid.utils.Constants.PREF_SORT_BUFFER_LIST_BY_HOT_MESSAGES_THEN_BY_OTHER_MESSAGES_THEN_BY_NUMBER +import com.ubergeek42.WeechatAndroid.utils.Constants.PREF_SORT_BUFFER_LIST_BY_NUMBER import com.ubergeek42.WeechatAndroid.utils.Utils import com.ubergeek42.cats.Kitty import com.ubergeek42.cats.Root @@ -135,12 +138,15 @@ class BufferListAdapter( newBuffers.add(VisualBuffer.fromBuffer(buffer)) } - if (P.sortBuffers) { - Collections.sort(newBuffers, sortByHotAndMessageCountComparator) - } else { - Collections.sort(newBuffers, sortByHotCountAndNumberComparator) + val bufferComparator = when (P.sortBufferList) { + PREF_SORT_BUFFER_LIST_BY_NUMBER -> sortByNumberComparator + PREF_SORT_BUFFER_LIST_BY_HOT_MESSAGES_THEN_BY_NUMBER -> sortByHotMessagesThenByNumberComparator + PREF_SORT_BUFFER_LIST_BY_HOT_MESSAGES_THEN_BY_OTHER_MESSAGES_THEN_BY_NUMBER -> sortByHotMessagesThenByOtherMessagesThenByNumberComparator + else -> sortByNumberComparator } + Collections.sort(newBuffers, bufferComparator) + val diffResult = DiffUtil.calculateDiff(DiffCallback(buffers, newBuffers), false) main { @@ -222,7 +228,11 @@ class BufferListAdapter( } -private val sortByHotCountAndNumberComparator = Comparator { left, right -> +private val sortByNumberComparator = Comparator { left, right -> + left.number - right.number +} + +private val sortByHotMessagesThenByNumberComparator = Comparator { left, right -> val highlightDiff = right.highlights - left.highlights if (highlightDiff != 0) return@Comparator highlightDiff @@ -235,7 +245,7 @@ private val sortByHotCountAndNumberComparator = Comparator { left, } -private val sortByHotAndMessageCountComparator = Comparator { left, right -> +private val sortByHotMessagesThenByOtherMessagesThenByNumberComparator = Comparator { left, right -> val highlightDiff = right.highlights - left.highlights if (highlightDiff != 0) return@Comparator highlightDiff @@ -244,5 +254,8 @@ private val sortByHotAndMessageCountComparator = Comparator { left val pmDiff = pmRight - pmLeft if (pmDiff != 0) return@Comparator pmDiff - right.unreads - left.unreads + val unreadsDiff = right.unreads - left.unreads + if (unreadsDiff != 0) return@Comparator unreadsDiff + + left.number - right.number } \ No newline at end of file diff --git a/app/src/main/java/com/ubergeek42/WeechatAndroid/service/P.java b/app/src/main/java/com/ubergeek42/WeechatAndroid/service/P.java index 5384156f..81f66793 100644 --- a/app/src/main/java/com/ubergeek42/WeechatAndroid/service/P.java +++ b/app/src/main/java/com/ubergeek42/WeechatAndroid/service/P.java @@ -105,7 +105,7 @@ public static void storeThemeOrColorSchemeColors(Context context) { final public static float _4dp = 4 * _1dp; final public static float _200dp = 200 * _1dp; - public static boolean sortBuffers; + public static String sortBufferList; public static boolean filterBuffers; public static boolean hideHiddenBuffers; public static boolean optimizeTraffic; @@ -158,7 +158,7 @@ static VolumeRole fromString(String value) { @MainThread private static void loadUIPreferences() { // buffer list preferences - sortBuffers = p.getBoolean(PREF_SORT_BUFFERS, PREF_SORT_BUFFERS_D); + sortBufferList = p.getString(PREF_SORT_BUFFER_LIST, PREF_SORT_BUFFER_LIST_D); filterBuffers = p.getBoolean(PREF_FILTER_NONHUMAN_BUFFERS, PREF_FILTER_NONHUMAN_BUFFERS_D); hideHiddenBuffers = p.getBoolean(PREF_HIDE_HIDDEN_BUFFERS, PREF_HIDE_HIDDEN_BUFFERS_D); optimizeTraffic = p.getBoolean(PREF_OPTIMIZE_TRAFFIC, PREF_OPTIMIZE_TRAFFIC_D); // okay this is out of sync with onChanged stuff—used for the bell icon @@ -329,7 +329,7 @@ public static void loadServerKeyVerifier() { @MainThread @Override @CatD public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { switch (key) { // buffer list preferences - case PREF_SORT_BUFFERS: sortBuffers = p.getBoolean(key, PREF_SORT_BUFFERS_D); break; + case PREF_SORT_BUFFER_LIST: sortBufferList = p.getString(key, PREF_SORT_BUFFER_LIST_D); break; case PREF_FILTER_NONHUMAN_BUFFERS: filterBuffers = p.getBoolean(key, PREF_FILTER_NONHUMAN_BUFFERS_D); break; case PREF_HIDE_HIDDEN_BUFFERS: hideHiddenBuffers = p.getBoolean(key, PREF_HIDE_HIDDEN_BUFFERS_D); break; case PREF_AUTO_HIDE_ACTIONBAR: autoHideActionbar = p.getBoolean(key, PREF_AUTO_HIDE_ACTIONBAR_D); break; diff --git a/app/src/main/java/com/ubergeek42/WeechatAndroid/utils/Constants.java b/app/src/main/java/com/ubergeek42/WeechatAndroid/utils/Constants.java index b8145a25..a8d844a9 100644 --- a/app/src/main/java/com/ubergeek42/WeechatAndroid/utils/Constants.java +++ b/app/src/main/java/com/ubergeek42/WeechatAndroid/utils/Constants.java @@ -83,8 +83,12 @@ public class Constants { // buffer list public static final String PREF_BUFFERLIST_GROUP = "bufferlist_group"; - public static final String PREF_SORT_BUFFERS = "sort_buffers"; - final public static boolean PREF_SORT_BUFFERS_D = false; + public static final String PREF_SORT_BUFFER_LIST = "sort_buffer_list"; + public static final String PREF_SORT_BUFFER_LIST_BY_NUMBER = "by_number"; + public static final String PREF_SORT_BUFFER_LIST_BY_HOT_MESSAGES_THEN_BY_NUMBER = "by_hot_messages_then_by_number"; + public static final String PREF_SORT_BUFFER_LIST_BY_HOT_MESSAGES_THEN_BY_OTHER_MESSAGES_THEN_BY_NUMBER = "by_hot_messages_then_by_other_messages_then_by_number"; + final public static String PREF_SORT_BUFFER_LIST_D = PREF_SORT_BUFFER_LIST_BY_NUMBER; + public static final String PREF_HIDE_HIDDEN_BUFFERS = "hide_hidden_buffers"; final public static boolean PREF_HIDE_HIDDEN_BUFFERS_D = true; public static final String PREF_FILTER_NONHUMAN_BUFFERS = "filter_nonhuman_buffers"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 92da247b..bc24a0fc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -572,10 +572,13 @@ Buffer list - + Sort buffer list - - Sort by number of highlights/private messages/unread messages + + By number + By hot messages, then by number + By hot messages, then by other messages, then by number + Hide non-conversation buffers diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index 832b95ec..23016d78 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -3,6 +3,12 @@ + + by_number + by_hot_messages_then_by_number + by_hot_messages_then_by_other_messages_then_by_number + + left right diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index dd0da1c8..14c6748e 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -196,11 +196,13 @@ - +