Skip to content

Commit ab07916

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 f4c35e1 commit ab07916

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
@@ -137,8 +137,17 @@ mixin _MessageSequence {
137137
/// This information is completely derived from [messages] and
138138
/// the flags [haveOldest], [fetchingOlder] and [fetchOlderCoolingDown].
139139
/// It exists as an optimization, to memoize that computation.
140+
///
141+
/// See also [middleItem], an index which divides this list
142+
/// into a top slice and a bottom slice.
140143
final QueueList<MessageListItem> items = QueueList();
141144

145+
/// An index into [items] dividing it into a top slice and a bottom slice.
146+
///
147+
/// The indices 0 to before [middleItem] are the top slice of [items],
148+
/// and the indices from [middleItem] to the end are the bottom slice.
149+
int get middleItem => items.isEmpty ? 0 : items.length - 1;
150+
142151
int _findMessageWithId(int messageId) {
143152
return binarySearchByKey(messages, messageId,
144153
(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
@@ -573,10 +573,9 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
573573
// The list has two slivers: a top sliver growing upward,
574574
// and a bottom sliver growing downward.
575575
// Each sliver has some of the items from `model.items`.
576-
const maxBottomItems = 1;
577576
final totalItems = model.items.length;
578-
final bottomItems = totalItems <= maxBottomItems ? totalItems : maxBottomItems;
579-
final topItems = totalItems - bottomItems;
577+
final topItems = model.middleItem;
578+
final bottomItems = totalItems - topItems;
580579

581580
// The top sliver has its child 0 as the item just before the
582581
// 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
@@ -1997,6 +1997,10 @@ void checkInvariants(MessageListView model) {
19971997
});
19981998
}
19991999
check(model.items).length.equals(i);
2000+
2001+
check(model).middleItem
2002+
..isGreaterOrEqual(0)
2003+
..isLessOrEqual(model.items.length);
20002004
}
20012005

20022006
extension MessageListRecipientHeaderItemChecks on Subject<MessageListRecipientHeaderItem> {
@@ -2024,6 +2028,7 @@ extension MessageListViewChecks on Subject<MessageListView> {
20242028
Subject<List<Message>> get messages => has((x) => x.messages, 'messages');
20252029
Subject<List<ZulipMessageContent>> get contents => has((x) => x.contents, 'contents');
20262030
Subject<List<MessageListItem>> get items => has((x) => x.items, 'items');
2031+
Subject<int> get middleItem => has((x) => x.middleItem, 'middleItem');
20272032
Subject<bool> get fetched => has((x) => x.fetched, 'fetched');
20282033
Subject<bool> get haveOldest => has((x) => x.haveOldest, 'haveOldest');
20292034
Subject<bool> get fetchingOlder => has((x) => x.fetchingOlder, 'fetchingOlder');

0 commit comments

Comments
 (0)