Skip to content

Commit 88be31f

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 941afe2 commit 88be31f

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
@@ -572,10 +572,9 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
572572
// The list has two slivers: a top sliver growing upward,
573573
// and a bottom sliver growing downward.
574574
// Each sliver has some of the items from `model!.items`.
575-
const maxBottomItems = 1;
576575
final totalItems = model!.items.length;
577-
final bottomItems = totalItems <= maxBottomItems ? totalItems : maxBottomItems;
578-
final topItems = totalItems - bottomItems;
576+
final topItems = model!.middleItem;
577+
final bottomItems = totalItems - topItems;
579578

580579
// The top sliver has its child 0 as the item just before the
581580
// 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
@@ -2006,6 +2006,10 @@ void checkInvariants(MessageListView model) {
20062006
});
20072007
}
20082008
check(model.items).length.equals(i);
2009+
2010+
check(model).middleItem
2011+
..isGreaterOrEqual(0)
2012+
..isLessOrEqual(model.items.length);
20092013
}
20102014

20112015
extension MessageListRecipientHeaderItemChecks on Subject<MessageListRecipientHeaderItem> {
@@ -2033,6 +2037,7 @@ extension MessageListViewChecks on Subject<MessageListView> {
20332037
Subject<List<Message>> get messages => has((x) => x.messages, 'messages');
20342038
Subject<List<ZulipMessageContent>> get contents => has((x) => x.contents, 'contents');
20352039
Subject<List<MessageListItem>> get items => has((x) => x.items, 'items');
2040+
Subject<int> get middleItem => has((x) => x.middleItem, 'middleItem');
20362041
Subject<bool> get fetched => has((x) => x.fetched, 'fetched');
20372042
Subject<bool> get haveOldest => has((x) => x.haveOldest, 'haveOldest');
20382043
Subject<bool> get fetchingOlder => has((x) => x.fetchingOlder, 'fetchingOlder');

0 commit comments

Comments
 (0)