Skip to content

Commit af4a1f4

Browse files
committed
autocomplete [nfc]: Use new mixin for ComposeAutocomplete's state
As with other uses of PerAccountStoreAwareStateMixin, we expect the benefit to be noticeable after our planned implementation of #185, "Start new event queue when old one has expired". At that time, this should mean that an expiring event queue won't break autocomplete: if the lifetime of your autocomplete intent spans across a queue-renewal, you can continue to edit your query after the renewal, and the app will ask the new store for the query results, instead of asking the old one.
1 parent 2e52afb commit af4a1f4

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

lib/widgets/autocomplete.dart

+20-4
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@ class ComposeAutocomplete extends StatefulWidget {
2626
State<ComposeAutocomplete> createState() => _ComposeAutocompleteState();
2727
}
2828

29-
class _ComposeAutocompleteState extends State<ComposeAutocomplete> {
29+
class _ComposeAutocompleteState extends State<ComposeAutocomplete> with PerAccountStoreAwareStateMixin<ComposeAutocomplete> {
3030
MentionAutocompleteView? _viewModel; // TODO different autocomplete view types
3131

32+
void _initViewModel() {
33+
final store = PerAccountStoreWidget.of(context);
34+
_viewModel = MentionAutocompleteView.init(store: store, narrow: widget.narrow)
35+
..addListener(_viewModelChanged);
36+
}
37+
3238
void _composeContentChanged() {
3339
final newAutocompleteIntent = widget.controller.autocompleteIntent();
3440
if (newAutocompleteIntent != null) {
35-
final store = PerAccountStoreWidget.of(context);
36-
_viewModel ??= MentionAutocompleteView.init(store: store, narrow: widget.narrow)
37-
..addListener(_viewModelChanged);
41+
if (_viewModel == null) {
42+
_initViewModel();
43+
}
3844
_viewModel!.query = newAutocompleteIntent.query;
3945
} else {
4046
if (_viewModel != null) {
@@ -51,6 +57,16 @@ class _ComposeAutocompleteState extends State<ComposeAutocomplete> {
5157
widget.controller.addListener(_composeContentChanged);
5258
}
5359

60+
@override
61+
void onNewStore() {
62+
if (_viewModel != null) {
63+
final query = _viewModel!.query;
64+
_viewModel!.dispose();
65+
_initViewModel();
66+
_viewModel!.query = query;
67+
}
68+
}
69+
5470
@override
5571
void didUpdateWidget(covariant ComposeAutocomplete oldWidget) {
5672
super.didUpdateWidget(oldWidget);

0 commit comments

Comments
 (0)