diff --git a/lib/features/mailbox/data/datasource/state_datasource.dart b/lib/features/mailbox/data/datasource/state_datasource.dart index fcce4a121..3cdd1ee9c 100644 --- a/lib/features/mailbox/data/datasource/state_datasource.dart +++ b/lib/features/mailbox/data/datasource/state_datasource.dart @@ -1,3 +1,4 @@ +import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/state.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart'; @@ -5,5 +6,5 @@ import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart'; abstract class StateDataSource { Future getState(StateType stateType); - Future saveState(StateCache stateCache); + Future saveState(AccountId accountId, StateCache stateCache); } \ No newline at end of file diff --git a/lib/features/mailbox/data/datasource_impl/state_datasource_impl.dart b/lib/features/mailbox/data/datasource_impl/state_datasource_impl.dart index ee82aaab6..428784c00 100644 --- a/lib/features/mailbox/data/datasource_impl/state_datasource_impl.dart +++ b/lib/features/mailbox/data/datasource_impl/state_datasource_impl.dart @@ -1,6 +1,9 @@ +import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/state.dart'; +import 'package:model/extensions/account_id_extensions.dart'; import 'package:tmail_ui_user/features/caching/state_cache_client.dart'; +import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart'; import 'package:tmail_ui_user/features/mailbox/data/datasource/state_datasource.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart'; @@ -23,13 +26,14 @@ class StateDataSourceImpl extends StateDataSource { } @override - Future saveState(StateCache stateCache) { + Future saveState(AccountId accountId, StateCache stateCache) { return Future.sync(() async { final stateCacheExist = await _stateCacheClient.isExistTable(); + final stateKey = TupleKey(stateCache.type.value, accountId.asString).toString(); if (stateCacheExist) { - return await _stateCacheClient.updateItem(stateCache.type.value, stateCache); + return await _stateCacheClient.updateItem(stateKey, stateCache); } else { - return await _stateCacheClient.insertItem(stateCache.type.value, stateCache); + return await _stateCacheClient.insertItem(stateKey, stateCache); } }).catchError(_exceptionThrower.throwException); } diff --git a/lib/features/mailbox/data/repository/mailbox_repository_impl.dart b/lib/features/mailbox/data/repository/mailbox_repository_impl.dart index c4e623749..396947ac4 100644 --- a/lib/features/mailbox/data/repository/mailbox_repository_impl.dart +++ b/lib/features/mailbox/data/repository/mailbox_repository_impl.dart @@ -69,7 +69,7 @@ class MailboxRepositoryImpl extends MailboxRepository { created: changesResponse.created, destroyed: changesResponse.destroyed), if (changesResponse.newStateMailbox != null) - stateDataSource.saveState(changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)), + stateDataSource.saveState(accountId, changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)), ]); } } else { @@ -78,7 +78,7 @@ class MailboxRepositoryImpl extends MailboxRepository { await Future.wait([ mapDataSource[DataSourceType.local]!.update(accountId, created: mailboxResponse.mailboxes), if (mailboxResponse.state != null) - stateDataSource.saveState(mailboxResponse.state!.toStateCache(StateType.mailbox)), + stateDataSource.saveState(accountId, mailboxResponse.state!.toStateCache(StateType.mailbox)), ]); } @@ -141,7 +141,7 @@ class MailboxRepositoryImpl extends MailboxRepository { created: changesResponse.created, destroyed: changesResponse.destroyed), if (changesResponse.newStateMailbox != null) - stateDataSource.saveState(changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)), + stateDataSource.saveState(accountId, changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)), ]); } diff --git a/lib/features/thread/data/repository/thread_repository_impl.dart b/lib/features/thread/data/repository/thread_repository_impl.dart index b02aefbbe..07c2689fc 100644 --- a/lib/features/thread/data/repository/thread_repository_impl.dart +++ b/lib/features/thread/data/repository/thread_repository_impl.dart @@ -98,7 +98,7 @@ class ThreadRepositoryImpl extends ThreadRepository { if (networkEmailResponse != null) { log('ThreadRepositoryImpl::getAllEmail(): filter = ${emailFilter?.mailboxId} no local state -> update from network: ${networkEmailResponse.state}'); if (networkEmailResponse.state != null) { - await _updateState(networkEmailResponse.state!); + await _updateState(accountId, networkEmailResponse.state!); } } } @@ -191,9 +191,9 @@ class ThreadRepositoryImpl extends ThreadRepository { destroyed: newDestroyed); } - Future _updateState(State newState) async { + Future _updateState(AccountId accountId, State newState) async { log('ThreadRepositoryImpl::_updateState(): [MAIL] $newState'); - await stateDataSource.saveState(newState.toStateCache(StateType.email)); + await stateDataSource.saveState(accountId, newState.toStateCache(StateType.email)); } @override @@ -356,7 +356,7 @@ class ThreadRepositoryImpl extends ThreadRepository { newDestroyed: emailChangeResponse.destroyed); if (emailChangeResponse.newStateEmail != null) { - await _updateState(emailChangeResponse.newStateEmail!); + await _updateState(accountId, emailChangeResponse.newStateEmail!); } } }