Store state cache by AccountId
(cherry picked from commit cb8d44d31360986b1c87694681166e83fc78a666)
This commit is contained in:
@@ -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<State?> getState(StateType stateType);
|
||||
|
||||
Future<void> saveState(StateCache stateCache);
|
||||
Future<void> saveState(AccountId accountId, StateCache stateCache);
|
||||
}
|
||||
@@ -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<void> saveState(StateCache stateCache) {
|
||||
Future<void> 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);
|
||||
}
|
||||
|
||||
@@ -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)),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<void> _updateState(State newState) async {
|
||||
Future<void> _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!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user