From ac9f00c948a0154145a4e7e73750099d3780eb18 Mon Sep 17 00:00:00 2001 From: dab246 Date: Thu, 22 Sep 2022 19:07:55 +0700 Subject: [PATCH] TF-971 Implement method `injectAutoCompleteBindings` in all controller handle autocomplete --- lib/features/base/base_controller.dart | 5 +- .../reloadable/reloadable_controller.dart | 7 +- .../presentation/composer_controller.dart | 67 +++++++++---- .../composer/presentation/composer_view.dart | 9 +- .../presentation/composer_view_web.dart | 9 +- .../extensions/session_extension.dart | 9 -- .../presentation/contact_controller.dart | 67 +++++++++---- .../emails_forward_creator_binding.dart | 49 ++-------- .../emails_forward_creator_controller.dart | 95 +++++++++++-------- .../emails_forward_creator_view.dart | 7 +- ...rd_input_with_drop_list_field_builder.dart | 2 +- .../advanced_filter_controller.dart | 82 +++++++++++----- .../mailbox_dashboard_controller.dart | 4 +- .../advanced_search_filter_form.dart | 8 +- .../forward/forward_controller.dart | 14 +-- .../manage_account_dashboard_controller.dart | 8 +- 16 files changed, 239 insertions(+), 203 deletions(-) delete mode 100644 lib/features/composer/presentation/extensions/session_extension.dart diff --git a/lib/features/base/base_controller.dart b/lib/features/base/base_controller.dart index 6ea9de5de..04c22343c 100644 --- a/lib/features/base/base_controller.dart +++ b/lib/features/base/base_controller.dart @@ -74,12 +74,9 @@ abstract class BaseController extends GetxController with MessageDialogActionMix } void injectAutoCompleteBindings(Session? session, AccountId? accountId) { - if (session == null || accountId == null) { - return; - } try { ContactAutoCompleteBindings().dependencies(); - requireCapability(session, accountId, [tmailContactCapabilityIdentifier]); + requireCapability(session!, accountId!, [tmailContactCapabilityIdentifier]); TMailAutoCompleteBindings().dependencies(); } catch (e) { logError('BaseController::injectAutoCompleteBindings(): exception: $e'); diff --git a/lib/features/base/reloadable/reloadable_controller.dart b/lib/features/base/reloadable/reloadable_controller.dart index e351e2894..0dd300c60 100644 --- a/lib/features/base/reloadable/reloadable_controller.dart +++ b/lib/features/base/reloadable/reloadable_controller.dart @@ -20,7 +20,6 @@ import 'package:tmail_ui_user/features/login/domain/usecases/delete_credential_i import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_account_interactor.dart'; import 'package:tmail_ui_user/features/login/presentation/login_form_type.dart'; import 'package:tmail_ui_user/features/login/presentation/model/login_arguments.dart'; -import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/autocomplete_bindings.dart'; import 'package:tmail_ui_user/features/manage_account/data/local/language_cache_manager.dart'; import 'package:tmail_ui_user/features/manage_account/domain/state/log_out_oidc_state.dart'; import 'package:tmail_ui_user/features/manage_account/domain/usecases/log_out_oidc_interactor.dart'; @@ -188,16 +187,12 @@ abstract class ReloadableController extends BaseController { newConfig: tokenOidcSuccess.oidcConfiguration); } - void injectAutoCompleteBindings() { - AutoCompleteBindings().dependencies(); - } - void injectVacationBindings(Session? session, AccountId? accountId) { try { requireCapability(session!, accountId!, [CapabilityIdentifier.jmapVacationResponse]); VacationInteractorsBindings().dependencies(); } catch(e) { - logError('MailboxDashBoardController::injectVacationBindings(): exception: $e'); + logError('ReloadableController::injectVacationBindings(): exception: $e'); } } } \ No newline at end of file diff --git a/lib/features/composer/presentation/composer_controller.dart b/lib/features/composer/presentation/composer_controller.dart index b829447f5..f2020cbd3 100644 --- a/lib/features/composer/presentation/composer_controller.dart +++ b/lib/features/composer/presentation/composer_controller.dart @@ -14,7 +14,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:get/get.dart'; import 'package:http_parser/http_parser.dart'; +import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/id.dart'; +import 'package:jmap_dart_client/jmap/core/session/session.dart'; import 'package:jmap_dart_client/jmap/identities/identity.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; @@ -97,8 +99,8 @@ class ComposerController extends BaseController { final RichTextWebController richTextWebController; final DownloadImageAsBase64Interactor _downloadImageAsBase64Interactor; - late GetAutoCompleteWithDeviceContactInteractor _getAutoCompleteWithDeviceContactInteractor; - late GetAutoCompleteInteractor _getAutoCompleteInteractor; + GetAutoCompleteWithDeviceContactInteractor? _getAutoCompleteWithDeviceContactInteractor; + GetAutoCompleteInteractor? _getAutoCompleteInteractor; List listToEmailAddress = []; List listCcEmailAddress = []; @@ -314,6 +316,10 @@ class ComposerController extends BaseController { final arguments = kIsWeb ? mailboxDashBoardController.routerArguments : Get.arguments; if (arguments is ComposerArguments) { composerArguments.value = arguments; + injectAutoCompleteBindings( + mailboxDashBoardController.sessionCurrent, + mailboxDashBoardController.accountId.value); + if (arguments.emailActionType == EmailActionType.edit) { _getEmailContentAction(arguments); } @@ -721,30 +727,55 @@ class ComposerController extends BaseController { } } - Future> getAutoCompleteSuggestion( - {required String word}) async { - log('ComposerController::getAutoCompleteSuggestion(): $word | $_contactSuggestionSource'); + @override + void injectAutoCompleteBindings(Session? session, AccountId? accountId) { + try { + super.injectAutoCompleteBindings(session, accountId); + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('ComposerController::injectAutoCompleteBindings(): $e'); + } + } - if (!Get.isRegistered() || - !Get.isRegistered()) { - mailboxDashBoardController.injectAutoCompleteBindings(); + Future> getAutoCompleteSuggestion(String word) async { + log('ComposerController::getAutoCompleteSuggestion(): $word | $_contactSuggestionSource'); + try { + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('ComposerController::getAutoCompleteSuggestion(): Exception $e'); } - _getAutoCompleteWithDeviceContactInteractor = Get.find(); - _getAutoCompleteInteractor = Get.find(); + final accountId = mailboxDashBoardController.accountId.value; if (_contactSuggestionSource == ContactSuggestionSource.all) { - return await _getAutoCompleteWithDeviceContactInteractor - .execute(AutoCompletePattern(word: word, accountId: mailboxDashBoardController.accountId.value)) + if (_getAutoCompleteWithDeviceContactInteractor == null || _getAutoCompleteInteractor == null) { + return []; + } + + final listEmailAddress = await _getAutoCompleteWithDeviceContactInteractor! + .execute(AutoCompletePattern(word: word, accountId: accountId)) .then((value) => value.fold( (failure) => [], - (success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : [])); + (success) => success is GetAutoCompleteSuccess + ? success.listEmailAddress + : [])); + return listEmailAddress; + } else { + if (_getAutoCompleteInteractor == null) { + return []; + } + + final listEmailAddress = await _getAutoCompleteInteractor! + .execute(AutoCompletePattern(word: word, accountId: accountId)) + .then((value) => value.fold( + (failure) => [], + (success) => success is GetAutoCompleteSuccess + ? success.listEmailAddress + : [])); + return listEmailAddress; } - return await _getAutoCompleteInteractor - .execute(AutoCompletePattern(word: word, accountId: mailboxDashBoardController.accountId.value)) - .then((value) => value.fold( - (failure) => [], - (success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : [])); } void openPickAttachmentMenu(BuildContext context, List actionTiles) { diff --git a/lib/features/composer/presentation/composer_view.dart b/lib/features/composer/presentation/composer_view.dart index 3772c8a50..ed04c0bf4 100644 --- a/lib/features/composer/presentation/composer_view.dart +++ b/lib/features/composer/presentation/composer_view.dart @@ -298,8 +298,7 @@ class ComposerView extends GetWidget ..addOnShowFullListEmailAddressAction((prefixEmailAddress) => controller.showFullEmailAddress(prefixEmailAddress)) ..addOnAddEmailAddressTypeAction((prefixEmailAddress) => controller.addEmailAddressType(prefixEmailAddress)) ..addOnUpdateListEmailAddressAction((prefixEmailAddress, listEmailAddress) => controller.updateListEmailAddress(prefixEmailAddress, listEmailAddress)) - ..addOnOpenSuggestionBoxEmailAddress(() => controller.getAutoCompleteSuggestion(word: '')) - ..addOnSuggestionEmailAddress((word) => controller.getAutoCompleteSuggestion(word: word))) + ..addOnSuggestionEmailAddress(controller.getAutoCompleteSuggestion)) .build() )), Obx(() => controller.listEmailAddressType.contains(PrefixEmailAddress.cc) == true @@ -319,8 +318,7 @@ class ComposerView extends GetWidget ..addOnShowFullListEmailAddressAction((prefixEmailAddress) => controller.showFullEmailAddress(prefixEmailAddress)) ..addOnDeleteEmailAddressTypeAction((prefixEmailAddress) => controller.deleteEmailAddressType(prefixEmailAddress)) ..addOnUpdateListEmailAddressAction((prefixEmailAddress, listEmailAddress) => controller.updateListEmailAddress(prefixEmailAddress, listEmailAddress)) - ..addOnOpenSuggestionBoxEmailAddress(() => controller.getAutoCompleteSuggestion(word: '')) - ..addOnSuggestionEmailAddress((word) => controller.getAutoCompleteSuggestion(word: word))) + ..addOnSuggestionEmailAddress(controller.getAutoCompleteSuggestion)) .build()) : const SizedBox.shrink() ), @@ -341,8 +339,7 @@ class ComposerView extends GetWidget ..addOnShowFullListEmailAddressAction((prefixEmailAddress) => controller.showFullEmailAddress(prefixEmailAddress)) ..addOnDeleteEmailAddressTypeAction((prefixEmailAddress) => controller.deleteEmailAddressType(prefixEmailAddress)) ..addOnUpdateListEmailAddressAction((prefixEmailAddress, listEmailAddress) => controller.updateListEmailAddress(prefixEmailAddress, listEmailAddress)) - ..addOnOpenSuggestionBoxEmailAddress(() => controller.getAutoCompleteSuggestion(word: '')) - ..addOnSuggestionEmailAddress((word) => controller.getAutoCompleteSuggestion(word: word))) + ..addOnSuggestionEmailAddress(controller.getAutoCompleteSuggestion)) .build()) : const SizedBox.shrink() ), diff --git a/lib/features/composer/presentation/composer_view_web.dart b/lib/features/composer/presentation/composer_view_web.dart index 24cbb00f9..3cc3ef723 100644 --- a/lib/features/composer/presentation/composer_view_web.dart +++ b/lib/features/composer/presentation/composer_view_web.dart @@ -421,8 +421,7 @@ class ComposerView extends GetWidget ..addOnShowFullListEmailAddressAction((prefixEmailAddress) => controller.showFullEmailAddress(prefixEmailAddress)) ..addOnAddEmailAddressTypeAction((prefixEmailAddress) => controller.addEmailAddressType(prefixEmailAddress)) ..addOnUpdateListEmailAddressAction((prefixEmailAddress, listEmailAddress) => controller.updateListEmailAddress(prefixEmailAddress, listEmailAddress)) - ..addOnOpenSuggestionBoxEmailAddress(() => controller.getAutoCompleteSuggestion(word: '')) - ..addOnSuggestionEmailAddress((word) => controller.getAutoCompleteSuggestion(word: word))) + ..addOnSuggestionEmailAddress(controller.getAutoCompleteSuggestion)) .build() )), Obx(() => controller.listEmailAddressType.contains(PrefixEmailAddress.cc) == true @@ -442,8 +441,7 @@ class ComposerView extends GetWidget ..addOnShowFullListEmailAddressAction((prefixEmailAddress) => controller.showFullEmailAddress(prefixEmailAddress)) ..addOnDeleteEmailAddressTypeAction((prefixEmailAddress) => controller.deleteEmailAddressType(prefixEmailAddress)) ..addOnUpdateListEmailAddressAction((prefixEmailAddress, listEmailAddress) => controller.updateListEmailAddress(prefixEmailAddress, listEmailAddress)) - ..addOnOpenSuggestionBoxEmailAddress(() => controller.getAutoCompleteSuggestion(word: '')) - ..addOnSuggestionEmailAddress((word) => controller.getAutoCompleteSuggestion(word: word))) + ..addOnSuggestionEmailAddress(controller.getAutoCompleteSuggestion)) .build()) : const SizedBox.shrink() ), @@ -464,8 +462,7 @@ class ComposerView extends GetWidget ..addOnShowFullListEmailAddressAction((prefixEmailAddress) => controller.showFullEmailAddress(prefixEmailAddress)) ..addOnDeleteEmailAddressTypeAction((prefixEmailAddress) => controller.deleteEmailAddressType(prefixEmailAddress)) ..addOnUpdateListEmailAddressAction((prefixEmailAddress, listEmailAddress) => controller.updateListEmailAddress(prefixEmailAddress, listEmailAddress)) - ..addOnOpenSuggestionBoxEmailAddress(() => controller.getAutoCompleteSuggestion(word: '')) - ..addOnSuggestionEmailAddress((word) => controller.getAutoCompleteSuggestion(word: word))) + ..addOnSuggestionEmailAddress(controller.getAutoCompleteSuggestion)) .build()) : const SizedBox.shrink() ), diff --git a/lib/features/composer/presentation/extensions/session_extension.dart b/lib/features/composer/presentation/extensions/session_extension.dart deleted file mode 100644 index f052f1050..000000000 --- a/lib/features/composer/presentation/extensions/session_extension.dart +++ /dev/null @@ -1,9 +0,0 @@ - -import 'package:contact/contact_module.dart' as contact; -import 'package:jmap_dart_client/jmap/core/session/session.dart'; - -extension SessionExtension on Session { - - bool get hasSupportTmailAutoComplete => - capabilities.containsKey(contact.tmailContactCapabilityIdentifier); -} \ No newline at end of file diff --git a/lib/features/contact/presentation/contact_controller.dart b/lib/features/contact/presentation/contact_controller.dart index 79f899536..a83b022ee 100644 --- a/lib/features/contact/presentation/contact_controller.dart +++ b/lib/features/contact/presentation/contact_controller.dart @@ -9,17 +9,16 @@ import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; import 'package:model/autocomplete/auto_complete_pattern.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:debounce_throttle/debounce_throttle.dart'; +import 'package:tmail_ui_user/features/base/base_controller.dart'; import 'package:tmail_ui_user/features/composer/domain/model/contact_suggestion_source.dart'; import 'package:tmail_ui_user/features/composer/domain/state/get_autocomplete_state.dart'; import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_interactor.dart'; import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_with_device_contact_interactor.dart'; -import 'package:tmail_ui_user/features/composer/presentation/extensions/session_extension.dart'; import 'package:tmail_ui_user/features/contact/presentation/model/contact_arguments.dart'; -import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/autocomplete_bindings.dart'; import 'package:tmail_ui_user/features/thread/domain/model/search_query.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; -class ContactController extends GetxController { +class ContactController extends BaseController { final textInputSearchController = TextEditingController(); final textInputSearchFocus = FocusNode(); @@ -28,8 +27,8 @@ class ContactController extends GetxController { final searchQuery = SearchQuery.initial().obs; final listContactSearched = RxList(); - late GetAutoCompleteWithDeviceContactInteractor _getAutoCompleteWithDeviceContactInteractor; - late GetAutoCompleteInteractor _getAutoCompleteInteractor; + GetAutoCompleteWithDeviceContactInteractor? _getAutoCompleteWithDeviceContactInteractor; + GetAutoCompleteInteractor? _getAutoCompleteInteractor; late Debouncer _deBouncerTime; late AccountId _accountId; @@ -54,6 +53,7 @@ class ContactController extends GetxController { if (listContactSelected.isNotEmpty) { contactSelected = EmailAddress(listContactSelected.first, listContactSelected.first); } + injectAutoCompleteBindings(_session, _accountId); } if (!BuildUtils.isWeb) { Future.delayed( @@ -108,33 +108,52 @@ class ContactController extends GetxController { listContactSearched.value = listContact; } - Future> _getAutoCompleteSuggestion(String query) async { - if(_session.hasSupportTmailAutoComplete != true) return []; + @override + void injectAutoCompleteBindings(Session? session, AccountId? accountId) { + try { + super.injectAutoCompleteBindings(session, accountId); + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('ContactController::injectAutoCompleteBindings(): $e'); + } + } - if (!Get.isRegistered() || - !Get.isRegistered()) { - AutoCompleteBindings().dependencies(); + Future> _getAutoCompleteSuggestion(String query) async { + try { + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('ContactController::getAutoCompleteSuggestion(): Exception $e'); } - _getAutoCompleteWithDeviceContactInteractor = Get.find(); - _getAutoCompleteInteractor = Get.find(); - if (_contactSuggestionSource == ContactSuggestionSource.all) { - return await _getAutoCompleteWithDeviceContactInteractor + if (_getAutoCompleteWithDeviceContactInteractor == null || _getAutoCompleteInteractor == null) { + return []; + } + + final listEmailAddress = await _getAutoCompleteWithDeviceContactInteractor! .execute(AutoCompletePattern(word: query, limit: 30, accountId: _accountId)) .then((value) => value.fold( (failure) => [], (success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : [])); + return listEmailAddress; + } else { + if (_getAutoCompleteInteractor == null) { + return []; + } + + final listEmailAddress = await _getAutoCompleteInteractor! + .execute(AutoCompletePattern(word: query, limit: 30, accountId: _accountId)) + .then((value) => value.fold( + (failure) => [], + (success) => success is GetAutoCompleteSuccess + ? success.listEmailAddress + : [])); + return listEmailAddress; } - return await _getAutoCompleteInteractor - .execute(AutoCompletePattern(word: query, limit: 30, accountId: _accountId)) - .then((value) => value.fold( - (failure) => [], - (success) => success is GetAutoCompleteSuccess - ? success.listEmailAddress - : [])); } void selectContact(BuildContext context, EmailAddress emailAddress) { @@ -147,4 +166,10 @@ class ContactController extends GetxController { FocusScope.of(context).unfocus(); popBack(); } + + @override + void onDone() {} + + @override + void onError(error) {} } \ No newline at end of file diff --git a/lib/features/emails_forward_creator/presentation/emails_forward_creator_binding.dart b/lib/features/emails_forward_creator/presentation/emails_forward_creator_binding.dart index 0553e26c9..cc23966ae 100644 --- a/lib/features/emails_forward_creator/presentation/emails_forward_creator_binding.dart +++ b/lib/features/emails_forward_creator/presentation/emails_forward_creator_binding.dart @@ -1,63 +1,26 @@ -import 'package:contact/data/datasource/auto_complete_datasource.dart'; -import 'package:contact/data/datasource_impl/tmail_contact_datasource_impl.dart'; -import 'package:contact/data/network/contact_api.dart'; import 'package:get/get.dart'; import 'package:tmail_ui_user/features/base/base_bindings.dart'; -import 'package:tmail_ui_user/features/composer/data/datasource/contact_datasource.dart'; -import 'package:tmail_ui_user/features/composer/data/datasource_impl/contact_datasource_impl.dart'; -import 'package:tmail_ui_user/features/composer/data/repository/auto_complete_repository_impl.dart'; -import 'package:tmail_ui_user/features/composer/data/repository/contact_repository_impl.dart'; -import 'package:tmail_ui_user/features/composer/domain/repository/auto_complete_repository.dart'; -import 'package:tmail_ui_user/features/composer/domain/repository/contact_repository.dart'; -import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_interactor.dart'; -import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_with_device_contact_interactor.dart'; -import 'package:tmail_ui_user/features/composer/domain/usecases/get_device_contact_suggestions_interactor.dart'; import 'package:tmail_ui_user/features/emails_forward_creator/presentation/emails_forward_creator_controller.dart'; class EmailsForwardCreatorBindings extends BaseBindings { - final Set _dataSources = {}; - @override void bindingsController() { - Get.lazyPut(() => EmailsForwardCreatorController( - Get.find(), - Get.find(), - )); + Get.lazyPut(() => EmailsForwardCreatorController()); } @override - void bindingsDataSource() { - Get.lazyPut(() => Get.find()); - } + void bindingsDataSource() {} @override - void bindingsDataSourceImpl() { - _dataSources.clear(); - Get.lazyPut(() => ContactDataSourceImpl()); - Get.lazyPut(() => TMailContactDataSourceImpl(Get.find())); - _dataSources.add(Get.find()); - } + void bindingsDataSourceImpl() {} @override - void bindingsInteractor() { - Get.lazyPut(() => GetDeviceContactSuggestionsInteractor(Get.find())); - Get.lazyPut(() => GetAutoCompleteInteractor(Get.find())); - Get.lazyPut(() => GetAutoCompleteWithDeviceContactInteractor( - Get.find(), - Get.find() - )); - } + void bindingsInteractor() {} @override - void bindingsRepository() { - Get.lazyPut(() => Get.find()); - Get.lazyPut(() => Get.find()); - } + void bindingsRepository() {} @override - void bindingsRepositoryImpl() { - Get.lazyPut(() => ContactRepositoryImpl(Get.find())); - Get.lazyPut(() => AutoCompleteRepositoryImpl(_dataSources)); - } + void bindingsRepositoryImpl() {} } \ No newline at end of file diff --git a/lib/features/emails_forward_creator/presentation/emails_forward_creator_controller.dart b/lib/features/emails_forward_creator/presentation/emails_forward_creator_controller.dart index a696b6f71..fe8d5e4a2 100644 --- a/lib/features/emails_forward_creator/presentation/emails_forward_creator_controller.dart +++ b/lib/features/emails_forward_creator/presentation/emails_forward_creator_controller.dart @@ -12,8 +12,6 @@ import 'package:tmail_ui_user/features/composer/domain/model/contact_suggestion_ import 'package:tmail_ui_user/features/composer/domain/state/get_autocomplete_state.dart'; import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_interactor.dart'; import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_with_device_contact_interactor.dart'; -import 'package:tmail_ui_user/features/composer/presentation/extensions/session_extension.dart'; -import 'package:tmail_ui_user/features/emails_forward_creator/presentation/model/mails_forward_creator_arguments.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/forward/forward_controller.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; @@ -23,21 +21,20 @@ class EmailsForwardCreatorController extends BaseController { final ContactSuggestionSource _contactSuggestionSource = ContactSuggestionSource.tMailContact; final _forwardController = Get.find(); - late AccountId _accountId; - late Session? _session; - late GetAutoCompleteWithDeviceContactInteractor _getAutoCompleteWithDeviceContactInteractor; - late GetAutoCompleteInteractor _getAutoCompleteInteractor; + GetAutoCompleteWithDeviceContactInteractor? _getAutoCompleteWithDeviceContactInteractor; + GetAutoCompleteInteractor? _getAutoCompleteInteractor; final listEmailForwards = RxSet(); - EmailsForwardCreatorController( - this._getAutoCompleteWithDeviceContactInteractor, - this._getAutoCompleteInteractor, - ); + EmailsForwardCreatorController(); + + AccountId? get _accountId => _forwardController.emailsForwardCreatorArguments.value?.accountId; + + Session? get _session => _forwardController.emailsForwardCreatorArguments.value?.session; @override void onReady() { - _getArguments(); + injectAutoCompleteBindings(_session, _accountId); super.onReady(); } @@ -53,35 +50,53 @@ class EmailsForwardCreatorController extends BaseController { @override void onError(error) {} - void _getArguments() { - final arguments = Get.arguments; - if (arguments is MailsForwardCreatorArguments) { - _accountId = arguments.accountId; - _session = arguments.session; + @override + void injectAutoCompleteBindings(Session? session, AccountId? accountId) { + try { + super.injectAutoCompleteBindings(session, accountId); + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('EmailsForwardCreatorController::injectAutoCompleteBindings(): $e'); } } - Future> getAutoCompleteSuggestion( - {required String word}) async { + Future> getAutoCompleteSuggestion(String word) async { log('EmailsForwardCreatorController::getAutoCompleteSuggestion(): $word | $_contactSuggestionSource'); - - if(_session?.hasSupportTmailAutoComplete != true) return []; - - _getAutoCompleteWithDeviceContactInteractor = Get.find(); - _getAutoCompleteInteractor = Get.find(); + try { + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('EmailsForwardCreatorController::getAutoCompleteSuggestion(): Exception $e'); + } if (_contactSuggestionSource == ContactSuggestionSource.all) { - return await _getAutoCompleteWithDeviceContactInteractor - .execute(AutoCompletePattern(word: word, accountId: _accountId)) - .then((value) => value.fold( - (failure) => [], - (success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : [])); + if (_getAutoCompleteWithDeviceContactInteractor == null || _getAutoCompleteInteractor == null) { + logError('EmailsForwardCreatorController::_getAutoCompleteWithDeviceContactInteractor(): is null'); + return []; + } else { + return await _getAutoCompleteWithDeviceContactInteractor! + .execute(AutoCompletePattern(word: word, accountId: _accountId)) + .then((value) => value.fold( + (failure) => [], + (success) => success is GetAutoCompleteSuccess + ? success.listEmailAddress + : [])); + } + } else { + if (_getAutoCompleteInteractor == null) { + logError('EmailsForwardCreatorController::_getAutoCompleteInteractor(): is null'); + return []; + } else { + return await _getAutoCompleteInteractor! + .execute(AutoCompletePattern(word: word, accountId: _accountId)) + .then((value) => value.fold( + (failure) => [], + (success) => success is GetAutoCompleteSuccess + ? success.listEmailAddress + : [])); + } } - return await _getAutoCompleteInteractor - .execute(AutoCompletePattern(word: word, accountId: _accountId)) - .then((value) => value.fold( - (failure) => [], - (success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : [])); } void addToListEmailForwards(EmailAddress emailAddress) { @@ -100,6 +115,8 @@ class EmailsForwardCreatorController extends BaseController { void closeView(BuildContext context) { FocusScope.of(context).unfocus(); + clearAll(); + if(kIsWeb) { _clearListEmailForwards(); _forwardController.accountDashBoardController.emailsForwardCreatorIsActive.toggle(); @@ -113,14 +130,18 @@ class EmailsForwardCreatorController extends BaseController { if(inputEmailForwardController.text.trim().isNotEmpty) { listEmailForwards.add(EmailAddress(null, inputEmailForwardController.text.trim())); } - inputEmailForwardController.clear(); + clearAll(); + + if (listEmailForwards.isNotEmpty) { + final newListEmailForwards = List.from(listEmailForwards); + _forwardController.handleAddRecipients(newListEmailForwards); + } if(kIsWeb) { - _forwardController.accountDashBoardController.emailsForwardCreatorIsActive.toggle(); - _forwardController.handleAddRecipients(listEmailForwards.toList()); _clearListEmailForwards(); + _forwardController.accountDashBoardController.emailsForwardCreatorIsActive.toggle(); } else { - popBack(result: listEmailForwards.toList()); + popBack(); } } } \ No newline at end of file diff --git a/lib/features/emails_forward_creator/presentation/emails_forward_creator_view.dart b/lib/features/emails_forward_creator/presentation/emails_forward_creator_view.dart index 2ae19674f..1b994a594 100644 --- a/lib/features/emails_forward_creator/presentation/emails_forward_creator_view.dart +++ b/lib/features/emails_forward_creator/presentation/emails_forward_creator_view.dart @@ -139,12 +139,7 @@ class EmailsForwardCreatorView extends GetWidget controller.clearAll(); } }) - ..addOnSuggestionCallbackAction((pattern) { - if (pattern != null) { - controller.getAutoCompleteSuggestion(word: pattern.trim()); - } - })) - .build(), + ..addOnSuggestionCallbackAction(controller.getAutoCompleteSuggestion)).build(), ), Expanded( child: Obx(() => ListView.builder( diff --git a/lib/features/emails_forward_creator/presentation/widgets/emails_forward_input_with_drop_list_field_builder.dart b/lib/features/emails_forward_creator/presentation/widgets/emails_forward_input_with_drop_list_field_builder.dart index 19b3fe63f..07a3d4412 100644 --- a/lib/features/emails_forward_creator/presentation/widgets/emails_forward_input_with_drop_list_field_builder.dart +++ b/lib/features/emails_forward_creator/presentation/widgets/emails_forward_input_with_drop_list_field_builder.dart @@ -6,7 +6,7 @@ import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; import 'package:tmail_ui_user/features/emails_forward_creator/presentation/widgets/emails_forward_input_decoration_builder.dart'; typedef OnSelectedSuggestionAction = Function(EmailAddress emailAddress); -typedef OnSuggestionCallbackAction = Function(String? pattern); +typedef OnSuggestionCallbackAction = Function(String pattern); typedef OnSummitedCallbackAction = Function(String? pattern); class EmailsForwardInputWithDropListFieldBuilder { diff --git a/lib/features/mailbox_dashboard/presentation/controller/advanced_filter_controller.dart b/lib/features/mailbox_dashboard/presentation/controller/advanced_filter_controller.dart index 18c13bf00..5ee3a93f0 100644 --- a/lib/features/mailbox_dashboard/presentation/controller/advanced_filter_controller.dart +++ b/lib/features/mailbox_dashboard/presentation/controller/advanced_filter_controller.dart @@ -2,9 +2,12 @@ import 'package:collection/collection.dart'; import 'package:core/core.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:jmap_dart_client/jmap/account_id.dart'; +import 'package:jmap_dart_client/jmap/core/session/session.dart'; import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; import 'package:model/model.dart'; import 'package:permission_handler/permission_handler.dart'; +import 'package:tmail_ui_user/features/base/base_controller.dart'; import 'package:tmail_ui_user/features/composer/domain/model/contact_suggestion_source.dart'; import 'package:tmail_ui_user/features/composer/domain/state/get_autocomplete_state.dart'; import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_interactor.dart'; @@ -20,9 +23,9 @@ import 'package:tmail_ui_user/features/thread/domain/model/search_query.dart'; import 'package:tmail_ui_user/main/routes/app_routes.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; -class AdvancedFilterController extends GetxController { - late GetAutoCompleteWithDeviceContactInteractor _getAutoCompleteWithDeviceContactInteractor; - late GetAutoCompleteInteractor _getAutoCompleteInteractor; +class AdvancedFilterController extends BaseController { + GetAutoCompleteWithDeviceContactInteractor? _getAutoCompleteWithDeviceContactInteractor; + GetAutoCompleteInteractor? _getAutoCompleteInteractor; final dateFilterSelectedFormAdvancedSearch = EmailReceiveTimeType.allTime.obs; final hasAttachment = false.obs; @@ -49,11 +52,15 @@ class AdvancedFilterController extends GetxController { final focusManager = InputFieldFocusManager.initial(); @override - void onReady() async { + void onReady() { if (!BuildUtils.isWeb) { Future.delayed( - const Duration(milliseconds: 500), () => _checkContactPermission()); + const Duration(milliseconds: 500), + () => _checkContactPermission()); } + injectAutoCompleteBindings( + _mailboxDashBoardController.sessionCurrent, + _mailboxDashBoardController.accountId.value); super.onReady(); } @@ -148,36 +155,55 @@ class AdvancedFilterController extends GetxController { } } - Future> getAutoCompleteSuggestion( - {required String word}) async { + @override + void injectAutoCompleteBindings(Session? session, AccountId? accountId) { + try { + super.injectAutoCompleteBindings(session, accountId); + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('AdvancedFilterController::injectAutoCompleteBindings(): $e'); + } + } - if (!Get.isRegistered() || - !Get.isRegistered()) { - _mailboxDashBoardController.injectAutoCompleteBindings(); + Future> getAutoCompleteSuggestion(String word) async { + log('AdvancedFilterController::getAutoCompleteSuggestion(): $word | $_contactSuggestionSource'); + final accountId = _mailboxDashBoardController.accountId.value; + + try { + _getAutoCompleteWithDeviceContactInteractor = Get.find(); + _getAutoCompleteInteractor = Get.find(); + } catch (e) { + logError('AdvancedFilterController::getAutoCompleteSuggestion(): Exception $e'); } - _getAutoCompleteWithDeviceContactInteractor = Get.find(); - _getAutoCompleteInteractor = Get.find(); - if (_contactSuggestionSource == ContactSuggestionSource.all) { - return await _getAutoCompleteWithDeviceContactInteractor - .execute(AutoCompletePattern( - word: word, - accountId: _mailboxDashBoardController.accountId.value)) + if (_getAutoCompleteWithDeviceContactInteractor == null || _getAutoCompleteInteractor == null) { + return []; + } + + final listEmailAddress = await _getAutoCompleteWithDeviceContactInteractor! + .execute(AutoCompletePattern(word: word, accountId: accountId)) .then((value) => value.fold( (failure) => [], (success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : [])); + return listEmailAddress; + } else { + if (_getAutoCompleteInteractor == null) { + return []; + } + + final listEmailAddress = await _getAutoCompleteInteractor! + .execute(AutoCompletePattern(word: word, accountId: accountId)) + .then((value) => value.fold( + (failure) => [], + (success) => success is GetAutoCompleteSuccess + ? success.listEmailAddress + : [])); + return listEmailAddress; } - return await _getAutoCompleteInteractor - .execute(AutoCompletePattern( - word: word, accountId: _mailboxDashBoardController.accountId.value)) - .then((value) => value.fold( - (failure) => [], - (success) => success is GetAutoCompleteSuccess - ? success.listEmailAddress - : [])); } bool _checkAdvancedSearchHasApply() { @@ -219,4 +245,10 @@ class AdvancedFilterController extends GetxController { dateFilterInputController.dispose(); super.onClose(); } + + @override + void onDone() {} + + @override + void onError(error) {} } diff --git a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart index c073342ee..a411b060a 100644 --- a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart +++ b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart @@ -337,7 +337,7 @@ class MailboxDashBoardController extends ReloadableController { if (arguments is Session) { sessionCurrent = arguments; accountId.value = sessionCurrent?.accounts.keys.first; - injectAutoCompleteBindings(); + injectAutoCompleteBindings(sessionCurrent, accountId.value); injectVacationBindings(sessionCurrent, accountId.value); } else { if (kIsWeb) { @@ -885,7 +885,7 @@ class MailboxDashBoardController extends ReloadableController { accountId.value = sessionCurrent?.accounts.keys.first; _getUserProfile(); _handleComposerCache(); - injectAutoCompleteBindings(); + injectAutoCompleteBindings(sessionCurrent, accountId.value); injectVacationBindings(sessionCurrent, accountId.value); _getVacationResponse(); } diff --git a/lib/features/mailbox_dashboard/presentation/widgets/advanced_search/advanced_search_filter_form.dart b/lib/features/mailbox_dashboard/presentation/widgets/advanced_search/advanced_search_filter_form.dart index 1c76f3b93..4478d485f 100644 --- a/lib/features/mailbox_dashboard/presentation/widgets/advanced_search/advanced_search_filter_form.dart +++ b/lib/features/mailbox_dashboard/presentation/widgets/advanced_search/advanced_search_filter_form.dart @@ -246,9 +246,7 @@ class AdvancedSearchInputForm extends GetWidget const Padding(padding: EdgeInsets.all(4)), _responsiveUtils.isMobile(context) || _responsiveUtils.landscapeTabletSupported(context) ? TextFieldAutoCompleteEmailAddress( - optionsBuilder: (word) async { - return controller.getAutoCompleteSuggestion(word: word); - }, + optionsBuilder: controller.getAutoCompleteSuggestion, advancedSearchFilterField: advancedSearchFilterField, initialTags: listTagInitial, currentFocusNode: currentFocusNode, @@ -282,9 +280,7 @@ class AdvancedSearchInputForm extends GetWidget ) : Expanded( child: TextFieldAutoCompleteEmailAddress( - optionsBuilder: (word) async { - return controller.getAutoCompleteSuggestion(word: word); - }, + optionsBuilder: controller.getAutoCompleteSuggestion, advancedSearchFilterField: advancedSearchFilterField, initialTags: listTagInitial, currentFocusNode: currentFocusNode, diff --git a/lib/features/manage_account/presentation/forward/forward_controller.dart b/lib/features/manage_account/presentation/forward/forward_controller.dart index 70b89ae02..ecbf69c74 100644 --- a/lib/features/manage_account/presentation/forward/forward_controller.dart +++ b/lib/features/manage_account/presentation/forward/forward_controller.dart @@ -48,8 +48,8 @@ class ForwardController extends BaseController { final selectionMode = Rx(SelectMode.INACTIVE); final listRecipientForward = RxList(); - final currentForward = Rxn(); + final emailsForwardCreatorArguments = Rxn(); final listForwards = [].obs; @@ -239,17 +239,13 @@ class ForwardController extends BaseController { void goToAddEmailsForward() async { final accountId = accountDashBoardController.accountId.value; - if (accountId != null) { + final session = accountDashBoardController.sessionCurrent.value; + if (accountId != null && session != null) { + emailsForwardCreatorArguments.value = MailsForwardCreatorArguments(accountId, session); if (kIsWeb) { _openMailsForwardCreatorOverlay(); } else { - final listEmail = await push( - AppRoutes.EMAILS_FORWARD_CREATOR, - arguments: MailsForwardCreatorArguments(accountId, accountDashBoardController.sessionCurrent.value)); - - if (listEmail is List) { - handleAddRecipients(listEmail); - } + push(AppRoutes.EMAILS_FORWARD_CREATOR); } } } diff --git a/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart b/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart index 6a65d702d..4d2ab3de2 100644 --- a/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart +++ b/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart @@ -98,7 +98,7 @@ class ManageAccountDashBoardController extends ReloadableController { accountId.value = session.accounts.keys.first; sessionCurrent.value = session; _getUserProfile(); - injectAutoCompleteBindings(); + injectAutoCompleteBindings(sessionCurrent.value, accountId.value); injectVacationBindings(sessionCurrent.value, accountId.value); _getVacationResponse(); } @@ -110,7 +110,7 @@ class ManageAccountDashBoardController extends ReloadableController { accountId.value = arguments.session?.accounts.keys.first; sessionCurrent.value = arguments.session; _getUserProfile(); - injectAutoCompleteBindings(); + injectAutoCompleteBindings(sessionCurrent.value, accountId.value); injectVacationBindings(sessionCurrent.value, accountId.value); _getVacationResponse(); if (arguments.menuSettingCurrent != null) { @@ -131,13 +131,13 @@ class ManageAccountDashBoardController extends ReloadableController { _getAllVacationInteractor = Get.find(); _updateVacationInteractor = Get.find(); } catch (e) { - logError('MailboxDashBoardController::injectVacationBindings(): $e'); + logError('ManageAccountDashBoardController::injectVacationBindings(): $e'); } } Future _getAppVersion() async { final info = await PackageInfo.fromPlatform(); - log('MailboxDashBoardController::_getAppVersion(): ${info.version}'); + log('ManageAccountDashBoardController::_getAppVersion(): ${info.version}'); appInformation.value = info; }