TF-439 Implement handle store device id
This commit is contained in:
@@ -26,6 +26,7 @@ import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_a
|
||||
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_token_handler.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/services/fcm_service.dart';
|
||||
@@ -54,6 +55,7 @@ class FcmController extends BaseController {
|
||||
|
||||
void initialize({AccountId? accountId}) {
|
||||
_currentAccountId = accountId;
|
||||
FcmTokenHandler.instance.initialize();
|
||||
}
|
||||
|
||||
void _listenFcmMessageStream() {
|
||||
@@ -65,6 +67,8 @@ class FcmController extends BaseController {
|
||||
FcmService.instance.backgroundMessageStream
|
||||
.throttleTime(const Duration(milliseconds: FcmService.durationMessageComing))
|
||||
.listen(_handleBackgroundMessageAction);
|
||||
|
||||
FcmService.instance.fcmTokenStream.listen(FcmTokenHandler.instance.handle);
|
||||
}
|
||||
|
||||
void _handleForegroundMessageAction(RemoteMessage newRemoteMessage) {
|
||||
@@ -151,6 +155,7 @@ class FcmController extends BaseController {
|
||||
_dynamicUrlInterceptors = Get.find<DynamicUrlInterceptors>();
|
||||
_authorizationInterceptors = Get.find<AuthorizationInterceptors>();
|
||||
_getSessionInteractor = Get.find<GetSessionInteractor>();
|
||||
FcmTokenHandler.instance.initialize();
|
||||
} catch (e) {
|
||||
logError('FcmController::_getBindings(): ${e.toString()}');
|
||||
}
|
||||
|
||||
@@ -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:dartz/dartz.dart';
|
||||
import 'package:fcm/model/firebase_token.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_device_id_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
|
||||
|
||||
class FcmTokenHandler {
|
||||
|
||||
FcmTokenHandler._internal();
|
||||
|
||||
static final FcmTokenHandler _instance = FcmTokenHandler._internal();
|
||||
|
||||
static FcmTokenHandler get instance => _instance;
|
||||
|
||||
StoreDeviceIdInteractor? _storeDeviceIdInteractor;
|
||||
|
||||
void initialize() {
|
||||
try {
|
||||
_storeDeviceIdInteractor = Get.find<StoreDeviceIdInteractor>();
|
||||
} catch (e) {
|
||||
logError('FcmTokenHandler::initialize(): ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
void handle(String token) {
|
||||
log('FcmTokenHandler::handle():token: $token');
|
||||
final fcmToken = FirebaseToken(token);
|
||||
final deviceId = FcmUtils.instance.hashTokenToDeviceId(token);
|
||||
log('FcmTokenHandler::handle(): fcmToken: $fcmToken');
|
||||
log('FcmTokenHandler::handle(): deviceId: $deviceId');
|
||||
_storeDeviceIdAction(deviceId);
|
||||
}
|
||||
|
||||
void _storeDeviceIdAction(String deviceId) {
|
||||
if (_storeDeviceIdInteractor != null) {
|
||||
_consumeState(_storeDeviceIdInteractor!.execute(deviceId));
|
||||
}
|
||||
}
|
||||
|
||||
void _consumeState(Stream<Either<Failure, Success>> newStateStream) {
|
||||
newStateStream.listen(
|
||||
_handleStateStream,
|
||||
onError: (error, stackTrace) {
|
||||
logError('FcmTokenHandler::consumeState():onError:error: $error');
|
||||
logError('FcmTokenHandler::consumeState():onError:stackTrace: $stackTrace');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void _handleStateStream(Either<Failure, Success> newState) {
|
||||
newState.fold(_handleFailureViewState, _handleSuccessViewState);
|
||||
}
|
||||
|
||||
void _handleFailureViewState(Failure failure) {
|
||||
log('FcmTokenHandler::_handleFailureViewState(): $failure');
|
||||
}
|
||||
|
||||
void _handleSuccessViewState(Success success) {
|
||||
log('FcmTokenHandler::_handleSuccessViewState(): $success');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user