Skip to content

Commit 489f48a

Browse files
committed
msglist [nfc]: Maintain middleItem as a field
This new logic maintains `middleItem` according to its documented relationship with `middleMessage`. Because of the current definition of `middleMessage`, that produces the same result as the previous definition of `middleItem`. The key reasoning for why this logic works is: this touches all the code that modifies `items`, to ensure that code keeps `middleItem` up to date. And all the code which modifies `messages` (which is the only way to modify `middleMessage`) already calls `_reprocessAll` to compute `items` from scratch, except one site in `_addMessage`. Studying `_addMessage`, it also maintains `middleItem` correctly, though for that conclusion one needs the specifics of the definition of `middleMessage`. This change involves no new test code: all this logic is in scenarios well exercised by existing tests, and the invariant-checks introduced in the previous commit then effectively test this logic. To be sure of that, I also confirmed that commenting out any one of these updates to `middleItem` causes some tests to fail.
1 parent 0aec5c1 commit 489f48a

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

lib/model/message_list.dart

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,25 +163,7 @@ mixin _MessageSequence {
163163
/// Either the bottom slices of both [items] and [messages] are empty,
164164
/// or the first item in the bottom slice of [items] is a [MessageListMessageItem]
165165
/// for the first message in the bottom slice of [messages].
166-
int get middleItem {
167-
switch (items.lastOrNull) {
168-
case null:
169-
return 0;
170-
171-
case MessageListHistoryStartItem():
172-
assert(items.length == 1);
173-
return items.length;
174-
175-
case MessageListMessageItem():
176-
return items.length - 1;
177-
178-
case MessageListLoadingItem(direction: MessageListDirection.older):
179-
case MessageListRecipientHeaderItem():
180-
case MessageListDateSeparatorItem():
181-
assert(false, "unexpected type of last item");
182-
return items.length - 1;
183-
}
184-
}
166+
int middleItem = 0;
185167

186168
int _findMessageWithId(int messageId) {
187169
return binarySearchByKey(messages, messageId,
@@ -313,6 +295,7 @@ mixin _MessageSequence {
313295
_fetchOlderCooldownBackoffMachine = null;
314296
contents.clear();
315297
items.clear();
298+
middleItem = 0;
316299
}
317300

318301
/// Redo all computations from scratch, based on [messages].
@@ -352,6 +335,7 @@ mixin _MessageSequence {
352335
canShareSender = (prevMessageItem.message.senderId == message.senderId);
353336
}
354337
}
338+
if (index == middleMessage) middleItem = items.length;
355339
items.add(MessageListMessageItem(message, content,
356340
showSender: !canShareSender, isLastInBlock: true));
357341
}
@@ -362,6 +346,7 @@ mixin _MessageSequence {
362346
for (var i = 0; i < messages.length; i++) {
363347
_processMessage(i);
364348
}
349+
if (middleMessage == messages.length) middleItem = items.length;
365350
}
366351
}
367352

0 commit comments

Comments
 (0)