Store and get fcm cache by AccountId

(cherry picked from commit e327dff45a4fc67d0c5a2d2afe64fc4673e2eac1)
This commit is contained in:
dab246
2023-04-25 18:15:02 +07:00
committed by Dat Vu
parent eb27022a08
commit 550752a2a4
23 changed files with 130 additions and 107 deletions
@@ -1,16 +1,17 @@
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/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 {
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState);
Future<void> storeStateToRefresh(AccountId accountId, TypeName typeName, jmap.State newState);
Future<jmap.State> getStateToRefresh(TypeName typeName);
Future<jmap.State> getStateToRefresh(AccountId accountId, TypeName typeName);
Future<bool> deleteStateToRefresh(TypeName typeName);
Future<void> deleteStateToRefresh(AccountId accountId, TypeName typeName);
Future<void> storeSubscription(FCMSubscriptionCache fcmSubscriptionCache);
@@ -1,5 +1,6 @@
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/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';
@@ -15,23 +16,23 @@ class CacheFCMDatasourceImpl extends FCMDatasource {
CacheFCMDatasourceImpl(this._firebaseCacheManager, this._exceptionThrower);
@override
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
Future<void> storeStateToRefresh(AccountId accountId, TypeName typeName, jmap.State newState) {
return Future.sync(() async {
return await _firebaseCacheManager.storeStateToRefresh(typeName, newState);
return await _firebaseCacheManager.storeStateToRefresh(accountId, typeName, newState);
}).catchError(_exceptionThrower.throwException);
}
@override
Future<jmap.State> getStateToRefresh(TypeName typeName) {
Future<jmap.State> getStateToRefresh(AccountId accountId, TypeName typeName) {
return Future.sync(() async {
return await _firebaseCacheManager.getStateToRefresh(typeName);
return await _firebaseCacheManager.getStateToRefresh(accountId, typeName);
}).catchError(_exceptionThrower.throwException);
}
@override
Future<bool> deleteStateToRefresh(TypeName typeName) {
Future<void> deleteStateToRefresh(AccountId accountId, TypeName typeName) {
return Future.sync(() async {
return await _firebaseCacheManager.deleteStateToRefresh(typeName);
return await _firebaseCacheManager.deleteStateToRefresh(accountId, typeName);
}).catchError(_exceptionThrower.throwException);
}
@@ -1,5 +1,6 @@
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/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';
@@ -23,17 +24,17 @@ class FcmDatasourceImpl extends FCMDatasource {
}
@override
Future<bool> deleteStateToRefresh(TypeName typeName) {
Future<bool> deleteStateToRefresh(AccountId accountId, TypeName typeName) {
throw UnimplementedError();
}
@override
Future<jmap.State> getStateToRefresh(TypeName typeName) {
Future<jmap.State> getStateToRefresh(AccountId accountId, TypeName typeName) {
throw UnimplementedError();
}
@override
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
Future<bool> storeStateToRefresh(AccountId accountId, TypeName typeName, jmap.State newState) {
throw UnimplementedError();
}
@@ -1,27 +1,27 @@
import 'package:core/utils/app_logger.dart';
import 'package:fcm/model/type_name.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:tmail_ui_user/features/caching/fcm_cache_client.dart';
import 'package:tmail_ui_user/features/caching/subscription_cache_client.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.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 FcmCacheClient _fcmCacheClient;
final FCMSubscriptionCacheClient _fcmSubscriptionCacheClient;
FCMCacheManager(this._fcmCacheClient,this._fcmSubscriptionCacheClient);
FCMCacheManager(this._sharedPreferences,this._fcmSubscriptionCacheClient);
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
return _sharedPreferences.setString(typeName.value, newState.value);
Future<void> storeStateToRefresh(AccountId accountId, TypeName typeName, jmap.State newState) {
final stateKeyCache = TupleKey(typeName.value, accountId.asString).toString();
return _fcmCacheClient.insertItem(stateKeyCache, newState.value);
}
Future<jmap.State> getStateToRefresh(TypeName typeName) async {
log('FCMCacheManager::getStoredFcmStateChange():keys_BEFORE: ${_sharedPreferences.getKeys().toString()}');
await _sharedPreferences.reload();
log('FCMCacheManager::getStoredFcmStateChange():keys_AFTER: ${_sharedPreferences.getKeys().toString()}');
final stateValue = _sharedPreferences.getString(typeName.value);
Future<jmap.State> getStateToRefresh(AccountId accountId, TypeName typeName) async {
final stateKeyCache = TupleKey(typeName.value, accountId.asString).toString();
final stateValue = await _fcmCacheClient.getItem(stateKeyCache);
if (stateValue != null) {
return jmap.State(stateValue);
} else {
@@ -33,16 +33,13 @@ class FCMCacheManager {
}
}
Future<bool> deleteStateToRefresh(TypeName typeName) {
return _sharedPreferences.remove(typeName.value);
Future<void> deleteStateToRefresh(AccountId accountId, TypeName typeName) {
final stateKeyCache = TupleKey(typeName.value, accountId.asString).toString();
return _fcmCacheClient.deleteItem(stateKeyCache);
}
Future<bool> clearAllStateToRefresh() async {
return await Future.wait([
_sharedPreferences.remove(TypeName.emailType.value),
_sharedPreferences.remove(TypeName.mailboxType.value),
_sharedPreferences.remove(TypeName.emailDelivery.value),
]).then((listResult) => listResult.every((result) => result));
Future<void> clearAllStateToRefresh() async {
return _fcmCacheClient.clearAllData();
}
Future<void> storeSubscription(FCMSubscriptionCache fcmSubscriptionCache) {
@@ -75,18 +75,18 @@ class FCMRepositoryImpl extends FCMRepository {
}
@override
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
return _fcmDatasource[DataSourceType.local]!.storeStateToRefresh(typeName, newState);
Future<void> storeStateToRefresh(AccountId accountId, TypeName typeName, jmap.State newState) {
return _fcmDatasource[DataSourceType.local]!.storeStateToRefresh(accountId, typeName, newState);
}
@override
Future<jmap.State> getStateToRefresh(TypeName typeName) {
return _fcmDatasource[DataSourceType.local]!.getStateToRefresh(typeName);
Future<jmap.State> getStateToRefresh(AccountId accountId, TypeName typeName) {
return _fcmDatasource[DataSourceType.local]!.getStateToRefresh(accountId, typeName);
}
@override
Future<bool> deleteStateToRefresh(TypeName typeName) {
return _fcmDatasource[DataSourceType.local]!.deleteStateToRefresh(typeName);
Future<void> deleteStateToRefresh(AccountId accountId, TypeName typeName) {
return _fcmDatasource[DataSourceType.local]!.deleteStateToRefresh(accountId, typeName);
}
@override