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 48ee32249..91d9b6392 100644 --- a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart +++ b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart @@ -2015,12 +2015,16 @@ class MailboxDashBoardController extends ReloadableController downloadController.deleteDownloadTask(taskId); } - void disableVacationResponder() { + void disableVacationResponder(VacationResponse vacationResponse) { if (accountId.value != null && _updateVacationInteractor != null) { - final vacationDisabled = vacationResponse.value != null - ? vacationResponse.value!.copyWith(isEnabled: false) - : VacationResponse(isEnabled: false); - consumeState(_updateVacationInteractor!.execute(accountId.value!, vacationDisabled)); + consumeState(_updateVacationInteractor!.execute( + accountId.value!, + vacationResponse.clearAllExceptHtmlBody(), + )); + } else { + consumeState( + Stream.value(Left(UpdateVacationFailure(ParametersIsNullException()))), + ); } } diff --git a/lib/features/mailbox_dashboard/presentation/mailbox_dashboard_view_web.dart b/lib/features/mailbox_dashboard/presentation/mailbox_dashboard_view_web.dart index 847d3cbff..47ebbf151 100644 --- a/lib/features/mailbox_dashboard/presentation/mailbox_dashboard_view_web.dart +++ b/lib/features/mailbox_dashboard/presentation/mailbox_dashboard_view_web.dart @@ -617,12 +617,14 @@ class MailboxDashBoardView extends BaseMailboxDashBoardView { Widget _buildVacationNotificationMessage(BuildContext context) { return Obx(() { - if (controller.vacationResponse.value?.vacationResponderIsValid == true) { + final vacation = controller.vacationResponse.value; + if (vacation?.vacationResponderIsValid == true) { return VacationNotificationMessageWidget( margin: VacationNotificationMessageWidgetStyle.bannerMargin, - vacationResponse: controller.vacationResponse.value!, + vacationResponse: vacation!, actionGotoVacationSetting: controller.goToVacationSetting, - actionEndNow: controller.disableVacationResponder); + actionEndNow: controller.disableVacationResponder, + ); } else { return const SizedBox.shrink(); } diff --git a/lib/features/manage_account/presentation/extensions/handle_vacation_response_extension.dart b/lib/features/manage_account/presentation/extensions/handle_vacation_response_extension.dart new file mode 100644 index 000000000..0e0b5ea91 --- /dev/null +++ b/lib/features/manage_account/presentation/extensions/handle_vacation_response_extension.dart @@ -0,0 +1,21 @@ +import 'package:jmap_dart_client/jmap/mail/vacation/vacation_response.dart'; +import 'package:tmail_ui_user/features/manage_account/presentation/extensions/vacation_response_extension.dart'; +import 'package:tmail_ui_user/features/manage_account/presentation/manage_account_dashboard_controller.dart'; + +extension HandleVacationResponseExtension on ManageAccountDashBoardController { + void syncVacationResponse(VacationResponse? newVacation) { + if (newVacation?.vacationResponderIsStopped == true) { + automaticallyDeactivateVacation(newVacation!); + } else { + setUpVacation(newVacation); + } + } + + void setUpVacation(VacationResponse? newVacation) { + vacationResponse.value = newVacation; + } + + void automaticallyDeactivateVacation(VacationResponse vacationResponse) { + disableVacationResponder(vacationResponse); + } +} diff --git a/lib/features/manage_account/presentation/extensions/vacation_response_extension.dart b/lib/features/manage_account/presentation/extensions/vacation_response_extension.dart index 7a1e0c2d0..8ab5c7abe 100644 --- a/lib/features/manage_account/presentation/extensions/vacation_response_extension.dart +++ b/lib/features/manage_account/presentation/extensions/vacation_response_extension.dart @@ -87,6 +87,10 @@ extension VacationResponseExtension on VacationResponse { htmlBody: htmlBody ?? this.htmlBody ); } + + VacationResponse clearAllExceptHtmlBody() { + return VacationResponse(isEnabled: false, htmlBody: htmlBody); + } String getNotificationMessage(BuildContext context) { if (vacationResponderIsValid) { 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 33a44f2d1..71cea8c51 100644 --- a/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart +++ b/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart @@ -1,4 +1,3 @@ - import 'package:back_button_interceptor/back_button_interceptor.dart'; import 'package:core/core.dart'; import 'package:dartz/dartz.dart'; @@ -18,6 +17,7 @@ import 'package:tmail_ui_user/features/base/mixin/own_email_address_mixin.dart'; import 'package:tmail_ui_user/features/base/reloadable/reloadable_controller.dart'; import 'package:tmail_ui_user/features/base/widget/dialog_picker/color_dialog_picker.dart'; import 'package:tmail_ui_user/features/base/widget/dialog_picker/date_time_dialog_picker.dart'; +import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.dart'; import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart'; import 'package:tmail_ui_user/features/manage_account/domain/state/export_trace_log_state.dart'; import 'package:tmail_ui_user/features/manage_account/domain/state/get_all_vacation_state.dart'; @@ -27,6 +27,7 @@ import 'package:tmail_ui_user/features/manage_account/domain/usecases/update_vac import 'package:tmail_ui_user/features/manage_account/presentation/action/dashboard_setting_action.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/email_rules/bindings/email_rules_bindings.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/extensions/export_trace_log_extension.dart'; +import 'package:tmail_ui_user/features/manage_account/presentation/extensions/handle_vacation_response_extension.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/extensions/vacation_response_extension.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/forward/bindings/forward_bindings.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/identities/identity_bindings.dart'; @@ -80,9 +81,7 @@ class ManageAccountDashBoardController extends ReloadableController @override void handleSuccessViewState(Success success) { if (success is GetAllVacationSuccess) { - if (success.listVacationResponse.isNotEmpty) { - vacationResponse.value = success.listVacationResponse.first; - } + syncVacationResponse(success.listVacationResponse.firstOrNull); } else if (success is UpdateVacationSuccess) { _handleUpdateVacationSuccess(success); } else if (success is ExportTraceLogSuccess) { @@ -96,6 +95,8 @@ class ManageAccountDashBoardController extends ReloadableController void handleFailureViewState(Failure failure) { if (failure is ExportTraceLogFailure) { handleExportTraceLogFailure(failure); + } else if (failure is UpdateVacationFailure) { + setUpVacation(null); } else { super.handleFailureViewState(failure); } @@ -178,10 +179,6 @@ class ManageAccountDashBoardController extends ReloadableController } } - void updateVacationResponse(VacationResponse? newVacation) { - vacationResponse.value = newVacation; - } - void selectAccountMenuItem(AccountMenuItem newAccountMenuItem) { settingsPageLevel.value = newAccountMenuItem == AccountMenuItem.none ? SettingsPageLevel.universal @@ -292,25 +289,28 @@ class ManageAccountDashBoardController extends ReloadableController } } - void disableVacationResponder() { + void disableVacationResponder(VacationResponse vacation) { if (accountId.value != null && _updateVacationInteractor != null) { - final vacationDisabled = vacationResponse.value != null - ? vacationResponse.value!.copyWith(isEnabled: false) - : VacationResponse(isEnabled: false); - consumeState(_updateVacationInteractor!.execute(accountId.value!, vacationDisabled)); + consumeState(_updateVacationInteractor!.execute( + accountId.value!, + vacation.clearAllExceptHtmlBody(), + )); + } else { + consumeState( + Stream.value(Left(UpdateVacationFailure(ParametersIsNullException()))), + ); } } void _handleUpdateVacationSuccess(UpdateVacationSuccess success) { - if (success.listVacationResponse.isNotEmpty) { - if (currentContext != null && currentOverlayContext != null) { - appToast.showToastSuccessMessage( - currentOverlayContext!, - AppLocalizations.of(currentContext!).yourVacationResponderIsDisabledSuccessfully); - } - vacationResponse.value = success.listVacationResponse.first; - log('ManageAccountDashBoardController::_handleUpdateVacationSuccess(): $vacationResponse'); + if (success.listVacationResponse.isNotEmpty && + currentContext != null && + currentOverlayContext != null) { + appToast.showToastSuccessMessage( + currentOverlayContext!, + AppLocalizations.of(currentContext!).yourVacationResponderIsDisabledSuccessfully); } + setUpVacation(success.listVacationResponse.firstOrNull); } bool inVacationSettings() { diff --git a/lib/features/manage_account/presentation/manage_account_dashboard_view.dart b/lib/features/manage_account/presentation/manage_account_dashboard_view.dart index 9304af60b..78a99b7a3 100644 --- a/lib/features/manage_account/presentation/manage_account_dashboard_view.dart +++ b/lib/features/manage_account/presentation/manage_account_dashboard_view.dart @@ -29,9 +29,10 @@ class ManageAccountDashBoardView extends GetWidget controller.selectAccountMenuItem(AccountMenuItem.vacation) : null, - actionEndNow: controller.disableVacationResponder); - } else if ((controller.vacationResponse.value?.vacationResponderIsWaiting == true - || controller.vacationResponse.value?.vacationResponderIsStopped == true) - && controller.accountMenuItemSelected.value == AccountMenuItem.vacation) { + actionEndNow: controller.disableVacationResponder, + ); + } else if (vacation?.vacationResponderIsWaiting == true && + controller.inVacationSettings()) { return VacationNotificationMessageWidget( margin: EdgeInsetsDirectional.only( - start: controller.responsiveUtils.isWebDesktop(context) ? 0 : 16, + start: isWebDesktop ? 0 : 16, end: 16, - top: controller.responsiveUtils.isWebDesktop(context) ? 16 : 0, - bottom: controller.responsiveUtils.isWebDesktop(context) ? 0 : 16, + top: isWebDesktop ? 16 : 0, + bottom: isWebDesktop ? 0 : 16, ), fromAccountDashBoard: true, - vacationResponse: controller.vacationResponse.value!, + vacationResponse: vacation!, padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16), leadingIcon: const Padding( padding: EdgeInsetsDirectional.only(end: 12), diff --git a/lib/features/manage_account/presentation/menu/settings/settings_view.dart b/lib/features/manage_account/presentation/menu/settings/settings_view.dart index 00925bca7..eb024426f 100644 --- a/lib/features/manage_account/presentation/menu/settings/settings_view.dart +++ b/lib/features/manage_account/presentation/menu/settings/settings_view.dart @@ -40,33 +40,36 @@ class SettingsView extends GetWidget { controller.showExportTraceLogConfirmDialog(context), )), Obx(() { - if (controller.manageAccountDashboardController.vacationResponse.value?.vacationResponderIsValid == true) { + final dashboard = controller.manageAccountDashboardController; + final vacation = dashboard.vacationResponse.value; + final isWebDesktop = controller.responsiveUtils.isWebDesktop(context); + + if (vacation?.vacationResponderIsValid == true) { return VacationNotificationMessageWidget( margin: EdgeInsetsDirectional.only( start: 12, end: 12, - top: controller.responsiveUtils.isWebDesktop(context) ? 8 : 0, - bottom: controller.responsiveUtils.isWebDesktop(context) ? 0 : 16, + top: isWebDesktop ? 8 : 0, + bottom: isWebDesktop ? 0 : 16, ), fromAccountDashBoard: true, - vacationResponse: controller.manageAccountDashboardController.vacationResponse.value!, - actionGotoVacationSetting: !controller.manageAccountDashboardController.inVacationSettings() - ? () => controller.manageAccountDashboardController.selectAccountMenuItem(AccountMenuItem.vacation) + vacationResponse: vacation!, + actionGotoVacationSetting: !dashboard.inVacationSettings() + ? () => dashboard.selectAccountMenuItem(AccountMenuItem.vacation) : null, - actionEndNow: controller.manageAccountDashboardController.disableVacationResponder + actionEndNow: dashboard.disableVacationResponder ); - } else if ((controller.manageAccountDashboardController.vacationResponse.value?.vacationResponderIsWaiting == true - || controller.manageAccountDashboardController.vacationResponse.value?.vacationResponderIsStopped == true) - && controller.manageAccountDashboardController.inVacationSettings()) { + } else if (vacation?.vacationResponderIsWaiting == true && + dashboard.inVacationSettings()) { return VacationNotificationMessageWidget( margin: EdgeInsetsDirectional.only( start: 12, end: 12, - top: controller.responsiveUtils.isWebDesktop(context) ? 8 : 0, - bottom: controller.responsiveUtils.isWebDesktop(context) ? 0 : 16, + top: isWebDesktop ? 8 : 0, + bottom: isWebDesktop ? 0 : 16, ), fromAccountDashBoard: true, - vacationResponse: controller.manageAccountDashboardController.vacationResponse.value!, + vacationResponse: vacation!, padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16), leadingIcon: const Padding( padding: EdgeInsetsDirectional.only(end: 12), diff --git a/lib/features/manage_account/presentation/vacation/vacation_controller.dart b/lib/features/manage_account/presentation/vacation/vacation_controller.dart index 0c221fb4d..941f61a55 100644 --- a/lib/features/manage_account/presentation/vacation/vacation_controller.dart +++ b/lib/features/manage_account/presentation/vacation/vacation_controller.dart @@ -8,8 +8,10 @@ import 'package:rich_text_composer/views/commons/constants.dart'; import 'package:tmail_ui_user/features/base/base_controller.dart'; import 'package:tmail_ui_user/features/base/widget/dialog_picker/date_time_dialog_picker.dart'; import 'package:tmail_ui_user/features/composer/presentation/controller/rich_text_web_controller.dart'; +import 'package:tmail_ui_user/features/mailbox/domain/exceptions/null_session_or_accountid_exception.dart'; import 'package:tmail_ui_user/features/manage_account/domain/state/update_vacation_state.dart'; import 'package:tmail_ui_user/features/manage_account/domain/usecases/update_vacation_interactor.dart'; +import 'package:tmail_ui_user/features/manage_account/presentation/extensions/handle_vacation_response_extension.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/extensions/vacation_response_extension.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/manage_account_dashboard_controller.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/menu/settings/settings_controller.dart'; @@ -223,7 +225,7 @@ class VacationController extends BaseController { _updateVacationAction(newVacationResponse); } else { final vacationDisabled = currentVacation != null - ? currentVacation!.copyWith(isEnabled: false) + ? currentVacation!.clearAllExceptHtmlBody() : VacationResponse(isEnabled: false); log('VacationController::saveVacation(): vacationDisabled: $vacationDisabled'); _updateVacationAction(vacationDisabled); @@ -234,26 +236,28 @@ class VacationController extends BaseController { final accountId = _accountDashBoardController.accountId.value; if (accountId != null) { consumeState(_updateVacationInteractor.execute(accountId, vacationResponse)); + } else { + consumeState( + Stream.value(Left(UpdateVacationFailure(NullSessionOrAccountIdException()))), + ); } } void _handleUpdateVacationSuccess(UpdateVacationSuccess success) { - if (success.listVacationResponse.isNotEmpty) { - if (currentOverlayContext != null && currentContext != null) { - appToast.showToastSuccessMessage( - currentOverlayContext!, - AppLocalizations.of(currentContext!).vacationSettingSaved); - } - currentVacation = success.listVacationResponse.first; - log('VacationController::_handleUpdateVacationSuccess(): $currentVacation'); - - if (currentVacation != null) { - final newVacationPresentation = currentVacation!.toVacationPresentation(); - _initializeValueForVacation(newVacationPresentation); - } - - _accountDashBoardController.updateVacationResponse(currentVacation); + if (success.listVacationResponse.isNotEmpty && + currentOverlayContext != null && + currentContext != null) { + appToast.showToastSuccessMessage( + currentOverlayContext!, + AppLocalizations.of(currentContext!).vacationSettingSaved); } + + currentVacation = success.listVacationResponse.firstOrNull; + if (currentVacation != null) { + final newVacationPresentation = currentVacation!.toVacationPresentation(); + _initializeValueForVacation(newVacationPresentation); + } + _accountDashBoardController.setUpVacation(currentVacation); } void updateMessageHtmlText(String? text) => _vacationMessageHtmlText = text; diff --git a/lib/features/manage_account/presentation/vacation/widgets/vacation_notification_message_widget.dart b/lib/features/manage_account/presentation/vacation/widgets/vacation_notification_message_widget.dart index bbc041b37..05b837c71 100644 --- a/lib/features/manage_account/presentation/vacation/widgets/vacation_notification_message_widget.dart +++ b/lib/features/manage_account/presentation/vacation/widgets/vacation_notification_message_widget.dart @@ -6,7 +6,7 @@ import 'package:jmap_dart_client/jmap/mail/vacation/vacation_response.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/extensions/vacation_response_extension.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; -typedef EndNowVacationSettingAction = Function(); +typedef EndNowVacationSettingAction = Function(VacationResponse vacation); typedef GoToVacationSettingAction = Function(); class VacationNotificationMessageWidget extends StatelessWidget { @@ -93,7 +93,8 @@ class VacationNotificationMessageWidget extends StatelessWidget { maxWidth: 180, maxLines: 1, tooltipMessage: AppLocalizations.of(context).endNow, - onTapActionCallback: actionEndNow), + onTapActionCallback: () => actionEndNow?.call(vacationResponse), + ), if (actionGotoVacationSetting != null) TMailButtonWidget.fromText( text: AppLocalizations.of(context).vacationSetting, @@ -148,7 +149,7 @@ class VacationNotificationMessageWidget extends StatelessWidget { maxWidth: 180, maxLines: 1, tooltipMessage: AppLocalizations.of(context).endNow, - onTapActionCallback: actionEndNow + onTapActionCallback: () => actionEndNow?.call(vacationResponse), ), ), if (actionGotoVacationSetting != null)