Skip to content

Commit 09758b2

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 81d8901 commit 09758b2

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

lib/model/message_list.dart

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,7 @@ mixin _MessageSequence {
178178
/// Either the bottom slices of both [items] and [messages] are empty,
179179
/// or the first item in the bottom slice of [items] is a [MessageListMessageItem]
180180
/// for the first message in the bottom slice of [messages].
181-
int get middleItem {
182-
switch (items.lastOrNull) {
183-
case null:
184-
return 0;
185-
186-
case MessageListHistoryStartItem():
187-
assert(items.length == 1);
188-
return items.length;
189-
190-
case MessageListMessageItem():
191-
return items.length - 1;
192-
193-
case MessageListLoadingItem(direction: MessageListDirection.older):
194-
case MessageListRecipientHeaderItem():
195-
case MessageListDateSeparatorItem():
196-
assert(false, "unexpected type of last item");
197-
return items.length - 1;
198-
}
199-
}
181+
int middleItem = 0;
200182

201183
int _findMessageWithId(int messageId) {
202184
return binarySearchByKey(messages, messageId,
@@ -333,6 +315,7 @@ mixin _MessageSequence {
333315
_fetchOlderCooldownBackoffMachine = null;
334316
contents.clear();
335317
items.clear();
318+
middleItem = 0;
336319
}
337320

338321
/// Redo all computations from scratch, based on [messages].
@@ -372,6 +355,7 @@ mixin _MessageSequence {
372355
canShareSender = (prevMessageItem.message.senderId == message.senderId);
373356
}
374357
}
358+
if (index == middleMessage) middleItem = items.length;
375359
items.add(MessageListMessageItem(message, content,
376360
showSender: !canShareSender, isLastInBlock: true));
377361
}
@@ -394,8 +378,8 @@ mixin _MessageSequence {
394378
};
395379
switch ((startMarker != null, hasStartMarker)) {
396380
case (true, true): items[0] = startMarker!;
397-
case (true, _ ): items.addFirst(startMarker!);
398-
case (_, true): items.removeFirst();
381+
case (true, _ ): items.addFirst(startMarker!); middleItem++;
382+
case (_, true): items.removeFirst(); middleItem--;
399383
case (_, _ ): break;
400384
}
401385
}
@@ -406,6 +390,7 @@ mixin _MessageSequence {
406390
for (var i = 0; i < messages.length; i++) {
407391
_processMessage(i);
408392
}
393+
if (middleMessage == messages.length) middleItem = items.length;
409394
_updateEndMarkers();
410395
}
411396
}

0 commit comments

Comments
 (0)