Skip to content

Commit 9f752aa

Browse files
gnpricePIG208
andcommitted
store [nfc]: Get selfUserId on substores from PerAccountStoreBase
Co-authored-by: Zixuan James Li <[email protected]>
1 parent ffda581 commit 9f752aa

7 files changed

+39
-37
lines changed

lib/model/recent_dm_conversations.dart

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ import '../api/model/initial_snapshot.dart';
77
import '../api/model/model.dart';
88
import '../api/model/events.dart';
99
import 'narrow.dart';
10+
import 'store.dart';
1011

1112
/// A view-model for the recent-DM-conversations UI.
1213
///
1314
/// This maintains the list of recent DM conversations,
1415
/// plus additional data in order to efficiently maintain the list.
15-
class RecentDmConversationsView extends ChangeNotifier {
16+
class RecentDmConversationsView extends PerAccountStoreBase with ChangeNotifier {
1617
factory RecentDmConversationsView({
18+
required CorePerAccountStore core,
1719
required List<RecentDmConversation> initial,
18-
required int selfUserId,
1920
}) {
2021
final entries = initial.map((conversation) => MapEntry(
21-
DmNarrow.ofRecentDmConversation(conversation, selfUserId: selfUserId),
22+
DmNarrow.ofRecentDmConversation(conversation, selfUserId: core.selfUserId),
2223
conversation.maxMessageId,
2324
)).toList()..sort((a, b) => -a.value.compareTo(b.value));
2425

@@ -33,18 +34,18 @@ class RecentDmConversationsView extends ChangeNotifier {
3334
}
3435

3536
return RecentDmConversationsView._(
37+
core: core,
3638
map: Map.fromEntries(entries),
3739
sorted: QueueList.from(entries.map((e) => e.key)),
3840
latestMessagesByRecipient: latestMessagesByRecipient,
39-
selfUserId: selfUserId,
4041
);
4142
}
4243

4344
RecentDmConversationsView._({
45+
required super.core,
4446
required this.map,
4547
required this.sorted,
4648
required this.latestMessagesByRecipient,
47-
required this.selfUserId,
4849
});
4950

5051
/// The latest message ID in each conversation.
@@ -62,8 +63,6 @@ class RecentDmConversationsView extends ChangeNotifier {
6263
/// it might have been sent by anyone in its conversation.)
6364
final Map<int, int> latestMessagesByRecipient;
6465

65-
final int selfUserId;
66-
6766
/// Insert the key at the proper place in [sorted].
6867
///
6968
/// Optimized, taking O(1) time, for the case where that place is the start,

lib/model/store.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -482,19 +482,18 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
482482
milliseconds: initialSnapshot.serverTypingStartedWaitPeriodMilliseconds),
483483
),
484484
users: UserStoreImpl(core: core, initialSnapshot: initialSnapshot),
485-
typingStatus: TypingStatus(
486-
selfUserId: account.userId,
485+
typingStatus: TypingStatus(core: core,
487486
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds),
488487
),
489488
channels: channels,
490489
messages: MessageStoreImpl(core: core),
491490
unreads: Unreads(
492491
initial: initialSnapshot.unreadMsgs,
493-
selfUserId: account.userId,
492+
core: core,
494493
channelStore: channels,
495494
),
496-
recentDmConversationsView: RecentDmConversationsView(
497-
initial: initialSnapshot.recentPrivateConversations, selfUserId: account.userId),
495+
recentDmConversationsView: RecentDmConversationsView(core: core,
496+
initial: initialSnapshot.recentPrivateConversations),
498497
recentSenders: RecentSenders(),
499498
);
500499
}

