Skip to content

Commit

Permalink
autocomplete [nfc]: Use new mixin for ComposeAutocomplete's state
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chrisbobbe committed Aug 1, 2023
1 parent 1303d07 commit f29f3cc
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/widgets/autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ class ComposeAutocomplete extends StatefulWidget {
State<ComposeAutocomplete> createState() => _ComposeAutocompleteState();
}

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

void _initViewModel() {
final store = PerAccountStoreWidget.of(context);
_viewModel = MentionAutocompleteView.init(store: store, narrow: widget.narrow)
..addListener(_viewModelChanged);
}

void _composeContentChanged() {
final newAutocompleteIntent = widget.controller.autocompleteIntent();
if (newAutocompleteIntent != null) {
final store = PerAccountStoreWidget.of(context);
_viewModel ??= MentionAutocompleteView.init(store: store, narrow: widget.narrow)
..addListener(_viewModelChanged);
if (_viewModel == null) {
_initViewModel();
}
_viewModel!.query = newAutocompleteIntent.query;
} else {
if (_viewModel != null) {
Expand All @@ -51,6 +57,16 @@ class _ComposeAutocompleteState extends State<ComposeAutocomplete> {
widget.controller.addListener(_composeContentChanged);
}

@override
void onNewStore() {
if (_viewModel != null) {
final query = _viewModel!.query;
_viewModel!.dispose();
_initViewModel();
_viewModel!.query = query;
}
}

@override
void didUpdateWidget(covariant ComposeAutocomplete oldWidget) {
super.didUpdateWidget(oldWidget);
Expand Down

0 comments on commit f29f3cc

Please sign in to comment.