TF-439 Implement get firebase subscription
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import 'package:fcm/model/firebase_subscription.dart';
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
|
||||
import 'package:model/fcm/fcm_token_dto.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
|
||||
|
||||
class FcmDatasourceImpl extends FCMDatasource {
|
||||
|
||||
final FcmApi _fcmApi;
|
||||
final ExceptionThrower _exceptionThrower;
|
||||
|
||||
FcmDatasourceImpl(this._fcmApi, this._exceptionThrower);
|
||||
|
||||
@override
|
||||
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId) {
|
||||
return Future.sync(() async {
|
||||
return await _fcmApi.getFirebaseSubscriptionByDeviceId(deviceId);
|
||||
}).catchError((error) {
|
||||
_exceptionThrower.throwException(error);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteFCMToken(String accountId) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> deleteStateToRefresh(TypeName typeName) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FCMTokenDto> getFCMToken(String accountId) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<jmap.State> getStateToRefresh(TypeName typeName) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setFCMToken(FCMTokenDto fcmToken) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> storeDeviceId(String deviceId) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:fcm/model/firebase_subscription.dart';
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
|
||||
import 'package:model/fcm/fcm_token_dto.dart';
|
||||
@@ -74,4 +75,9 @@ class HiveFCMDatasourceImpl extends FCMDatasource {
|
||||
_exceptionThrower.throwException(error);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
import 'package:fcm/method/get/firebase_subscription_get_method.dart';
|
||||
import 'package:fcm/method/get/firebase_subscription_get_response.dart';
|
||||
import 'package:fcm/model/firebase_subscription.dart';
|
||||
import 'package:jmap_dart_client/http/http_client.dart';
|
||||
import 'package:jmap_dart_client/jmap/jmap_request.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/fcm_exception.dart';
|
||||
|
||||
class FcmApi {
|
||||
|
||||
final HttpClient _httpClient;
|
||||
|
||||
FcmApi(this._httpClient);
|
||||
|
||||
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId) async {
|
||||
final processingInvocation = ProcessingInvocation();
|
||||
final requestBuilder = JmapRequestBuilder(_httpClient, processingInvocation);
|
||||
|
||||
final firebaseSubscriptionGetMethod = FirebaseSubscriptionGetMethod();
|
||||
final firebaseSubscriptionGetInvocation = requestBuilder.invocation(firebaseSubscriptionGetMethod);
|
||||
final response = await (requestBuilder
|
||||
..usings(firebaseSubscriptionGetMethod.requiredCapabilities))
|
||||
.build()
|
||||
.execute();
|
||||
|
||||
final getResponse = response.parse<FirebaseSubscriptionGetResponse>(
|
||||
firebaseSubscriptionGetInvocation.methodCallId,
|
||||
FirebaseSubscriptionGetResponse.deserialize);
|
||||
|
||||
final matchedFirebaseSubscription = getResponse?.list
|
||||
.where((fcmSub) => fcmSub.deviceClientId?.value == deviceId)
|
||||
.toList();
|
||||
|
||||
if (matchedFirebaseSubscription?.isNotEmpty == true) {
|
||||
return matchedFirebaseSubscription!.first;
|
||||
} else {
|
||||
throw NotFoundFirebaseSubscriptionException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/data/model/source_type/data_source_type.dart';
|
||||
import 'package:fcm/model/firebase_subscription.dart';
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
|
||||
@@ -12,7 +13,7 @@ import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
||||
|
||||
class FCMRepositoryImpl extends FCMRepository {
|
||||
|
||||
final FCMDatasource _fcmDatasource;
|
||||
final Map<DataSourceType, FCMDatasource> _fcmDatasource;
|
||||
final ThreadDataSource _threadDataSource;
|
||||
|
||||
FCMRepositoryImpl(
|
||||
@@ -22,18 +23,17 @@ class FCMRepositoryImpl extends FCMRepository {
|
||||
|
||||
@override
|
||||
Future<FCMTokenDto> getFCMToken(String accountId) {
|
||||
return _fcmDatasource.getFCMToken(accountId);
|
||||
return _fcmDatasource[DataSourceType.local]!.getFCMToken(accountId);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setFCMToken(FCMTokenDto fcmTokenDto) {
|
||||
log('FCMRepositoryImpl::setFCMToken(): $fcmTokenDto');
|
||||
return _fcmDatasource.setFCMToken(fcmTokenDto);
|
||||
return _fcmDatasource[DataSourceType.local]!.setFCMToken(fcmTokenDto);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteFCMToken(String accountId) {
|
||||
return _fcmDatasource.deleteFCMToken(accountId);
|
||||
return _fcmDatasource[DataSourceType.local]!.deleteFCMToken(accountId);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -76,21 +76,26 @@ class FCMRepositoryImpl extends FCMRepository {
|
||||
|
||||
@override
|
||||
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
|
||||
return _fcmDatasource.storeStateToRefresh(typeName, newState);
|
||||
return _fcmDatasource[DataSourceType.local]!.storeStateToRefresh(typeName, newState);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<jmap.State> getStateToRefresh(TypeName typeName) {
|
||||
return _fcmDatasource.getStateToRefresh(typeName);
|
||||
return _fcmDatasource[DataSourceType.local]!.getStateToRefresh(typeName);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> deleteStateToRefresh(TypeName typeName) {
|
||||
return _fcmDatasource.deleteStateToRefresh(typeName);
|
||||
return _fcmDatasource[DataSourceType.local]!.deleteStateToRefresh(typeName);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> storeDeviceId(String deviceId) {
|
||||
return _fcmDatasource.storeDeviceId(deviceId);
|
||||
return _fcmDatasource[DataSourceType.local]!.storeDeviceId(deviceId);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId) {
|
||||
return _fcmDatasource[DataSourceType.network]!.getFirebaseSubscriptionByDeviceId(deviceId);
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,6 @@ class NotSupportFCMException implements Exception {}
|
||||
|
||||
class NotFoundStateToRefreshException implements Exception {}
|
||||
|
||||
class NotFoundEmailDeliveryStateException implements Exception {}
|
||||
class NotFoundEmailDeliveryStateException implements Exception {}
|
||||
|
||||
class NotFoundFirebaseSubscriptionException implements Exception {}
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:fcm/model/firebase_subscription.dart';
|
||||
|
||||
class GetFirebaseSubscriptionLoading extends UIState {}
|
||||
|
||||
class GetFirebaseSubscriptionSuccess extends UIState {
|
||||
|
||||
final FirebaseSubscription firebaseSubscription;
|
||||
|
||||
GetFirebaseSubscriptionSuccess(this.firebaseSubscription);
|
||||
|
||||
@override
|
||||
List<Object> get props => [firebaseSubscription];
|
||||
}
|
||||
|
||||
class GetFirebaseSubscriptionFailure extends FeatureFailure {
|
||||
final dynamic exception;
|
||||
|
||||
GetFirebaseSubscriptionFailure(this.exception);
|
||||
|
||||
@override
|
||||
List<Object> get props => [exception];
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/state/get_firebase_subscription_state.dart';
|
||||
|
||||
class GetFirebaseSubscriptionInteractor {
|
||||
|
||||
final FCMRepository _fcmRepository;
|
||||
|
||||
GetFirebaseSubscriptionInteractor(this._fcmRepository);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(String deviceId) async* {
|
||||
try {
|
||||
yield Right<Failure, Success>(GetFirebaseSubscriptionLoading());
|
||||
final firebaseSubscription = await _fcmRepository.getFirebaseSubscriptionByDeviceId(deviceId);
|
||||
yield Right<Failure, Success>(GetFirebaseSubscriptionSuccess(firebaseSubscription));
|
||||
} catch (e) {
|
||||
yield Left<Failure, Success>(GetFirebaseSubscriptionFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
import 'package:core/data/model/source_type/data_source_type.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tmail_ui_user/features/base/interactors_bindings.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/fcm_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/hive_fcm_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/repository/fcm_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_email_state_to_refresh_interactor.dart';
|
||||
@@ -10,6 +13,7 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_changes_to_push_notification_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_state_to_refresh_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_fcm_token_cache_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_firebase_subscription_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_stored_email_delivery_state_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/save_fcm_token_cache_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_device_id_interactor.dart';
|
||||
@@ -26,7 +30,7 @@ class FcmInteractorBindings extends InteractorsBindings {
|
||||
|
||||
@override
|
||||
void bindingsDataSource() {
|
||||
Get.lazyPut<FCMDatasource>(() => Get.find<HiveFCMDatasourceImpl>());
|
||||
Get.lazyPut<FCMDatasource>(() => Get.find<FcmDatasourceImpl>());
|
||||
Get.lazyPut<ThreadDataSource>(() => Get.find<ThreadDataSourceImpl>());
|
||||
}
|
||||
|
||||
@@ -36,6 +40,10 @@ class FcmInteractorBindings extends InteractorsBindings {
|
||||
Get.find<FCMCacheManager>(),
|
||||
Get.find<CacheExceptionThrower>(),
|
||||
));
|
||||
Get.lazyPut(() => FcmDatasourceImpl(
|
||||
Get.find<FcmApi>(),
|
||||
Get.find<RemoteExceptionThrower>(),
|
||||
));
|
||||
Get.lazyPut(() => ThreadDataSourceImpl(
|
||||
Get.find<ThreadAPI>(),
|
||||
Get.find<ThreadIsolateWorker>(),
|
||||
@@ -55,6 +63,7 @@ class FcmInteractorBindings extends InteractorsBindings {
|
||||
Get.lazyPut(() => GetEmailStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
|
||||
Get.lazyPut(() => DeleteEmailStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
|
||||
Get.lazyPut(() => StoreDeviceIdInteractor(Get.find<FCMRepositoryImpl>()));
|
||||
Get.lazyPut(() => GetFirebaseSubscriptionInteractor(Get.find<FCMRepositoryImpl>()));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -65,7 +74,10 @@ class FcmInteractorBindings extends InteractorsBindings {
|
||||
@override
|
||||
void bindingsRepositoryImpl() {
|
||||
Get.lazyPut(() => FCMRepositoryImpl(
|
||||
Get.find<FCMDatasource>(),
|
||||
{
|
||||
DataSourceType.local: Get.find<HiveFCMDatasourceImpl>(),
|
||||
DataSourceType.network: Get.find<FCMDatasource>(),
|
||||
},
|
||||
Get.find<ThreadDataSource>()
|
||||
));
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ 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/state/get_firebase_subscription_state.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_firebase_subscription_interactor.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';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
|
||||
class FcmTokenHandler {
|
||||
|
||||
@@ -17,22 +19,35 @@ class FcmTokenHandler {
|
||||
static FcmTokenHandler get instance => _instance;
|
||||
|
||||
StoreDeviceIdInteractor? _storeDeviceIdInteractor;
|
||||
GetFirebaseSubscriptionInteractor? _getFirebaseSubscriptionInteractor;
|
||||
|
||||
FirebaseToken? _fcmToken;
|
||||
String? _deviceId;
|
||||
|
||||
void initialize() {
|
||||
try {
|
||||
_storeDeviceIdInteractor = Get.find<StoreDeviceIdInteractor>();
|
||||
_storeDeviceIdInteractor = getBinding<StoreDeviceIdInteractor>();
|
||||
_getFirebaseSubscriptionInteractor = getBinding<GetFirebaseSubscriptionInteractor>();
|
||||
} catch (e) {
|
||||
logError('FcmTokenHandler::initialize(): ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
void handle(String token) {
|
||||
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);
|
||||
if (token != null) {
|
||||
_fcmToken = FirebaseToken(token);
|
||||
_deviceId = FcmUtils.instance.hashTokenToDeviceId(token);
|
||||
log('FcmTokenHandler::handle(): fcmToken: $_fcmToken');
|
||||
log('FcmTokenHandler::handle(): deviceId: $_deviceId');
|
||||
_getFcmTokenFromBackend(_deviceId!);
|
||||
}
|
||||
}
|
||||
|
||||
void _getFcmTokenFromBackend(String deviceId) {
|
||||
if (_getFirebaseSubscriptionInteractor != null) {
|
||||
_consumeState(_getFirebaseSubscriptionInteractor!.execute(deviceId));
|
||||
}
|
||||
}
|
||||
|
||||
void _storeDeviceIdAction(String deviceId) {
|
||||
@@ -57,9 +72,16 @@ class FcmTokenHandler {
|
||||
|
||||
void _handleFailureViewState(Failure failure) {
|
||||
log('FcmTokenHandler::_handleFailureViewState(): $failure');
|
||||
if (failure is GetFirebaseSubscriptionFailure) {
|
||||
// Register new token
|
||||
}
|
||||
}
|
||||
|
||||
void _handleSuccessViewState(Success success) {
|
||||
log('FcmTokenHandler::_handleSuccessViewState(): $success');
|
||||
if (success is GetFirebaseSubscriptionSuccess) {
|
||||
final token = success.firebaseSubscription.token;
|
||||
log('FcmTokenHandler::_handleSuccessViewState():token: $token');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,9 +28,7 @@ class FcmReceiver {
|
||||
void getFcmToken() async {
|
||||
final token = await FirebaseMessaging.instance.getToken();
|
||||
log('FcmReceiver::onFcmToken():token: $token');
|
||||
if (token?.isNotEmpty == true) {
|
||||
FcmService.instance.handleRefreshToken(token!);
|
||||
}
|
||||
FcmService.instance.handleRefreshToken(token);
|
||||
}
|
||||
|
||||
void onRefreshFcmToken() {
|
||||
|
||||
@@ -15,8 +15,8 @@ class FcmService {
|
||||
final StreamController<RemoteMessage> backgroundMessageStreamController = StreamController<RemoteMessage>.broadcast();
|
||||
Stream<RemoteMessage> get backgroundMessageStream => backgroundMessageStreamController.stream;
|
||||
|
||||
final StreamController<String> fcmTokenStreamController = StreamController<String>.broadcast();
|
||||
Stream<String> get fcmTokenStream => fcmTokenStreamController.stream;
|
||||
final StreamController<String?> fcmTokenStreamController = StreamController<String?>.broadcast();
|
||||
Stream<String?> get fcmTokenStream => fcmTokenStreamController.stream;
|
||||
|
||||
FcmService._internal();
|
||||
|
||||
@@ -35,13 +35,14 @@ class FcmService {
|
||||
backgroundMessageStreamController.add(newRemoteMessage);
|
||||
}
|
||||
|
||||
void handleRefreshToken(String newToken) {
|
||||
void handleRefreshToken(String? newToken) {
|
||||
fcmTokenStreamController.add(newToken);
|
||||
}
|
||||
|
||||
void _closeStream() {
|
||||
foregroundMessageStreamController.close();
|
||||
backgroundMessageStreamController.close();
|
||||
fcmTokenStreamController.close();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
|
||||
@@ -21,6 +21,7 @@ import 'package:tmail_ui_user/features/manage_account/data/network/forwarding_ap
|
||||
import 'package:tmail_ui_user/features/manage_account/data/network/identity_api.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/data/network/rule_filter_api.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/data/network/vacation_api.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
|
||||
import 'package:tmail_ui_user/features/quotas/data/network/quotas_api.dart';
|
||||
import 'package:tmail_ui_user/features/session/data/network/session_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
@@ -88,6 +89,7 @@ class NetworkBindings extends Bindings {
|
||||
Get.put(MdnAPI(Get.find<JmapHttpClient.HttpClient>()));
|
||||
Get.put(ForwardingAPI(Get.find<JmapHttpClient.HttpClient>()));
|
||||
Get.put(QuotasAPI(Get.find<JmapHttpClient.HttpClient>()));
|
||||
Get.put(FcmApi(Get.find<JmapHttpClient.HttpClient>()));
|
||||
}
|
||||
|
||||
void _bindingConnection() {
|
||||
|
||||
@@ -24,4 +24,12 @@ bool canBack(BuildContext context) {
|
||||
|
||||
BuildContext? get currentContext => Get.context;
|
||||
|
||||
BuildContext? get currentOverlayContext => Get.overlayContext;
|
||||
BuildContext? get currentOverlayContext => Get.overlayContext;
|
||||
|
||||
T? getBinding<T>() {
|
||||
if (Get.isRegistered<T>()) {
|
||||
return Get.find<T>();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user