Skip to content

Commit

Permalink
Add an option to not put hot buffers on top
Browse files Browse the repository at this point in the history
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
  • Loading branch information
oakkitten committed Mar 22, 2024
1 parent 024fe26 commit 82367c7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -222,7 +228,11 @@ class BufferListAdapter(
}


private val sortByHotCountAndNumberComparator = Comparator<VisualBuffer> { left, right ->
private val sortByNumberComparator = Comparator<VisualBuffer> { left, right ->
left.number - right.number
}

private val sortByHotMessagesThenByNumberComparator = Comparator<VisualBuffer> { left, right ->
val highlightDiff = right.highlights - left.highlights
if (highlightDiff != 0) return@Comparator highlightDiff

Expand All @@ -235,7 +245,7 @@ private val sortByHotCountAndNumberComparator = Comparator<VisualBuffer> { left,
}


private val sortByHotAndMessageCountComparator = Comparator<VisualBuffer> { left, right ->
private val sortByHotMessagesThenByOtherMessagesThenByNumberComparator = Comparator<VisualBuffer> { left, right ->
val highlightDiff = right.highlights - left.highlights
if (highlightDiff != 0) return@Comparator highlightDiff

Expand All @@ -244,5 +254,8 @@ private val sortByHotAndMessageCountComparator = Comparator<VisualBuffer> { 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,13 @@

<string name="pref__buffer_list_group">Buffer list</string>

<string name="pref__buffer_list__sort_by_hot">
<string name="pref__buffer_list__sort_buffer_list">
Sort buffer list</string>
<string name="pref__buffer_list__sort_by_hot_summary">
Sort by number of highlights/private messages/unread messages</string>
<string-array name="pref__buffer_list__sort_buffer_list_names">
<item>By number</item>
<item>By hot messages, then by number</item>
<item>By hot messages, then by other messages, then by number</item>
</string-array>

<string name="pref__buffer_list__hide_server_buffers">
Hide non-conversation buffers</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

<!-- Don't touch these, they are internal names -->

<string-array name="pref__buffer_list__sort_buffer_list_values">
<item>by_number</item>
<item>by_hot_messages_then_by_number</item>
<item>by_hot_messages_then_by_other_messages_then_by_number</item>
</string-array>

<string-array name="pref__look_n_feel__alignment_values">
<item>left</item>
<item>right</item>
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@
<PreferenceScreen
android:key="bufferlist_group"
android:title="@string/pref__buffer_list_group">
<CheckBoxPreference
android:key="sort_buffers"
android:title="@string/pref__buffer_list__sort_by_hot"
android:summary="@string/pref__buffer_list__sort_by_hot_summary"
android:defaultValue="false" />
<ListPreference
android:key="sort_buffer_list"
android:title="@string/pref__buffer_list__sort_buffer_list"
android:summary="%s"
android:entries="@array/pref__buffer_list__sort_buffer_list_names"
android:entryValues="@array/pref__buffer_list__sort_buffer_list_values"
android:defaultValue="by_number" />
<CheckBoxPreference
android:key="filter_nonhuman_buffers"
android:title="@string/pref__buffer_list__hide_server_buffers"
Expand Down

0 comments on commit 82367c7

Please sign in to comment.