Skip to content

Commit 81d8901

Browse files
committed
msglist [nfc]: Define sliver boundary by messages
This gives a bit more structured of an idea of what `middleItem` is supposed to mean. We'll use this for maintaining `middleItem` as a more dynamic value in upcoming commits.
1 parent 93ad5d0 commit 81d8901

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/model/message_list.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,20 @@ mixin _MessageSequence {
9191
/// conceptually belongs in this message list.
9292
/// That information is expressed in [fetched] and [haveOldest].
9393
///
94+
/// See also [middleMessage], an index which divides this list
95+
/// into a top slice and a bottom slice.
96+
///
9497
/// See also [contents] and [items].
9598
final List<Message> messages = [];
9699

100+
/// An index into [messages] dividing it into a top slice and a bottom slice.
101+
///
102+
/// The indices 0 to before [middleMessage] are the top slice of [messages],
103+
/// and the indices from [middleMessage] to the end are the bottom slice.
104+
///
105+
/// The corresponding item index is [middleItem].
106+
int get middleMessage => messages.isEmpty ? 0 : messages.length - 1;
107+
97108
/// Whether [messages] and [items] represent the results of a fetch.
98109
///
99110
/// This allows the UI to distinguish "still working on fetching messages"
@@ -162,8 +173,11 @@ mixin _MessageSequence {
162173
/// The indices 0 to before [middleItem] are the top slice of [items],
163174
/// and the indices from [middleItem] to the end are the bottom slice.
164175
///
165-
/// If the bottom slice is not empty,
166-
/// then its first item is a [MessageListMessageItem].
176+
/// The top and bottom slices of [items] correspond to
177+
/// the top and bottom slices of [messages] respectively.
178+
/// Either the bottom slices of both [items] and [messages] are empty,
179+
/// or the first item in the bottom slice of [items] is a [MessageListMessageItem]
180+
/// for the first message in the bottom slice of [messages].
167181
int get middleItem {
168182
switch (items.lastOrNull) {
169183
case null:

test/model/message_list_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,10 @@ void checkInvariants(MessageListView model) {
19611961
check(isSortedWithoutDuplicates(model.messages.map((m) => m.id).toList()))
19621962
.isTrue();
19631963

1964+
check(model).middleMessage
1965+
..isGreaterOrEqual(0)
1966+
..isLessOrEqual(model.messages.length);
1967+
19641968
check(model).contents.length.equals(model.messages.length);
19651969
for (int i = 0; i < model.contents.length; i++) {
19661970
final poll = model.messages[i].poll;
@@ -2010,8 +2014,11 @@ void checkInvariants(MessageListView model) {
20102014
check(model).middleItem
20112015
..isGreaterOrEqual(0)
20122016
..isLessOrEqual(model.items.length);
2013-
if (model.middleItem < model.items.length) {
2014-
check(model.items[model.middleItem]).isA<MessageListMessageItem>();
2017+
if (model.middleItem == model.items.length) {
2018+
check(model.middleMessage).equals(model.messages.length);
2019+
} else {
2020+
check(model.items[model.middleItem]).isA<MessageListMessageItem>()
2021+
.message.identicalTo(model.messages[model.middleMessage]);
20152022
}
20162023
}
20172024

@@ -2038,6 +2045,7 @@ extension MessageListViewChecks on Subject<MessageListView> {
20382045
Subject<PerAccountStore> get store => has((x) => x.store, 'store');
20392046
Subject<Narrow> get narrow => has((x) => x.narrow, 'narrow');
20402047
Subject<List<Message>> get messages => has((x) => x.messages, 'messages');
2048+
Subject<int> get middleMessage => has((x) => x.middleMessage, 'middleMessage');
20412049
Subject<List<ZulipMessageContent>> get contents => has((x) => x.contents, 'contents');
20422050
Subject<List<MessageListItem>> get items => has((x) => x.items, 'items');
20432051
Subject<int> get middleItem => has((x) => x.middleItem, 'middleItem');

0 commit comments

Comments
 (0)