Skip to content

Commit 93ad5d0

Browse files
committed
msglist: Always place the sliver boundary at a message
This brings the behavior closer to what we'll want in the future, and so allows the next commit affecting `middleItem` to be NFC. This itself is practically NFC. It's entirely NFC except in the case where there are no messages, so that `items` consists only of a history-start marker; and in that case, it causes that marker to go in the top sliver rather than the bottom. I believe the only observable effect of that change is that if the viewport is so very short that the history-start marker is too tall to fit (so that it's possible to scroll when that's the only item), then the initial scroll position will be different.
1 parent 88be31f commit 93ad5d0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/model/message_list.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,28 @@ mixin _MessageSequence {
161161
///
162162
/// The indices 0 to before [middleItem] are the top slice of [items],
163163
/// and the indices from [middleItem] to the end are the bottom slice.
164-
int get middleItem => items.isEmpty ? 0 : items.length - 1;
164+
///
165+
/// If the bottom slice is not empty,
166+
/// then its first item is a [MessageListMessageItem].
167+
int get middleItem {
168+
switch (items.lastOrNull) {
169+
case null:
170+
return 0;
171+
172+
case MessageListHistoryStartItem():
173+
assert(items.length == 1);
174+
return items.length;
175+
176+
case MessageListMessageItem():
177+
return items.length - 1;
178+
179+
case MessageListLoadingItem(direction: MessageListDirection.older):
180+
case MessageListRecipientHeaderItem():
181+
case MessageListDateSeparatorItem():
182+
assert(false, "unexpected type of last item");
183+
return items.length - 1;
184+
}
185+
}
165186

166187
int _findMessageWithId(int messageId) {
167188
return binarySearchByKey(messages, messageId,

test/model/message_list_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,9 @@ void checkInvariants(MessageListView model) {
20102010
check(model).middleItem
20112011
..isGreaterOrEqual(0)
20122012
..isLessOrEqual(model.items.length);
2013+
if (model.middleItem < model.items.length) {
2014+
check(model.items[model.middleItem]).isA<MessageListMessageItem>();
2015+
}
20132016
}
20142017

20152018
extension MessageListRecipientHeaderItemChecks on Subject<MessageListRecipientHeaderItem> {

0 commit comments

Comments
 (0)