Skip to content

Commit 65d7218

Browse files
committed
channel [nfc]: Factor out channel-name compare function as static on store
1 parent 989630a commit 65d7218

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/model/channel.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ mixin ChannelStore on UserStore {
4242
/// and [streamsByName].
4343
Map<int, Subscription> get subscriptions;
4444

45+
static int compareChannelsByName(ZulipStream a, ZulipStream b) {
46+
// A user gave feedback wanting zulip-flutter to match web in putting
47+
// emoji-prefixed channels first; see #1202.
48+
// TODO(#1165) for matching web's ordering completely, which
49+
// (for the all-channels view) I think just means locale-aware sorting.
50+
final aStartsWithEmoji = _startsWithEmojiRegex.hasMatch(a.name);
51+
final bStartsWithEmoji = _startsWithEmojiRegex.hasMatch(b.name);
52+
if (aStartsWithEmoji && !bStartsWithEmoji) return -1;
53+
if (!aStartsWithEmoji && bStartsWithEmoji) return 1;
54+
55+
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
56+
}
57+
58+
// TODO(linter): The linter incorrectly flags the following regexp string
59+
// as invalid. See: https://github.com/dart-lang/sdk/issues/61246
60+
// ignore: valid_regexps
61+
static final _startsWithEmojiRegex = RegExp(r'^\p{Emoji}', unicode: true);
62+
4563
/// The visibility policy that the self-user has for the given topic.
4664
///
4765
/// This does not incorporate the user's channel-level policy,

lib/widgets/subscription_list.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22

33
import '../api/model/model.dart';
44
import '../generated/l10n/zulip_localizations.dart';
5+
import '../model/channel.dart';
56
import '../model/narrow.dart';
67
import '../model/unreads.dart';
78
import 'action_sheet.dart';
@@ -66,27 +67,12 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
6667
});
6768
}
6869

69-
// TODO(linter): The linter incorrectly flags the following regexp string
70-
// as invalid. See: https://github.com/dart-lang/sdk/issues/61246
71-
// ignore: valid_regexps
72-
static final _startsWithEmojiRegex = RegExp(r'^\p{Emoji}', unicode: true);
73-
7470
void _sortSubs(List<Subscription> list) {
7571
list.sort((a, b) {
7672
if (a.isMuted && !b.isMuted) return 1;
7773
if (!a.isMuted && b.isMuted) return -1;
7874

79-
// A user gave feedback wanting zulip-flutter to match web in putting
80-
// emoji-prefixed channels first; see #1202.
81-
// For matching web's ordering completely, see:
82-
// https://github.com/zulip/zulip-flutter/issues/1165
83-
final aStartsWithEmoji = _startsWithEmojiRegex.hasMatch(a.name);
84-
final bStartsWithEmoji = _startsWithEmojiRegex.hasMatch(b.name);
85-
if (aStartsWithEmoji && !bStartsWithEmoji) return -1;
86-
if (!aStartsWithEmoji && bStartsWithEmoji) return 1;
87-
88-
// TODO(i18n): add locale-aware sorting
89-
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
75+
return ChannelStore.compareChannelsByName(a, b);
9076
});
9177
}
9278

0 commit comments

Comments
 (0)