TF-3157 Implement web socket push
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
|
||||
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';
|
||||
|
||||
abstract class FcmBaseController {
|
||||
|
||||
void consumeState(Stream<Either<Failure, Success>> newStateStream) {
|
||||
newStateStream.listen(
|
||||
_handleStateStream,
|
||||
onError: (error, stackTrace) {
|
||||
logError('FcmBaseController::consumeState():onError:error: $error | stackTrace: $stackTrace');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void _handleStateStream(Either<Failure, Success> newState) {
|
||||
newState.fold(handleFailureViewState, handleSuccessViewState);
|
||||
}
|
||||
|
||||
void handleFailureViewState(Failure failure);
|
||||
|
||||
void handleSuccessViewState(Success success);
|
||||
}
|
||||
+16
-91
@@ -1,21 +1,17 @@
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:core/data/network/config/dynamic_url_interceptors.dart';
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/platform_info.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:jmap_dart_client/jmap/push/state_change.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:tmail_ui_user/features/base/action/ui_action.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
|
||||
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
|
||||
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
|
||||
@@ -30,10 +26,9 @@ import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_
|
||||
import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_account_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/action/fcm_action.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_token_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/push_base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/extensions/state_change_extension.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/mailbox_change_listener.dart';
|
||||
@@ -42,12 +37,7 @@ import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_
|
||||
import 'package:tmail_ui_user/main/bindings/main_bindings.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
|
||||
class FcmMessageController extends FcmBaseController {
|
||||
|
||||
AccountId? _currentAccountId;
|
||||
Session? _currentSession;
|
||||
UserName? _userName;
|
||||
|
||||
class FcmMessageController extends PushBaseController {
|
||||
GetAuthenticatedAccountInteractor? _getAuthenticatedAccountInteractor;
|
||||
DynamicUrlInterceptors? _dynamicUrlInterceptors;
|
||||
AuthorizationInterceptors? _authorizationInterceptors;
|
||||
@@ -64,10 +54,9 @@ class FcmMessageController extends FcmBaseController {
|
||||
|
||||
static FcmMessageController get instance => _instance;
|
||||
|
||||
@override
|
||||
void initialize({AccountId? accountId, Session? session}) {
|
||||
_currentAccountId = accountId;
|
||||
_currentSession = session;
|
||||
_userName = session?.username;
|
||||
super.initialize(accountId: accountId, session: session);
|
||||
|
||||
_listenTokenStream();
|
||||
_listenForegroundMessageStream();
|
||||
@@ -96,15 +85,17 @@ class FcmMessageController extends FcmBaseController {
|
||||
}
|
||||
|
||||
void _handleForegroundMessageAction(Map<String, dynamic> payloadData) {
|
||||
log('FcmMessageController::_handleForegroundMessageAction():payloadData: $payloadData | _currentAccountId: $_currentAccountId');
|
||||
if (_currentAccountId != null && _userName != null) {
|
||||
log('FcmMessageController::_handleForegroundMessageAction():payloadData: $payloadData | accountId: $accountId');
|
||||
if (accountId != null && session?.username != null) {
|
||||
final stateChange = FcmUtils.instance.convertFirebaseDataMessageToStateChange(payloadData);
|
||||
final mapTypeState = stateChange.getMapTypeState(_currentAccountId!);
|
||||
_mappingTypeStateToAction(
|
||||
final mapTypeState = stateChange.getMapTypeState(accountId!);
|
||||
mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
_currentAccountId!,
|
||||
_userName!,
|
||||
session: _currentSession);
|
||||
accountId!,
|
||||
emailChangeListener: EmailChangeListener.instance,
|
||||
mailboxChangeListener: MailboxChangeListener.instance,
|
||||
session!.username,
|
||||
session: session);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,74 +106,6 @@ class FcmMessageController extends FcmBaseController {
|
||||
_getAuthenticatedAccount(stateChange: stateChange);
|
||||
}
|
||||
|
||||
void _mappingTypeStateToAction(
|
||||
Map<String, dynamic> mapTypeState,
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
bool isForeground = true,
|
||||
Session? session
|
||||
}) {
|
||||
log('FcmMessageController::_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) => toFcmAction(typeName, accountId, userName, mapTypeState, isForeground, session: session))
|
||||
.whereNotNull()
|
||||
.toList();
|
||||
|
||||
log('FcmMessageController::_mappingTypeStateToAction():listEmailActions: $listEmailActions');
|
||||
|
||||
if (listEmailActions.isNotEmpty) {
|
||||
EmailChangeListener.instance.dispatchActions(listEmailActions);
|
||||
}
|
||||
|
||||
final listMailboxActions = listTypeName
|
||||
.where((typeName) => typeName == TypeName.mailboxType)
|
||||
.map((typeName) => toFcmAction(typeName, accountId, userName, mapTypeState, isForeground))
|
||||
.whereNotNull()
|
||||
.toList();
|
||||
|
||||
log('FcmMessageController::_mappingTypeStateToAction():listMailboxActions: $listEmailActions');
|
||||
|
||||
if (listMailboxActions.isNotEmpty) {
|
||||
MailboxChangeListener.instance.dispatchActions(listMailboxActions);
|
||||
}
|
||||
}
|
||||
|
||||
FcmAction? toFcmAction(
|
||||
TypeName typeName,
|
||||
AccountId accountId,
|
||||
UserName userName,
|
||||
Map<String, dynamic> mapTypeState,
|
||||
isForeground,
|
||||
{
|
||||
Session? session
|
||||
}
|
||||
) {
|
||||
final newState = jmap.State(mapTypeState[typeName.value]);
|
||||
if (typeName == TypeName.emailType) {
|
||||
if (isForeground) {
|
||||
return SynchronizeEmailOnForegroundAction(typeName, newState, accountId, session);
|
||||
} else {
|
||||
return StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session);
|
||||
}
|
||||
} else if (typeName == TypeName.emailDelivery) {
|
||||
if (!isForeground) {
|
||||
return PushNotificationAction(typeName, newState, session, accountId, userName);
|
||||
}
|
||||
} else if (typeName == TypeName.mailboxType) {
|
||||
if (isForeground) {
|
||||
return SynchronizeMailboxOnForegroundAction(typeName, newState, accountId);
|
||||
} else {
|
||||
return StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> _initialAppConfig() async {
|
||||
await Future.wait([
|
||||
MainBindings().dependencies(),
|
||||
@@ -312,10 +235,12 @@ class FcmMessageController extends FcmBaseController {
|
||||
}) {
|
||||
final mapTypeState = stateChange.getMapTypeState(accountId);
|
||||
|
||||
_mappingTypeStateToAction(
|
||||
mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
accountId,
|
||||
userName,
|
||||
emailChangeListener: EmailChangeListener.instance,
|
||||
mailboxChangeListener: MailboxChangeListener.instance,
|
||||
isForeground: false,
|
||||
session: session);
|
||||
}
|
||||
|
||||
@@ -20,12 +20,12 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_sto
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/register_new_firebase_registration_token_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_firebase_registration_interator.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/update_firebase_registration_token_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/push_base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class FcmTokenController extends FcmBaseController {
|
||||
class FcmTokenController extends PushBaseController {
|
||||
|
||||
FcmTokenController._internal();
|
||||
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import 'package:collection/collection.dart';
|
||||
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/mailbox_change_listener.dart';
|
||||
|
||||
abstract class PushBaseController {
|
||||
Session? session;
|
||||
AccountId? accountId;
|
||||
|
||||
void consumeState(Stream<Either<Failure, Success>> newStateStream) {
|
||||
newStateStream.listen(
|
||||
_handleStateStream,
|
||||
onError: (error, stackTrace) {
|
||||
logError('PushBaseController::consumeState():onError:error: $error | stackTrace: $stackTrace');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void _handleStateStream(Either<Failure, Success> newState) {
|
||||
newState.fold(handleFailureViewState, handleSuccessViewState);
|
||||
}
|
||||
|
||||
void handleFailureViewState(Failure failure);
|
||||
|
||||
void handleSuccessViewState(Success success);
|
||||
|
||||
void initialize({AccountId? accountId, Session? session}) {
|
||||
this.accountId = accountId;
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
void mappingTypeStateToAction(
|
||||
Map<String, dynamic> mapTypeState,
|
||||
AccountId accountId,
|
||||
UserName userName, {
|
||||
bool isForeground = true,
|
||||
Session? session,
|
||||
required EmailChangeListener emailChangeListener,
|
||||
required MailboxChangeListener mailboxChangeListener
|
||||
}) {
|
||||
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))
|
||||
.whereNotNull()
|
||||
.toList();
|
||||
|
||||
log('PushBaseController::mappingTypeStateToAction():listEmailActions: $listEmailActions');
|
||||
|
||||
if (listEmailActions.isNotEmpty) {
|
||||
emailChangeListener.dispatchActions(listEmailActions);
|
||||
}
|
||||
|
||||
final listMailboxActions = listTypeName
|
||||
.where((typeName) => typeName == TypeName.mailboxType)
|
||||
.map((typeName) => _toPushNotificationAction(typeName, accountId, userName, mapTypeState, isForeground))
|
||||
.whereNotNull()
|
||||
.toList();
|
||||
|
||||
log('PushBaseController::mappingTypeStateToAction():listMailboxActions: $listEmailActions');
|
||||
|
||||
if (listMailboxActions.isNotEmpty) {
|
||||
mailboxChangeListener.dispatchActions(listMailboxActions);
|
||||
}
|
||||
}
|
||||
|
||||
PushNotificationStateChangeAction? _toPushNotificationAction(
|
||||
TypeName typeName,
|
||||
AccountId accountId,
|
||||
UserName userName,
|
||||
Map<String, dynamic> mapTypeState,
|
||||
isForeground,
|
||||
{Session? session}
|
||||
) {
|
||||
final newState = jmap.State(mapTypeState[typeName.value]);
|
||||
if (typeName == TypeName.emailType) {
|
||||
if (isForeground) {
|
||||
return SynchronizeEmailOnForegroundAction(typeName, newState, accountId, session);
|
||||
} else {
|
||||
return StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session);
|
||||
}
|
||||
} else if (typeName == TypeName.emailDelivery) {
|
||||
if (!isForeground) {
|
||||
return PushNotificationAction(typeName, newState, session, accountId, userName);
|
||||
}
|
||||
} else if (typeName == TypeName.mailboxType) {
|
||||
if (isForeground) {
|
||||
return SynchronizeMailboxOnForegroundAction(typeName, newState, accountId);
|
||||
} else {
|
||||
return StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/state/web_socket_push_state.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/connect_web_socket_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/push_base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/extensions/state_change_extension.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/mailbox_change_listener.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
|
||||
class WebSocketController extends PushBaseController {
|
||||
WebSocketController._internal();
|
||||
|
||||
static final WebSocketController _instance = WebSocketController._internal();
|
||||
|
||||
static WebSocketController get instance => _instance;
|
||||
|
||||
ConnectWebSocketInteractor? _connectWebSocketInteractor;
|
||||
|
||||
@override
|
||||
void handleFailureViewState(Failure failure) {
|
||||
logError('WebSocketController::handleFailureViewState():Failure $failure');
|
||||
}
|
||||
|
||||
@override
|
||||
void handleSuccessViewState(Success success) {
|
||||
log('WebSocketController::handleSuccessViewState():Success $success');
|
||||
if (success is WebSocketPushStateReceived) {
|
||||
_handleWebSocketPushStateReceived(success);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initialize({AccountId? accountId, Session? session}) {
|
||||
super.initialize(accountId: accountId, session: session);
|
||||
|
||||
_connectWebSocket(accountId, session);
|
||||
}
|
||||
|
||||
void _connectWebSocket(AccountId? accountId, Session? session) {
|
||||
_connectWebSocketInteractor = getBinding<ConnectWebSocketInteractor>();
|
||||
if (_connectWebSocketInteractor == null || accountId == null || session == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
consumeState(_connectWebSocketInteractor!.execute(session, accountId));
|
||||
}
|
||||
|
||||
void _handleWebSocketPushStateReceived(WebSocketPushStateReceived success) {
|
||||
log('WebSocketController::_handleWebSocketPushStateReceived(): $success');
|
||||
if (accountId == null || session == null) return;
|
||||
final stateChange = success.stateChange;
|
||||
final mapTypeState = stateChange.getMapTypeState(accountId!);
|
||||
mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
accountId!,
|
||||
emailChangeListener: EmailChangeListener.instance,
|
||||
mailboxChangeListener: MailboxChangeListener.instance,
|
||||
session!.username,
|
||||
session: session);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user