TF-438 Implement method for get/store/delete StateToRefresh and get email changes
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
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/local/fcm_cache_manager.dart';
|
||||
@@ -36,4 +38,31 @@ class HiveFCMDatasourceImpl extends FCMDatasource {
|
||||
_exceptionThrower.throwException(error);
|
||||
});
|
||||
}
|
||||
|
||||
@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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
import 'package:model/fcm/fcm_token_dto.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tmail_ui_user/features/caching/fcm_token_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/extensions/fcm_cache_extensions.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/extensions/fcm_extensions.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 FcmTokenCacheClient _fcmTokenCacheClient;
|
||||
final SharedPreferences _sharedPreferences;
|
||||
|
||||
FCMCacheManager(this._fcmTokenCacheClient);
|
||||
FCMCacheManager(this._fcmTokenCacheClient, this._sharedPreferences);
|
||||
|
||||
Future<FCMTokenDto> getFCMToken(String accountId) async {
|
||||
try {
|
||||
@@ -33,4 +37,36 @@ class FCMCacheManager {
|
||||
log('FCMCacheManager::deleteSelectedFCM(): $accountId');
|
||||
return _fcmTokenCacheClient.deleteItem(accountId);
|
||||
}
|
||||
|
||||
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
|
||||
return _sharedPreferences.setString(typeName.value, 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);
|
||||
if (stateValue != null) {
|
||||
return jmap.State(stateValue);
|
||||
} else {
|
||||
if (typeName == TypeName.emailDelivery) {
|
||||
throw NotFoundEmailDeliveryStateException();
|
||||
} else {
|
||||
throw NotFoundStateToRefreshException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> deleteStateToRefresh(TypeName typeName) {
|
||||
return _sharedPreferences.remove(typeName.value);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,24 @@
|
||||
import 'package:core/utils/app_logger.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';
|
||||
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/domain/repository/fcm_repository.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
|
||||
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
||||
|
||||
class FCMRepositoryImpl extends FCMRepository {
|
||||
|
||||
final FCMDatasource _fcmDatasource;
|
||||
final ThreadDataSource _threadDataSource;
|
||||
|
||||
FCMRepositoryImpl(this._fcmDatasource);
|
||||
FCMRepositoryImpl(
|
||||
this._fcmDatasource,
|
||||
this._threadDataSource
|
||||
);
|
||||
|
||||
@override
|
||||
Future<FCMTokenDto> getFCMToken(String accountId) {
|
||||
@@ -24,4 +35,57 @@ class FCMRepositoryImpl extends FCMRepository {
|
||||
Future<void> deleteFCMToken(String accountId) {
|
||||
return _fcmDatasource.deleteFCMToken(accountId);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<EmailsResponse> getEmailChangesToPushNotification(
|
||||
AccountId accountId,
|
||||
jmap.State currentState,
|
||||
{
|
||||
Properties? propertiesCreated,
|
||||
Properties? propertiesUpdated
|
||||
}
|
||||
) async {
|
||||
EmailChangeResponse? emailChangeResponse;
|
||||
bool hasMoreChanges = true;
|
||||
jmap.State? sinceState = currentState;
|
||||
|
||||
while (hasMoreChanges && sinceState != null) {
|
||||
final changesResponse = await _threadDataSource.getChanges(
|
||||
accountId,
|
||||
sinceState,
|
||||
propertiesCreated: propertiesCreated,
|
||||
propertiesUpdated: propertiesUpdated
|
||||
);
|
||||
|
||||
hasMoreChanges = changesResponse.hasMoreChanges;
|
||||
sinceState = changesResponse.newStateChanges;
|
||||
|
||||
if (emailChangeResponse != null) {
|
||||
emailChangeResponse.union(changesResponse);
|
||||
} else {
|
||||
emailChangeResponse = changesResponse;
|
||||
}
|
||||
}
|
||||
|
||||
if (emailChangeResponse != null) {
|
||||
return EmailsResponse(emailList: emailChangeResponse.created ?? []);
|
||||
} else {
|
||||
return EmailsResponse();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> storeStateToRefresh(TypeName typeName, jmap.State newState) {
|
||||
return _fcmDatasource.storeStateToRefresh(typeName, newState);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<jmap.State> getStateToRefresh(TypeName typeName) {
|
||||
return _fcmDatasource.getStateToRefresh(typeName);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> deleteStateToRefresh(TypeName typeName) {
|
||||
return _fcmDatasource.deleteStateToRefresh(typeName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user