From 50bba630d5cb5450114d508cfadf2d6241c03b09 Mon Sep 17 00:00:00 2001 From: dab246 Date: Tue, 25 Oct 2022 00:25:08 +0700 Subject: [PATCH] TF-1074 Handle go to path `/mailbox_creator` --- .../mixin/view_as_dialog_action_mixin.dart | 36 +++++++++++- .../presentation/mailbox_controller.dart | 39 +++++++++---- .../mailbox/presentation/mailbox_view.dart | 2 +- .../presentation/mailbox_view_web.dart | 2 +- .../mailbox_creator_bindings.dart | 8 ++- .../mailbox_creator_controller.dart | 56 +++++++++++++------ .../presentation/mailbox_creator_view.dart | 29 +++++++++- lib/main/pages/app_pages.dart | 10 ++-- lib/main/routes/app_routes.dart | 2 +- 9 files changed, 142 insertions(+), 42 deletions(-) diff --git a/lib/features/base/mixin/view_as_dialog_action_mixin.dart b/lib/features/base/mixin/view_as_dialog_action_mixin.dart index 44f053246..240eb0129 100644 --- a/lib/features/base/mixin/view_as_dialog_action_mixin.dart +++ b/lib/features/base/mixin/view_as_dialog_action_mixin.dart @@ -4,6 +4,10 @@ import 'package:model/mailbox/presentation_mailbox.dart'; import 'package:tmail_ui_user/features/destination_picker/presentation/destination_picker_bindings.dart'; import 'package:tmail_ui_user/features/destination_picker/presentation/destination_picker_view.dart'; import 'package:tmail_ui_user/features/destination_picker/presentation/model/destination_picker_arguments.dart'; +import 'package:tmail_ui_user/features/mailbox_creator/presentation/mailbox_creator_bindings.dart'; +import 'package:tmail_ui_user/features/mailbox_creator/presentation/mailbox_creator_view.dart'; +import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/mailbox_creator_arguments.dart'; +import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/new_mailbox_arguments.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; mixin ViewAsDialogActionMixin { @@ -19,7 +23,7 @@ mixin ViewAsDialogActionMixin { context: context, barrierDismissible: true, barrierLabel: '', - barrierColor: Colors.black54, + barrierColor: Colors.black.withAlpha(24), pageBuilder: (context, animation, secondaryAnimation) { return DestinationPickerView.fromArguments( arguments, @@ -37,4 +41,34 @@ mixin ViewAsDialogActionMixin { }); }); } + + void showDialogMailboxCreator({ + required BuildContext context, + required MailboxCreatorArguments arguments, + required Function(NewMailboxArguments) onCreatedMailbox + }) { + MailboxCreatorBindings().dependencies(); + + showGeneralDialog( + context: context, + barrierDismissible: true, + barrierLabel: '', + barrierColor: Colors.black.withAlpha(24), + pageBuilder: (context, animation, secondaryAnimation) { + return MailboxCreatorView.fromArguments( + arguments, + onDismissCallback: () { + MailboxCreatorBindings().dispose(); + popBack(); + }, + onCreatedMailboxCallback: (arguments) { + MailboxCreatorBindings().dispose(); + popBack(); + + if (arguments is NewMailboxArguments) { + onCreatedMailbox.call(arguments); + } + }); + }); + } } \ No newline at end of file diff --git a/lib/features/mailbox/presentation/mailbox_controller.dart b/lib/features/mailbox/presentation/mailbox_controller.dart index 19d7676a6..b79e872b3 100644 --- a/lib/features/mailbox/presentation/mailbox_controller.dart +++ b/lib/features/mailbox/presentation/mailbox_controller.dart @@ -380,20 +380,37 @@ class MailboxController extends BaseMailboxController { _openMailboxEventController.add(OpenMailboxViewEvent(context, presentationMailboxSelected)); } - void goToCreateNewMailboxView() async { + void goToCreateNewMailboxView(BuildContext context) async { final accountId = mailboxDashBoardController.accountId.value; if (accountId != null) { - final newMailboxArguments = await push( - AppRoutes.mailboxCreator, - arguments: MailboxCreatorArguments(accountId, defaultMailboxTree.value, folderMailboxTree.value) - ); + final arguments = MailboxCreatorArguments( + accountId, + defaultMailboxTree.value, + folderMailboxTree.value); - if (newMailboxArguments != null && newMailboxArguments is NewMailboxArguments) { - final generateCreateId = Id(_uuid.v1()); - _createNewMailboxAction(accountId, CreateNewMailboxRequest( - generateCreateId, - newMailboxArguments.newName, - parentId: newMailboxArguments.mailboxLocation?.id)); + if (BuildUtils.isWeb) { + showDialogMailboxCreator( + context: context, + arguments: arguments, + onCreatedMailbox: (newMailboxArguments) { + final generateCreateId = Id(_uuid.v1()); + _createNewMailboxAction(accountId, CreateNewMailboxRequest( + generateCreateId, + newMailboxArguments.newName, + parentId: newMailboxArguments.mailboxLocation?.id)); + }); + } else { + final newMailboxArguments = await push( + AppRoutes.mailboxCreator, + arguments: arguments); + + if (newMailboxArguments != null && newMailboxArguments is NewMailboxArguments) { + final generateCreateId = Id(_uuid.v1()); + _createNewMailboxAction(accountId, CreateNewMailboxRequest( + generateCreateId, + newMailboxArguments.newName, + parentId: newMailboxArguments.mailboxLocation?.id)); + } } } } diff --git a/lib/features/mailbox/presentation/mailbox_view.dart b/lib/features/mailbox/presentation/mailbox_view.dart index 9ae8f5e97..11f5efbef 100644 --- a/lib/features/mailbox/presentation/mailbox_view.dart +++ b/lib/features/mailbox/presentation/mailbox_view.dart @@ -147,7 +147,7 @@ class MailboxView extends GetWidget { return buildIconWeb( icon: SvgPicture.asset(_imagePaths.icAddNewFolder, width: 28, height: 24, color: AppColor.colorTextButton, fit: BoxFit.fill), tooltip: AppLocalizations.of(context).new_mailbox, - onTap: () => controller.goToCreateNewMailboxView()); + onTap: () => controller.goToCreateNewMailboxView(context)); } Widget _buildEditMailboxButton(BuildContext context, bool isSelectionEnabled) { diff --git a/lib/features/mailbox/presentation/mailbox_view_web.dart b/lib/features/mailbox/presentation/mailbox_view_web.dart index ef860e5cc..f427268bb 100644 --- a/lib/features/mailbox/presentation/mailbox_view_web.dart +++ b/lib/features/mailbox/presentation/mailbox_view_web.dart @@ -186,7 +186,7 @@ class MailboxView extends GetWidget with AppLoaderMixin, Popu splashRadius: 15, icon: SvgPicture.asset(_imagePaths.icAddNewFolder, color: AppColor.colorTextButton, fit: BoxFit.fill), tooltip: AppLocalizations.of(context).new_mailbox, - onTap: () => controller.goToCreateNewMailboxView())), + onTap: () => controller.goToCreateNewMailboxView(context))), ]), Obx(() => controller.folderMailboxHasChild ? _buildMailboxCategory(context, MailboxCategories.folders, controller.folderRootNode) diff --git a/lib/features/mailbox_creator/presentation/mailbox_creator_bindings.dart b/lib/features/mailbox_creator/presentation/mailbox_creator_bindings.dart index 2a3b36047..c7caed4d9 100644 --- a/lib/features/mailbox_creator/presentation/mailbox_creator_bindings.dart +++ b/lib/features/mailbox_creator/presentation/mailbox_creator_bindings.dart @@ -7,9 +7,7 @@ class MailboxCreatorBindings extends BaseBindings { @override void bindingsController() { - Get.lazyPut(() => MailboxCreatorController( - Get.find(), - )); + Get.lazyPut(() => MailboxCreatorController(Get.find())); } @override @@ -28,4 +26,8 @@ class MailboxCreatorBindings extends BaseBindings { @override void bindingsRepositoryImpl() {} + + void dispose() { + Get.delete(); + } } \ No newline at end of file diff --git a/lib/features/mailbox_creator/presentation/mailbox_creator_controller.dart b/lib/features/mailbox_creator/presentation/mailbox_creator_controller.dart index a28980a1d..dc9c2d172 100644 --- a/lib/features/mailbox_creator/presentation/mailbox_creator_controller.dart +++ b/lib/features/mailbox_creator/presentation/mailbox_creator_controller.dart @@ -20,6 +20,8 @@ import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/new_ma import 'package:tmail_ui_user/main/routes/app_routes.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; +typedef OnCreatedMailboxCallback = Function(NewMailboxArguments? arguments); + class MailboxCreatorController extends BaseController { final VerifyNameInteractor _verifyNameInteractor; @@ -27,27 +29,34 @@ class MailboxCreatorController extends BaseController { final selectedMailbox = Rxn(); final newNameMailbox = Rxn(); - final nameInputFocusNode = FocusNode(); + FocusNode? nameInputFocusNode; + MailboxCreatorArguments? arguments; AccountId? accountId; MailboxTree? folderMailboxTree; MailboxTree? defaultMailboxTree; + OnCreatedMailboxCallback? onCreatedMailboxCallback; + VoidCallback? onDismissMailboxCreator; + List listMailboxNameAsStringExist = []; - MailboxCreatorController( - this._verifyNameInteractor, - ); + MailboxCreatorController(this._verifyNameInteractor); void setNewNameMailbox(String newName) => newNameMailbox.value = newName; + @override + void onInit() { + super.onInit(); + nameInputFocusNode = FocusNode(); + } + @override void onReady() { super.onReady(); - final arguments = Get.arguments; - if (arguments is MailboxCreatorArguments) { - folderMailboxTree = arguments.folderMailboxTree; - defaultMailboxTree = arguments.defaultMailboxTree; - accountId = arguments.accountId; + if (arguments != null) { + folderMailboxTree = arguments!.folderMailboxTree; + defaultMailboxTree = arguments!.defaultMailboxTree; + accountId = arguments!.accountId; _createListMailboxNameAsStringInMailboxLocation(); } } @@ -61,14 +70,14 @@ class MailboxCreatorController extends BaseController { @override void onClose() { - nameInputFocusNode.dispose(); + nameInputFocusNode?.dispose(); super.onClose(); } bool isCreateMailboxValidated(BuildContext context) { final nameValidated = getErrorInputNameString(context); - if (!nameInputFocusNode.hasFocus && newNameMailbox.value == null) { + if (nameInputFocusNode?.hasFocus == false && newNameMailbox.value == null) { return false; } @@ -109,14 +118,12 @@ class MailboxCreatorController extends BaseController { listMailboxNameAsStringExist = []; } } - - log('MailboxCreatorController::_createListMailboxNameAsStringInMailboxLocation(): ${listMailboxNameAsStringExist.toString()}'); } String? getErrorInputNameString(BuildContext context) { final nameMailbox = newNameMailbox.value; - if (!nameInputFocusNode.hasFocus && nameMailbox == null) { + if (nameInputFocusNode?.hasFocus == false && nameMailbox == null) { return null; } @@ -178,17 +185,34 @@ class MailboxCreatorController extends BaseController { void createNewMailbox(BuildContext context) { FocusScope.of(context).unfocus(); + final nameMailbox = newNameMailbox.value; if (nameMailbox != null && nameMailbox.isNotEmpty) { final newMailboxArguments = NewMailboxArguments( MailboxName(nameMailbox), mailboxLocation: selectedMailbox.value); - popBack(result: newMailboxArguments); + + if (BuildUtils.isWeb) { + nameInputFocusNode?.dispose(); + nameInputFocusNode = null; + + onCreatedMailboxCallback?.call(newMailboxArguments); + } else { + popBack(result: newMailboxArguments); + } } } void closeMailboxCreator(BuildContext context) { FocusScope.of(context).unfocus(); - popBack(); + + if (BuildUtils.isWeb) { + nameInputFocusNode?.dispose(); + nameInputFocusNode = null; + + onDismissMailboxCreator?.call(); + } else { + popBack(); + } } } \ No newline at end of file diff --git a/lib/features/mailbox_creator/presentation/mailbox_creator_view.dart b/lib/features/mailbox_creator/presentation/mailbox_creator_view.dart index ab7500b23..07b27cdba 100644 --- a/lib/features/mailbox_creator/presentation/mailbox_creator_view.dart +++ b/lib/features/mailbox_creator/presentation/mailbox_creator_view.dart @@ -1,10 +1,16 @@ -import 'package:core/core.dart'; +import 'package:core/presentation/extensions/color_extension.dart'; +import 'package:core/presentation/resources/image_paths.dart'; +import 'package:core/presentation/utils/responsive_utils.dart'; +import 'package:core/presentation/utils/style_utils.dart'; +import 'package:core/presentation/views/text/text_field_builder.dart'; +import 'package:core/utils/build_utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:get/get.dart'; import 'package:pointer_interceptor/pointer_interceptor.dart'; import 'package:tmail_ui_user/features/mailbox/domain/extensions/presentation_mailbox_extension.dart'; import 'package:tmail_ui_user/features/mailbox_creator/presentation/mailbox_creator_controller.dart'; +import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/mailbox_creator_arguments.dart'; import 'package:tmail_ui_user/features/mailbox_creator/presentation/widgets/app_bar_mailbox_creator_builder.dart'; import 'package:tmail_ui_user/features/mailbox_creator/presentation/widgets/create_mailbox_name_input_decoration_builder.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; @@ -15,7 +21,24 @@ class MailboxCreatorView extends GetWidget { final _imagePaths = Get.find(); final _responsiveUtils = Get.find(); - MailboxCreatorView({Key? key}) : super(key: key); + @override + final controller = Get.find(); + + MailboxCreatorView({Key? key}) : super(key: key) { + controller.arguments = Get.arguments; + } + + MailboxCreatorView.fromArguments( + MailboxCreatorArguments arguments, { + Key? key, + OnCreatedMailboxCallback? onCreatedMailboxCallback, + VoidCallback? onDismissCallback + }) : super(key: key) { + controller.arguments = arguments; + controller.onCreatedMailboxCallback = onCreatedMailboxCallback; + controller.onDismissMailboxCreator = onDismissCallback; + controller.onInit(); + } @override Widget build(BuildContext context) { @@ -95,7 +118,7 @@ class MailboxCreatorView extends GetWidget { ..keyboardType(TextInputType.visiblePassword) ..cursorColor(AppColor.colorTextButton) ..maxLines(1) - ..textStyle(TextStyle( + ..textStyle(const TextStyle( color: AppColor.colorNameEmail, fontSize: 16, overflow: CommonTextStyle.defaultTextOverFlow)) diff --git a/lib/main/pages/app_pages.dart b/lib/main/pages/app_pages.dart index 1462e263e..410abde5c 100644 --- a/lib/main/pages/app_pages.dart +++ b/lib/main/pages/app_pages.dart @@ -42,6 +42,11 @@ class AppPages { opaque: false, page: () => DeferredWidget(destination_picker.loadLibrary, () => destination_picker.DestinationPickerView()), binding: DestinationPickerBindings()), + GetPage( + name: AppRoutes.mailboxCreator, + opaque: false, + page: () => DeferredWidget(mailbox_creator.loadLibrary, () => mailbox_creator.MailboxCreatorView()), + binding: MailboxCreatorBindings()), ]; static final pages = [ @@ -61,11 +66,6 @@ class AppPages { name: AppRoutes.dashboard, page: () => DeferredWidget(mailbox_dashboard.loadLibrary, () => mailbox_dashboard.MailboxDashBoardView()), binding: MailboxDashBoardBindings()), - GetPage( - name: AppRoutes.mailboxCreator, - opaque: false, - page: () => DeferredWidget(mailbox_creator.loadLibrary, () => mailbox_creator.MailboxCreatorView()), - binding: MailboxCreatorBindings()), GetPage( name: AppRoutes.settings, page: () => DeferredWidget(manage_account_dashboard.loadLibrary, diff --git a/lib/main/routes/app_routes.dart b/lib/main/routes/app_routes.dart index 88d894972..a15f0a98a 100644 --- a/lib/main/routes/app_routes.dart +++ b/lib/main/routes/app_routes.dart @@ -7,7 +7,7 @@ abstract class AppRoutes { static const composer = '/composer'; static const destinationPicker = '/destination_picker'; - static const mailboxCreator = '$dashboard/mailbox_creator'; + static const mailboxCreator = '/mailbox_creator'; static const contact = '$dashboard/contact'; static const identityCreator = '$settings/identity_creator'; static const rulesFilterCreator = '$settings/rules_filter_creator';