lib/model/typing_status.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import 'store.dart';
1111
/// The model for tracking the typing status organized by narrows.
1212
///
1313
/// Listeners are notified when a typist is added or removed from any narrow.
14-
class TypingStatus extends ChangeNotifier {
14+
class TypingStatus extends PerAccountStoreBase with ChangeNotifier {
1515
TypingStatus({
16-
required this.selfUserId,
16+
required super.core,
1717
required this.typingStartedExpiryPeriod,
1818
});
1919

20-
final int selfUserId;
2120
final Duration typingStartedExpiryPeriod;
2221

2322
Iterable<SendableNarrow> get debugActiveNarrows => _timerMapsByNarrow.keys;

lib/model/unreads.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import '../log.dart';
1010
import 'algorithms.dart';
1111
import 'narrow.dart';
1212
import 'channel.dart';
13+
import 'store.dart';
1314

1415
/// The view-model for unread messages.
1516
///
@@ -34,10 +35,10 @@ import 'channel.dart';
3435
// sync to those unreads, because the user has shown an interest in them.
3536
// TODO When loading a message list with stream messages, check all the stream
3637
// messages and refresh [mentions] (see [mentions] dartdoc).
37-
class Unreads extends ChangeNotifier {
38+
class Unreads extends PerAccountStoreBase with ChangeNotifier {
3839
factory Unreads({
3940
required UnreadMessagesSnapshot initial,
40-
required int selfUserId,
41+
required CorePerAccountStore core,
4142
required ChannelStore channelStore,
4243
}) {
4344
final streams = <int, Map<TopicName, QueueList<int>>>{};
@@ -52,32 +53,33 @@ class Unreads extends ChangeNotifier {
5253

5354
for (final unreadDmSnapshot in initial.dms) {
5455
final otherUserId = unreadDmSnapshot.otherUserId;
55-
final narrow = DmNarrow.withUser(otherUserId, selfUserId: selfUserId);
56+
final narrow = DmNarrow.withUser(otherUserId, selfUserId: core.selfUserId);
5657
dms[narrow] = QueueList.from(unreadDmSnapshot.unreadMessageIds);
5758
}
5859

5960
for (final unreadHuddleSnapshot in initial.huddles) {
60-
final narrow = DmNarrow.ofUnreadHuddleSnapshot(unreadHuddleSnapshot, selfUserId: selfUserId);
61+
final narrow = DmNarrow.ofUnreadHuddleSnapshot(unreadHuddleSnapshot,
62+
selfUserId: core.selfUserId);
6163
dms[narrow] = QueueList.from(unreadHuddleSnapshot.unreadMessageIds);
6264
}
6365

6466
return Unreads._(
67+
core: core,
6568
channelStore: channelStore,
6669
streams: streams,
6770
dms: dms,
6871
mentions: mentions,
6972
oldUnreadsMissing: initial.oldUnreadsMissing,
70-
selfUserId: selfUserId,
7173
);
7274
}
7375

7476
Unreads._({
77+
required super.core,
7578
required this.channelStore,
7679
required this.streams,
7780
required this.dms,
7881
required this.mentions,
7982
required this.oldUnreadsMissing,
80-
required this.selfUserId,
8183
});
8284

8385
final ChannelStore channelStore;
@@ -125,8 +127,6 @@ class Unreads extends ChangeNotifier {
125127
/// Is set to false when the user clears out all unreads.
126128
bool oldUnreadsMissing;
127129

128-
final int selfUserId;
129-
130130
// TODO(#370): maintain this count incrementally, rather than recomputing from scratch
131131
int countInCombinedFeedNarrow() {
132132
int c = 0;

test/model/recent_dm_conversations_test.dart

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:zulip/model/recent_dm_conversations.dart';
66

77
import '../example_data.dart' as eg;
88
import 'recent_dm_conversations_checks.dart';
9+
import 'store_checks.dart';
910

1011
void main() {
1112
group('RecentDmConversationsView', () {
@@ -18,18 +19,19 @@ void main() {
1819
}
1920

2021
test('construct from initial data', () {
21-
check(RecentDmConversationsView(selfUserId: eg.selfUser.userId,
22-
initial: []))
22+
check(eg.store(initialSnapshot: eg.initialSnapshot(
23+
recentPrivateConversations: [],
24+
))).recentDmConversationsView
2325
..map.isEmpty()
2426
..sorted.isEmpty()
2527
..latestMessagesByRecipient.isEmpty();
2628

27-
check(RecentDmConversationsView(selfUserId: eg.selfUser.userId,
28-
initial: [
29+
check(eg.store(initialSnapshot: eg.initialSnapshot(
30+
recentPrivateConversations: [
2931
RecentDmConversation(userIds: [], maxMessageId: 200),
3032
RecentDmConversation(userIds: [1], maxMessageId: 100),
3133
RecentDmConversation(userIds: [2, 1], maxMessageId: 300), // userIds out of order
32-
]))
34+
]))).recentDmConversationsView
3335
..map.deepEquals({
3436
key([1, 2]): 300,
3537
key([]): 200,
@@ -41,11 +43,11 @@ void main() {
4143

4244
group('message event (new message)', () {
4345
RecentDmConversationsView setupView() {
44-
return RecentDmConversationsView(selfUserId: eg.selfUser.userId,
45-
initial: [
46+
return eg.store(initialSnapshot: eg.initialSnapshot(
47+
recentPrivateConversations: [
4648
RecentDmConversation(userIds: [1], maxMessageId: 200),
4749
RecentDmConversation(userIds: [1, 2], maxMessageId: 100),
48-
]);
50+
])).recentDmConversationsView;
4951
}
5052

5153
test('(check base state)', () {

test/model/typing_status_test.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ void main() {
7777
int? selfUserId,
7878
Map<SendableNarrow, List<User>> typistsByNarrow = const {},
7979
}) {
80-
model = TypingStatus(
81-
selfUserId: selfUserId ?? eg.selfUser.userId,
82-
typingStartedExpiryPeriod: const Duration(milliseconds: 15000));
80+
final store = eg.store(
81+
account: eg.selfAccount.copyWith(id: selfUserId),
82+
initialSnapshot: eg.initialSnapshot(
83+
serverTypingStartedExpiryPeriodMilliseconds: 15000));
84+
model = store.typingStatus;
8385
check(model.debugActiveNarrows).isEmpty();
8486
notifiedCount = 0;
8587
model.addListener(() => notifiedCount += 1);

test/model/unreads_test.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ void main() {
3737
oldUnreadsMissing: false,
3838
),
3939
}) {
40-
channelStore = eg.store();
40+
final store = eg.store(
41+
initialSnapshot: eg.initialSnapshot(unreadMsgs: initial));
42+
channelStore = store;
4143
notifiedCount = 0;
42-
model = Unreads(initial: initial,
43-
selfUserId: eg.selfUser.userId, channelStore: channelStore)
44+
model = store.unreads
4445
..addListener(() {
4546
notifiedCount++;
4647
});

0 commit comments

Comments
 (0)