-
Notifications
You must be signed in to change notification settings - Fork 306
msglist [nfc]: Handle start markers within widget code, not model #1512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
86e2397
a6e0efd
4b69adf
c5e6957
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -448,8 +448,11 @@ class MessageList extends StatefulWidget { | |
} | ||
|
||
class _MessageListState extends State<MessageList> with PerAccountStoreAwareStateMixin<MessageList> { | ||
MessageListView? model; | ||
MessageListView get model => _model!; | ||
MessageListView? _model; | ||
|
||
final MessageListScrollController scrollController = MessageListScrollController(); | ||
|
||
final ValueNotifier<bool> _scrollToBottomVisible = ValueNotifier<bool>(false); | ||
|
||
@override | ||
|
@@ -460,32 +463,32 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
|
||
@override | ||
void onNewStore() { // TODO(#464) try to keep using old model until new one gets messages | ||
model?.dispose(); | ||
_model?.dispose(); | ||
_initModel(PerAccountStoreWidget.of(context)); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
model?.dispose(); | ||
_model?.dispose(); | ||
scrollController.dispose(); | ||
_scrollToBottomVisible.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
void _initModel(PerAccountStore store) { | ||
model = MessageListView.init(store: store, narrow: widget.narrow); | ||
model!.addListener(_modelChanged); | ||
model!.fetchInitial(); | ||
_model = MessageListView.init(store: store, narrow: widget.narrow); | ||
model.addListener(_modelChanged); | ||
model.fetchInitial(); | ||
} | ||
|
||
void _modelChanged() { | ||
if (model!.narrow != widget.narrow) { | ||
if (model.narrow != widget.narrow) { | ||
// Either: | ||
// - A message move event occurred, where propagate mode is | ||
// [PropagateMode.changeAll] or [PropagateMode.changeLater]. Or: | ||
// - We fetched a "with" / topic-permalink narrow, and the response | ||
// redirected us to the new location of the operand message ID. | ||
widget.onNarrowChanged(model!.narrow); | ||
widget.onNarrowChanged(model.narrow); | ||
} | ||
setState(() { | ||
// The actual state lives in the [MessageListView] model. | ||
|
@@ -507,7 +510,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
// but makes things a bit more complicated to reason about. | ||
// The cause seems to be that this gets called again with maxScrollExtent | ||
// still not yet updated to account for the newly-added messages. | ||
model?.fetchOlder(); | ||
model.fetchOlder(); | ||
} | ||
} | ||
|
||
|
@@ -528,8 +531,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
|
||
@override | ||
Widget build(BuildContext context) { | ||
assert(model != null); | ||
if (!model!.fetched) return const Center(child: CircularProgressIndicator()); | ||
if (!model.fetched) return const Center(child: CircularProgressIndicator()); | ||
|
||
// Pad the left and right insets, for small devices in landscape. | ||
return SafeArea( | ||
|
@@ -567,13 +569,12 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
|
||
Widget _buildListView(BuildContext context) { | ||
const centerSliverKey = ValueKey('center sliver'); | ||
final zulipLocalizations = ZulipLocalizations.of(context); | ||
|
||
// The list has two slivers: a top sliver growing upward, | ||
// and a bottom sliver growing downward. | ||
// Each sliver has some of the items from `model!.items`. | ||
// Each sliver has some of the items from `model.items`. | ||
const maxBottomItems = 1; | ||
final totalItems = model!.items.length; | ||
final totalItems = model.items.length; | ||
final bottomItems = totalItems <= maxBottomItems ? totalItems : maxBottomItems; | ||
final topItems = totalItems - bottomItems; | ||
|
||
|
@@ -599,17 +600,19 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
// and will not trigger this callback. | ||
findChildIndexCallback: (Key key) { | ||
final messageId = (key as ValueKey<int>).value; | ||
final itemIndex = model!.findItemWithMessageId(messageId); | ||
final itemIndex = model.findItemWithMessageId(messageId); | ||
if (itemIndex == -1) return null; | ||
final childIndex = totalItems - 1 - (itemIndex + bottomItems); | ||
if (childIndex < 0) return null; | ||
return childIndex; | ||
}, | ||
childCount: topItems, | ||
childCount: topItems + 1, | ||
(context, childIndex) { | ||
if (childIndex == topItems) return _buildStartCap(); | ||
|
||
final itemIndex = totalItems - 1 - (childIndex + bottomItems); | ||
final data = model!.items[itemIndex]; | ||
final item = _buildItem(zulipLocalizations, data); | ||
final data = model.items[itemIndex]; | ||
final item = _buildItem(data); | ||
return item; | ||
})); | ||
|
||
|
@@ -637,7 +640,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
// and will not trigger this callback. | ||
findChildIndexCallback: (Key key) { | ||
final messageId = (key as ValueKey<int>).value; | ||
final itemIndex = model!.findItemWithMessageId(messageId); | ||
final itemIndex = model.findItemWithMessageId(messageId); | ||
if (itemIndex == -1) return null; | ||
final childIndex = itemIndex - topItems; | ||
if (childIndex < 0) return null; | ||
|
@@ -654,8 +657,8 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
if (childIndex == bottomItems) return TypingStatusWidget(narrow: widget.narrow); | ||
|
||
final itemIndex = topItems + childIndex; | ||
final data = model!.items[itemIndex]; | ||
return _buildItem(zulipLocalizations, data); | ||
final data = model.items[itemIndex]; | ||
return _buildItem(data); | ||
})); | ||
|
||
if (!ComposeBox.hasComposeBox(widget.narrow)) { | ||
|
@@ -687,18 +690,21 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
]); | ||
} | ||
|
||
Widget _buildItem(ZulipLocalizations zulipLocalizations, MessageListItem data) { | ||
Widget _buildStartCap() { | ||
// These assertions are invariants of [MessageListView]. | ||
assert(!(model.fetchingOlder && model.fetchOlderCoolingDown)); | ||
final effectiveFetchingOlder = | ||
model.fetchingOlder || model.fetchOlderCoolingDown; | ||
assert(!(model.haveOldest && effectiveFetchingOlder)); | ||
Comment on lines
+693
to
+698
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I suppose I feel that it still applies here, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the bulk of the code on In I think this method's logic also makes sense even without the assumption that |
||
return switch ((effectiveFetchingOlder, model.haveOldest)) { | ||
(true, _) => const _MessageListLoadingMore(), | ||
(_, true) => const _MessageListHistoryStart(), | ||
(_, _) => const SizedBox.shrink(), | ||
}; | ||
} | ||
|
||
Widget _buildItem(MessageListItem data) { | ||
switch (data) { | ||
case MessageListHistoryStartItem(): | ||
return Center( | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric(vertical: 16.0), | ||
child: Text(zulipLocalizations.noEarlierMessages))); // TODO use an icon | ||
case MessageListLoadingItem(): | ||
return const Center( | ||
child: Padding( | ||
padding: EdgeInsets.symmetric(vertical: 16.0), | ||
child: CircularProgressIndicator())); // TODO perhaps a different indicator | ||
case MessageListRecipientHeaderItem(): | ||
final header = RecipientHeader(message: data.message, narrow: widget.narrow); | ||
return StickyHeaderItem(allowOverflow: true, | ||
|
@@ -719,6 +725,31 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat | |
} | ||
} | ||
|
||
class _MessageListHistoryStart extends StatelessWidget { | ||
const _MessageListHistoryStart(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final zulipLocalizations = ZulipLocalizations.of(context); | ||
return Center( | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric(vertical: 16.0), | ||
child: Text(zulipLocalizations.noEarlierMessages))); // TODO use an icon | ||
} | ||
} | ||
|
||
class _MessageListLoadingMore extends StatelessWidget { | ||
const _MessageListLoadingMore(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Center( | ||
child: Padding( | ||
padding: EdgeInsets.symmetric(vertical: 16.0), | ||
child: CircularProgressIndicator())); // TODO perhaps a different indicator | ||
} | ||
} | ||
|
||
class ScrollToBottomButton extends StatelessWidget { | ||
const ScrollToBottomButton({super.key, required this.scrollController, required this.visible}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wonder if
dispose
can ever get called before_initModel
.I guess the answer is no. When
dispose
is called, theState
should be in its "ready" state.and from the dart doc of
_StateLifecycle
, "ready" is later than "created" and "initialized" inState
objects' lifecycle:so
State.didChangeDependencies
(and thenonNewStore
) should have been called already.On a side-note, we have been cautious about this at other places. While the framework might maintain this guarantee that
onNewStore
is called beforedispose
is called, it's probably easier to rest assure that?.dispose()
is always going to be safe. So I'm actually not sure if we want a null-assert here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks for the investigation.
Given those results, I think there would be some value in systematically having our
dispose
methods assume thatdidChangeDependencies
(and soonNewStore
where applicable) has been called. By visibly relying on that guarantee from the framework, it'd help teach the reader that that guarantee is there, and give confidence in relying on it in whatever new code they're writing.That'd be an independent change from this one, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah,
_EmojiPickerState
seems like a place where a similar refactor may apply. The teaching aspect of this is refreshing think about.