TF-1289: [Data] implement storeSubscription in fcm repository

This commit is contained in:
HuyNguyen
2022-12-29 15:37:15 +07:00
committed by Dat Vu
parent 097682fdcf
commit 97a37e36be
6 changed files with 105 additions and 18 deletions
@@ -2,6 +2,7 @@
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:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/register_new_token_request.dart';
abstract class FCMDatasource {
@@ -11,11 +12,9 @@ abstract class FCMDatasource {
Future<bool> deleteStateToRefresh(TypeName typeName);
Future<bool> storeDeviceId(String deviceId);
Future<void> storeSubscription(FCMSubscriptionCache fcmSubscriptionCache);
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId);
Future<FirebaseSubscription> registerNewToken(RegisterNewTokenRequest newTokenRequest);
Future<String> getDeviceId();
}
@@ -2,6 +2,7 @@ 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:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart';
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
import 'package:tmail_ui_user/features/push_notification/domain/extensions/firebase_subscription_extension.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/register_new_token_request.dart';
@@ -33,11 +34,6 @@ class FcmDatasourceImpl extends FCMDatasource {
throw UnimplementedError();
}
@override
Future<bool> storeDeviceId(String deviceId) {
throw UnimplementedError();
}
@override
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
throw UnimplementedError();
@@ -52,9 +48,9 @@ class FcmDatasourceImpl extends FCMDatasource {
_exceptionThrower.throwException(error);
});
}
@override
Future<String> getDeviceId() {
Future<bool> storeSubscription(FCMSubscriptionCache fcmSubscriptionCache) {
throw UnimplementedError();
}
}
@@ -0,0 +1,62 @@
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:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_manager.dart';
import 'package:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/register_new_token_request.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
class CacheFCMDatasourceImpl extends FCMDatasource {
final FCMCacheManager _firebaseCacheManager;
final ExceptionThrower _exceptionThrower;
CacheFCMDatasourceImpl(this._firebaseCacheManager, this._exceptionThrower);
@override
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
return Future.sync(() async {
return await _firebaseCacheManager.storeStateToRefresh(typeName, newState);
}).catchError((error) {
_exceptionThrower.throwException(error);
});
}
@override
Future<jmap.State> getStateToRefresh(TypeName typeName) {
return Future.sync(() async {
return await _firebaseCacheManager.getStateToRefresh(typeName);
}).catchError((error) {
_exceptionThrower.throwException(error);
});
}
@override
Future<bool> deleteStateToRefresh(TypeName typeName) {
return Future.sync(() async {
return await _firebaseCacheManager.deleteStateToRefresh(typeName);
}).catchError((error) {
_exceptionThrower.throwException(error);
});
}
@override
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId) {
throw UnimplementedError();
}
@override
Future<FirebaseSubscription> registerNewToken(RegisterNewTokenRequest newTokenRequest) {
throw UnimplementedError();
}
@override
Future<void> storeSubscription(FCMSubscriptionCache fcmSubscriptionCache) {
return Future.sync(() async {
return await _firebaseCacheManager.storeSubscription(fcmSubscriptionCache);
}).catchError((error) {
_exceptionThrower.throwException(error);
});
}
}
@@ -0,0 +1,10 @@
import 'package:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/fcm_subscription.dart';
extension FCMSubscriptionExtension on FCMSubscription {
FCMSubscriptionCache toFCMSubscriptionCache() {
return FCMSubscriptionCache(deviceId, subscriptionId);
}
}
@@ -0,0 +1,23 @@
import 'package:equatable/equatable.dart';
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
part 'fcm_subscription.g.dart';
@HiveType(typeId: CachingConstants.FCM_SUBSCRIPTION_HIVE_CACHE_INDENTITY)
class FCMSubscriptionCache extends HiveObject with EquatableMixin {
static const String keyCacheValue = 'fcmSubscriptionCache';
@HiveField(0)
final String deviceId;
@HiveField(1)
final String subscriptionId;
FCMSubscriptionCache(this.deviceId, this.subscriptionId);
@override
List<Object?> get props => [deviceId, subscriptionId];
}
@@ -4,6 +4,8 @@ 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';
import 'package:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/extensions/fcm_subscription_extensions.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/fcm_subscription.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/register_new_token_request.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource.dart';
@@ -74,11 +76,6 @@ class FCMRepositoryImpl extends FCMRepository {
return _fcmDatasource[DataSourceType.local]!.deleteStateToRefresh(typeName);
}
@override
Future<bool> storeDeviceId(String deviceId) {
return _fcmDatasource[DataSourceType.local]!.storeDeviceId(deviceId);
}
@override
Future<FirebaseSubscription> getFirebaseSubscriptionByDeviceId(String deviceId) {
return _fcmDatasource[DataSourceType.network]!.getFirebaseSubscriptionByDeviceId(deviceId);
@@ -88,9 +85,9 @@ class FCMRepositoryImpl extends FCMRepository {
Future<FirebaseSubscription> registerNewToken(RegisterNewTokenRequest newTokenRequest) {
return _fcmDatasource[DataSourceType.network]!.registerNewToken(newTokenRequest);
}
@override
Future<String> getDeviceId() {
return _fcmDatasource[DataSourceType.local]!.getDeviceId();
Future<void> storeSubscription(FCMSubscription fcmSubscription) {
return _fcmDatasource[DataSourceType.local]!.storeSubscription(fcmSubscription.toFCMSubscriptionCache());
}
}