From dd6079385d7f44edb982a3f51d5baeba986ce1e2 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Thu, 29 Dec 2022 15:49:41 +0700 Subject: [PATCH] TF-1289: [Data] implement storeSubscription in fcm datasource --- .../caching/config/hive_cache_config.dart | 5 ++ .../caching/subscription_cache_client.dart | 9 +++ .../caching/utils/caching_constants.dart | 1 + .../cache_fcm_datasource_impl.dart | 62 +++++++++++++++++++ .../data/local/fcm_cache_manager.dart | 21 +++---- 5 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 lib/features/caching/subscription_cache_client.dart create mode 100644 lib/features/push_notification/data/datasource_impl/cache_fcm_datasource_impl.dart diff --git a/lib/features/caching/config/hive_cache_config.dart b/lib/features/caching/config/hive_cache_config.dart index fa964c9c9..fdcd51123 100644 --- a/lib/features/caching/config/hive_cache_config.dart +++ b/lib/features/caching/config/hive_cache_config.dart @@ -19,6 +19,7 @@ import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_rights_cache.d import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/data/model/recent_search_cache.dart'; +import 'package:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart'; import 'package:tmail_ui_user/features/thread/data/model/email_address_hive_cache.dart'; import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart'; @@ -118,6 +119,10 @@ class HiveCacheConfig { RecentLoginUsernameCacheAdapter(), CachingConstants.RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY ); + registerCacheAdapter( + FCMSubscriptionCacheAdapter(), + CachingConstants.FCM_SUBSCRIPTION_HIVE_CACHE_INDENTITY + ); } void registerCacheAdapter(TypeAdapter typeAdapter, int typeId) { diff --git a/lib/features/caching/subscription_cache_client.dart b/lib/features/caching/subscription_cache_client.dart new file mode 100644 index 000000000..62b76ae3a --- /dev/null +++ b/lib/features/caching/subscription_cache_client.dart @@ -0,0 +1,9 @@ + +import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart'; +import 'package:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart'; + +class FCMSubscriptionCacheClient extends HiveCacheClient { + + @override + String get tableName => 'FCMSubscriptionCache'; +} \ No newline at end of file diff --git a/lib/features/caching/utils/caching_constants.dart b/lib/features/caching/utils/caching_constants.dart index ed2f0008e..9235ddcc6 100644 --- a/lib/features/caching/utils/caching_constants.dart +++ b/lib/features/caching/utils/caching_constants.dart @@ -13,4 +13,5 @@ class CachingConstants { static const int AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY = 11; static const int RECENT_LOGIN_URL_HIVE_CACHE_IDENTITY = 12; static const int RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY = 13; + static const int FCM_SUBSCRIPTION_HIVE_CACHE_INDENTITY = 14; } \ No newline at end of file diff --git a/lib/features/push_notification/data/datasource_impl/cache_fcm_datasource_impl.dart b/lib/features/push_notification/data/datasource_impl/cache_fcm_datasource_impl.dart new file mode 100644 index 000000000..4ae4b5c79 --- /dev/null +++ b/lib/features/push_notification/data/datasource_impl/cache_fcm_datasource_impl.dart @@ -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 storeStateToRefresh(TypeName typeName, jmap.State newState) { + return Future.sync(() async { + return await _firebaseCacheManager.storeStateToRefresh(typeName, newState); + }).catchError((error) { + _exceptionThrower.throwException(error); + }); + } + + @override + Future getStateToRefresh(TypeName typeName) { + return Future.sync(() async { + return await _firebaseCacheManager.getStateToRefresh(typeName); + }).catchError((error) { + _exceptionThrower.throwException(error); + }); + } + + @override + Future deleteStateToRefresh(TypeName typeName) { + return Future.sync(() async { + return await _firebaseCacheManager.deleteStateToRefresh(typeName); + }).catchError((error) { + _exceptionThrower.throwException(error); + }); + } + + @override + Future getFirebaseSubscriptionByDeviceId(String deviceId) { + throw UnimplementedError(); + } + + @override + Future registerNewToken(RegisterNewTokenRequest newTokenRequest) { + throw UnimplementedError(); + } + + @override + Future storeSubscription(FCMSubscriptionCache fcmSubscriptionCache) { + return Future.sync(() async { + return await _firebaseCacheManager.storeSubscription(fcmSubscriptionCache); + }).catchError((error) { + _exceptionThrower.throwException(error); + }); + } +} \ No newline at end of file diff --git a/lib/features/push_notification/data/local/fcm_cache_manager.dart b/lib/features/push_notification/data/local/fcm_cache_manager.dart index 0f7cbc9ed..7037005b1 100644 --- a/lib/features/push_notification/data/local/fcm_cache_manager.dart +++ b/lib/features/push_notification/data/local/fcm_cache_manager.dart @@ -1,15 +1,17 @@ import 'package:core/utils/app_logger.dart'; import 'package:fcm/model/type_name.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:tmail_ui_user/features/caching/subscription_cache_client.dart'; +import 'package:tmail_ui_user/features/push_notification/data/model/fcm_subscription.dart'; import 'package:tmail_ui_user/features/push_notification/domain/exceptions/fcm_exception.dart'; import 'package:jmap_dart_client/jmap/core/state.dart' as jmap; class FCMCacheManager { final SharedPreferences _sharedPreferences; + final FCMSubscriptionCacheClient _fcmSubscriptionCacheClient; - static const String fcmDeviceIdKey = 'FCM_DEVICE_ID'; - FCMCacheManager(this._sharedPreferences); + FCMCacheManager(this._sharedPreferences,this._fcmSubscriptionCacheClient); Future storeStateToRefresh(TypeName typeName, jmap.State newState) { return _sharedPreferences.setString(typeName.value, newState.value); @@ -43,17 +45,8 @@ class FCMCacheManager { ]).then((listResult) => listResult.every((result) => result)); } - Future storeDeviceId(String deviceId) { - return _sharedPreferences.setString(fcmDeviceIdKey, deviceId); - } - - Future getDeviceId() async { - await _sharedPreferences.reload(); - final deviceId = _sharedPreferences.getString(fcmDeviceIdKey); - if (deviceId != null) { - return deviceId; - } else { - throw NotFoundDeviceIdException(); - } + Future storeSubscription(FCMSubscriptionCache fcmSubscriptionCache) { + return _fcmSubscriptionCacheClient.insertItem( + FCMSubscriptionCache.keyCacheValue, fcmSubscriptionCache); } } \ No newline at end of file