TF-971 Implement method injectAutoCompleteBindings in all controller handle autocomplete

This commit is contained in:
dab246
2022-09-22 19:07:55 +07:00
committed by Dat H. Pham
parent 129118bd3c
commit ac9f00c948
16 changed files with 239 additions and 203 deletions
@@ -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<AutoCompleteDataSource> _dataSources = {};
@override
void bindingsController() {
Get.lazyPut(() => EmailsForwardCreatorController(
Get.find<GetAutoCompleteWithDeviceContactInteractor>(),
Get.find<GetAutoCompleteInteractor>(),
));
Get.lazyPut(() => EmailsForwardCreatorController());
}
@override
void bindingsDataSource() {
Get.lazyPut<ContactDataSource>(() => Get.find<ContactDataSourceImpl>());
}
void bindingsDataSource() {}
@override
void bindingsDataSourceImpl() {
_dataSources.clear();
Get.lazyPut(() => ContactDataSourceImpl());
Get.lazyPut(() => TMailContactDataSourceImpl(Get.find<ContactAPI>()));
_dataSources.add(Get.find<TMailContactDataSourceImpl>());
}
void bindingsDataSourceImpl() {}
@override
void bindingsInteractor() {
Get.lazyPut(() => GetDeviceContactSuggestionsInteractor(Get.find<ContactRepository>()));
Get.lazyPut(() => GetAutoCompleteInteractor(Get.find<AutoCompleteRepository>()));
Get.lazyPut(() => GetAutoCompleteWithDeviceContactInteractor(
Get.find<GetAutoCompleteInteractor>(),
Get.find<GetDeviceContactSuggestionsInteractor>()
));
}
void bindingsInteractor() {}
@override
void bindingsRepository() {
Get.lazyPut<ContactRepository>(() => Get.find<ContactRepositoryImpl>());
Get.lazyPut<AutoCompleteRepository>(() => Get.find<AutoCompleteRepositoryImpl>());
}
void bindingsRepository() {}
@override
void bindingsRepositoryImpl() {
Get.lazyPut(() => ContactRepositoryImpl(Get.find<ContactDataSource>()));
Get.lazyPut(() => AutoCompleteRepositoryImpl(_dataSources));
}
void bindingsRepositoryImpl() {}
}
@@ -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<ForwardController>();
late AccountId _accountId;
late Session? _session;
late GetAutoCompleteWithDeviceContactInteractor _getAutoCompleteWithDeviceContactInteractor;
late GetAutoCompleteInteractor _getAutoCompleteInteractor;
GetAutoCompleteWithDeviceContactInteractor? _getAutoCompleteWithDeviceContactInteractor;
GetAutoCompleteInteractor? _getAutoCompleteInteractor;
final listEmailForwards = RxSet<EmailAddress>();
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<GetAutoCompleteWithDeviceContactInteractor>();
_getAutoCompleteInteractor = Get.find<GetAutoCompleteInteractor>();
} catch (e) {
logError('EmailsForwardCreatorController::injectAutoCompleteBindings(): $e');
}
}
Future<List<EmailAddress>> getAutoCompleteSuggestion(
{required String word}) async {
Future<List<EmailAddress>> getAutoCompleteSuggestion(String word) async {
log('EmailsForwardCreatorController::getAutoCompleteSuggestion(): $word | $_contactSuggestionSource');
if(_session?.hasSupportTmailAutoComplete != true) return <EmailAddress>[];
_getAutoCompleteWithDeviceContactInteractor = Get.find<GetAutoCompleteWithDeviceContactInteractor>();
_getAutoCompleteInteractor = Get.find<GetAutoCompleteInteractor>();
try {
_getAutoCompleteWithDeviceContactInteractor = Get.find<GetAutoCompleteWithDeviceContactInteractor>();
_getAutoCompleteInteractor = Get.find<GetAutoCompleteInteractor>();
} 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) => <EmailAddress>[],
(success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : <EmailAddress>[]));
if (_getAutoCompleteWithDeviceContactInteractor == null || _getAutoCompleteInteractor == null) {
logError('EmailsForwardCreatorController::_getAutoCompleteWithDeviceContactInteractor(): is null');
return <EmailAddress>[];
} else {
return await _getAutoCompleteWithDeviceContactInteractor!
.execute(AutoCompletePattern(word: word, accountId: _accountId))
.then((value) => value.fold(
(failure) => <EmailAddress>[],
(success) => success is GetAutoCompleteSuccess
? success.listEmailAddress
: <EmailAddress>[]));
}
} else {
if (_getAutoCompleteInteractor == null) {
logError('EmailsForwardCreatorController::_getAutoCompleteInteractor(): is null');
return <EmailAddress>[];
} else {
return await _getAutoCompleteInteractor!
.execute(AutoCompletePattern(word: word, accountId: _accountId))
.then((value) => value.fold(
(failure) => <EmailAddress>[],
(success) => success is GetAutoCompleteSuccess
? success.listEmailAddress
: <EmailAddress>[]));
}
}
return await _getAutoCompleteInteractor
.execute(AutoCompletePattern(word: word, accountId: _accountId))
.then((value) => value.fold(
(failure) => <EmailAddress>[],
(success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : <EmailAddress>[]));
}
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<EmailAddress>.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();
}
}
}
@@ -139,12 +139,7 @@ class EmailsForwardCreatorView extends GetWidget<EmailsForwardCreatorController>
controller.clearAll();
}
})
..addOnSuggestionCallbackAction((pattern) {
if (pattern != null) {
controller.getAutoCompleteSuggestion(word: pattern.trim());
}
}))
.build(),
..addOnSuggestionCallbackAction(controller.getAutoCompleteSuggestion)).build(),
),
Expanded(
child: Obx(() => ListView.builder(
@@ -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 {