Skip to content

Commit 48983a3

Browse files
committed
msglist [nfc]: Move sliver boundary into the view-model
This will allow the model to maintain it over time as newer messages arrive or get fetched.
1 parent a8cee66 commit 48983a3

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

lib/model/message_list.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,17 @@ mixin _MessageSequence {
152152
/// This information is completely derived from [messages] and
153153
/// the flags [haveOldest], [fetchingOlder] and [fetchOlderCoolingDown].
154154
/// It exists as an optimization, to memoize that computation.
155+
///
156+
/// See also [middleItem], an index which divides this list
157+
/// into a top slice and a bottom slice.
155158
final QueueList<MessageListItem> items = QueueList();
156159

160+
/// An index into [items] dividing it into a top slice and a bottom slice.
161+
///
162+
/// The indices 0 to before [middleItem] are the top slice of [items],
163+
/// and the indices from [middleItem] to the end are the bottom slice.
164+
int get middleItem => items.isEmpty ? 0 : items.length - 1;
165+
157166
int _findMessageWithId(int messageId) {
158167
return binarySearchByKey(messages, messageId,
159168
(message, messageId) => message.id.compareTo(messageId));

lib/widgets/message_list.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,9 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
574574
// The list has two slivers: a top sliver growing upward,
575575
// and a bottom sliver growing downward.
576576
// Each sliver has some of the items from `model!.items`.
577-
const maxBottomItems = 1;
578577
final totalItems = model!.items.length;
579-
final bottomItems = totalItems <= maxBottomItems ? totalItems : maxBottomItems;
580-
final topItems = totalItems - bottomItems;
578+
final topItems = model!.middleItem;
579+
final bottomItems = totalItems - topItems;
581580

582581
// The top sliver has its child 0 as the item just before the
583582
// sliver boundary, child 1 as the item before that, and so on.

test/model/message_list_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,10 @@ void checkInvariants(MessageListView model) {
20022002
});
20032003
}
20042004
check(model.items).length.equals(i);
2005+
2006+
check(model).middleItem
2007+
..isGreaterOrEqual(0)
2008+
..isLessOrEqual(model.items.length);
20052009
}
20062010

20072011
extension MessageListRecipientHeaderItemChecks on Subject<MessageListRecipientHeaderItem> {
@@ -2029,6 +2033,7 @@ extension MessageListViewChecks on Subject<MessageListView> {
20292033
Subject<List<Message>> get messages => has((x) => x.messages, 'messages');
20302034
Subject<List<ZulipMessageContent>> get contents => has((x) => x.contents, 'contents');
20312035
Subject<List<MessageListItem>> get items => has((x) => x.items, 'items');
2036+
Subject<int> get middleItem => has((x) => x.middleItem, 'middleItem');
20322037
Subject<bool> get fetched => has((x) => x.fetched, 'fetched');
20332038
Subject<bool> get haveOldest => has((x) => x.haveOldest, 'haveOldest');
20342039
Subject<bool> get fetchingOlder => has((x) => x.fetchingOlder, 'fetchingOlder');

0 commit comments

Comments
 (0)