import 'dart:async'; import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:fcm/model/type_name.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/core/state.dart' as jmap; import 'package:jmap_dart_client/jmap/core/user_name.dart'; import 'package:tmail_ui_user/features/base/action/ui_action.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/action/push_notification_state_change_action.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/listener/email_change_listener.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/listener/label_change_listener.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/listener/mailbox_change_listener.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/model/state_change_action_roles.dart'; abstract class PushBaseController { Session? session; AccountId? accountId; void consumeState(Stream> newStateStream) { newStateStream.listen( _handleStateStream, onError: handleErrorViewState, ); } void _handleStateStream(Either newState) { newState.fold(handleFailureViewState, handleSuccessViewState); } void handleFailureViewState(Failure failure); void handleSuccessViewState(Success success); void handleErrorViewState(Object error, StackTrace stackTrace) { logWarning('PushBaseController::handleErrorViewState():error: $error | stackTrace: $stackTrace'); } void initialize({AccountId? accountId, Session? session}) { this.accountId = accountId; this.session = session; } void onClose() {} void mappingTypeStateToAction( Map mapTypeState, AccountId accountId, UserName userName, { bool isForeground = true, Session? session, StateChangeActionRoles roles = StateChangeActionRoles.all, required EmailChangeListener emailChangeListener, required MailboxChangeListener mailboxChangeListener, required LabelChangeListener labelChangeListener, }) { log('PushBaseController::mappingTypeStateToAction():mapTypeState: $mapTypeState'); final listTypeName = mapTypeState.keys .map((value) => TypeName(value)) .toList(); final listEmailActions = listTypeName .where((typeName) => typeName == TypeName.emailType || typeName == TypeName.emailDelivery) .map((typeName) => _toPushNotificationAction( typeName, accountId, userName, mapTypeState, isForeground, session: session, roles: roles, )) .nonNulls .toList(); log('PushBaseController::mappingTypeStateToAction():listEmailActions: $listEmailActions'); if (listEmailActions.isNotEmpty) { emailChangeListener.dispatchActions(listEmailActions); } if (roles.refreshView || !isForeground) { final listMailboxActions = listTypeName .where((typeName) => typeName == TypeName.mailboxType) .map((typeName) => _toPushNotificationAction(typeName, accountId, userName, mapTypeState, isForeground)) .nonNulls .toList(); log('PushBaseController::mappingTypeStateToAction():listMailboxActions: $listMailboxActions'); if (listMailboxActions.isNotEmpty) { mailboxChangeListener.dispatchActions(listMailboxActions); } final labelActions = listTypeName .where((typeName) => typeName == TypeName.labelType) .map((typeName) => _toPushNotificationAction( typeName, accountId, userName, mapTypeState, isForeground, )) .nonNulls .toList(); log('PushBaseController::mappingTypeStateToAction():LabelActions: $labelActions'); if (labelActions.isNotEmpty) { labelChangeListener.dispatchActions(labelActions); } } } PushNotificationStateChangeAction? _toPushNotificationAction( TypeName typeName, AccountId accountId, UserName userName, Map mapTypeState, isForeground, { Session? session, StateChangeActionRoles roles = StateChangeActionRoles.all, }) { final newState = jmap.State(mapTypeState[typeName.value]); if (typeName == TypeName.emailType || typeName == TypeName.emailDelivery) { return _toEmailPushAction( typeName, newState, accountId, userName, isForeground, session, roles, ); } switch (typeName) { case TypeName.mailboxType: return isForeground ? SynchronizeMailboxOnForegroundAction(typeName, newState, accountId) : StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName); case TypeName.labelType: return isForeground ? SynchronizeLabelOnForegroundAction(typeName, newState, accountId) : StoreLabelStateToRefreshAction( typeName, newState, accountId, userName, ); default: return null; } } PushNotificationStateChangeAction? _toEmailPushAction( TypeName typeName, jmap.State newState, AccountId accountId, UserName userName, bool isForeground, Session? session, StateChangeActionRoles roles, ) { if (typeName == TypeName.emailDelivery) { if (!isForeground && roles.updateDeliveryAndNotifications) { return PushNotificationAction( typeName, newState, session, accountId, userName, storeOfflineBodies: roles.storeOfflineBodies, ); } return null; } if (!isForeground) { return StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session); } if (!roles.hasForegroundEmailWork) return null; return SynchronizeEmailOnForegroundAction( typeName, newState, accountId, session, refreshView: roles.refreshView, storeOfflineBodies: roles.storeOfflineBodies, updateDeliveryAndNotifications: roles.updateDeliveryAndNotifications, ); } }