TF-123 Implement caching mailbox when get all mailbox

This commit is contained in:
dab246
2021-09-28 15:05:24 +07:00
committed by Dat H. Pham
parent b8b3cf0db0
commit 8f5171d581
21 changed files with 438 additions and 61 deletions
@@ -1,7 +1,19 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache_response.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
abstract class MailboxDataSource {
Future<List<Mailbox>> getAllMailbox(AccountId accountId, {Properties? properties});
Future<GetMailboxResponse?> getAllMailbox(AccountId accountId, {Properties? properties});
Future<MailboxChangeResponse> getChanges(AccountId accountId, State sinceState);
Future<MailboxCacheResponse> getAllMailboxCache();
Future<MailboxChangeResponse> combineMailboxCache(MailboxChangeResponse mailboxChangeResponse, List<Mailbox> mailboxList);
Future<void> asyncUpdateCache(MailboxChangeResponse mailboxChangeResponse);
}
@@ -0,0 +1,53 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/mailbox_datasource.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache_response.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
import 'package:tmail_ui_user/features/mailbox/data/network/mailbox_cache_manager.dart';
class MailboxCacheDataSourceImpl extends MailboxDataSource {
final MailboxCacheManager _mailboxCacheManager;
MailboxCacheDataSourceImpl(this._mailboxCacheManager);
@override
Future<GetMailboxResponse?> getAllMailbox(AccountId accountId, {Properties? properties}) {
throw UnimplementedError();
}
@override
Future<MailboxCacheResponse> getAllMailboxCache() {
return Future.sync(() async {
return await _mailboxCacheManager.getAllMailbox();
}).catchError((error) {
throw error;
});
}
@override
Future<void> asyncUpdateCache(MailboxChangeResponse mailboxChangeResponse) {
return Future.sync(() async {
return await _mailboxCacheManager.asyncUpdateCache(mailboxChangeResponse);
}).catchError((error) {
throw error;
});
}
@override
Future<MailboxChangeResponse> getChanges(AccountId accountId, State sinceState) {
throw UnimplementedError();
}
@override
Future<MailboxChangeResponse> combineMailboxCache(MailboxChangeResponse mailboxChangeResponse, List<Mailbox> mailboxList) {
return Future.sync(() async {
return await _mailboxCacheManager.combineMailboxCache(mailboxChangeResponse, mailboxList);
}).catchError((error) {
throw error;
});
}
}
@@ -1,7 +1,11 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/mailbox_datasource.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache_response.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
import 'package:tmail_ui_user/features/mailbox/data/network/mailbox_api.dart';
class MailboxDataSourceImpl extends MailboxDataSource {
@@ -11,11 +15,35 @@ class MailboxDataSourceImpl extends MailboxDataSource {
MailboxDataSourceImpl(this.mailboxAPI,);
@override
Future<List<Mailbox>> getAllMailbox(AccountId accountId, {Properties? properties}) {
Future<GetMailboxResponse?> getAllMailbox(AccountId accountId, {Properties? properties}) {
return Future.sync(() async {
return await mailboxAPI.getAllMailbox(accountId, properties: properties);
}).catchError((error) {
throw error;
});
}
@override
Future<MailboxCacheResponse> getAllMailboxCache() {
throw UnimplementedError();
}
@override
Future<void> asyncUpdateCache(MailboxChangeResponse mailboxChangeResponse) {
throw UnimplementedError();
}
@override
Future<MailboxChangeResponse> getChanges(AccountId accountId, State sinceState) {
return Future.sync(() async {
return await mailboxAPI.getChanges(accountId, sinceState);
}).catchError((error) {
throw error;
});
}
@override
Future<MailboxChangeResponse> combineMailboxCache(MailboxChangeResponse mailboxChangeResponse, List<Mailbox> mailboxList) {
throw UnimplementedError();
}
}
@@ -0,0 +1,16 @@
import 'package:equatable/equatable.dart';
import 'package:model/model.dart';
class MailboxCacheResponse with EquatableMixin {
final List<MailboxCache>? mailboxCaches;
final StateDao? oldState;
MailboxCacheResponse({
this.mailboxCaches,
this.oldState
});
@override
List<Object?> get props => [mailboxCaches, oldState];
}
@@ -0,0 +1,24 @@
import 'package:equatable/equatable.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
class MailboxChangeResponse with EquatableMixin {
final List<Mailbox>? updated;
final List<Mailbox>? created;
final List<MailboxId>? destroyed;
final State? newState;
final Properties? updatedProperties;
MailboxChangeResponse({
this.updated,
this.created,
this.destroyed,
this.newState,
this.updatedProperties,
});
@override
List<Object?> get props => [updated, created, destroyed, newState, updatedProperties];
}
@@ -3,10 +3,16 @@ import 'dart:async';
import 'package:jmap_dart_client/http/http_client.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/request/reference_path.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/jmap_request.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/changes/changes_mailbox_method.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/changes/changes_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_method.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
class MailboxAPI {
@@ -14,7 +20,7 @@ class MailboxAPI {
MailboxAPI(this.httpClient);
Future<List<Mailbox>> getAllMailbox(AccountId accountId, {Properties? properties}) async {
Future<GetMailboxResponse?> getAllMailbox(AccountId accountId, {Properties? properties}) async {
final processingInvocation = ProcessingInvocation();
final jmapRequestBuilder = JmapRequestBuilder(httpClient, processingInvocation);
@@ -32,6 +38,66 @@ class MailboxAPI {
queryInvocation.methodCallId,
GetMailboxResponse.deserialize);
return resultCreated == null ? [] : resultCreated.list;
return resultCreated;
}
Future<MailboxChangeResponse> getChanges(AccountId accountId, State sinceState) async {
final processingInvocation = ProcessingInvocation();
final jmapRequestBuilder = JmapRequestBuilder(httpClient, processingInvocation);
final changesMailboxMethod = ChangesMailboxMethod(accountId, sinceState, maxChanges: UnsignedInt(128));
final changesMailboxInvocation = jmapRequestBuilder.invocation(changesMailboxMethod);
final getMailboxUpdated = GetMailboxMethod(accountId)
..addReferenceIds(processingInvocation.createResultReference(
changesMailboxInvocation.methodCallId,
ReferencePath.updatedPath))
..addReferenceProperties(processingInvocation.createResultReference(
changesMailboxInvocation.methodCallId,
ReferencePath.updatedPropertiesPath));
final getMailboxCreated = GetMailboxMethod(accountId)
..addReferenceIds(processingInvocation.createResultReference(
changesMailboxInvocation.methodCallId,
ReferencePath.createdPath));
final getMailboxUpdatedInvocation = jmapRequestBuilder.invocation(getMailboxUpdated);
final getMailboxCreatedInvocation = jmapRequestBuilder.invocation(getMailboxCreated);
final result = await (jmapRequestBuilder
..usings(getMailboxCreated.requiredCapabilities))
.build()
.execute();
final resultChanges = result.parse<ChangesMailboxResponse>(
changesMailboxInvocation.methodCallId,
ChangesMailboxResponse.deserialize);
final resultUpdated = result.parse<GetMailboxResponse>(
getMailboxUpdatedInvocation.methodCallId,
GetMailboxResponse.deserialize);
final resultCreated = result.parse<GetMailboxResponse>(
getMailboxCreatedInvocation.methodCallId,
GetMailboxResponse.deserialize);
final listMailboxIdDestroyed = resultChanges?.destroyed.map((id) => MailboxId(id)).toList() ?? <MailboxId>[];
final updatedProperties = resultChanges?.updatedProperties;
final listMailboxUpdated = resultUpdated?.list ?? <Mailbox>[];
final listMailboxCreated = resultCreated?.list ?? <Mailbox>[];
final newState = resultChanges?.newState;
return MailboxChangeResponse(
updated: listMailboxUpdated,
created: listMailboxCreated,
destroyed: listMailboxIdDestroyed,
newState: newState,
updatedProperties: updatedProperties);
}
}
@@ -0,0 +1,105 @@
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/caching/mailbox_cache_client.dart';
import 'package:tmail_ui_user/features/caching/state_cache_client.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache_response.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
class MailboxCacheManager {
final MailboxCacheClient _mailboxCacheClient;
final StateCacheClient _stateCacheClient;
MailboxCacheManager(this._mailboxCacheClient, this._stateCacheClient);
Future<MailboxCacheResponse> getAllMailbox() async {
return await Future.wait(
[
_stateCacheClient.getItem(StateType.mailbox.value),
_mailboxCacheClient.getListItem()
],
eagerError: true
).then((List responses) {
return MailboxCacheResponse(mailboxCaches: responses.last, oldState: responses.first);
});
}
Future<MailboxChangeResponse> combineMailboxCache(MailboxChangeResponse mailboxChangeResponse, List<Mailbox> mailboxList) async {
final mailboxUpdated = mailboxChangeResponse.updated;
final updatedProperties = mailboxChangeResponse.updatedProperties;
if (mailboxUpdated != null && mailboxUpdated.isNotEmpty) {
final newListMailboxUpdated = mailboxUpdated.map((mailboxUpdated) {
if (updatedProperties == null) {
return mailboxUpdated;
} else {
final mailboxOld = findMailbox(mailboxUpdated.id, mailboxList);
if (mailboxOld != null) {
return mailboxOld.combineMailbox(mailboxUpdated, updatedProperties);
} else {
return mailboxUpdated;
}
}
}).toList();
final newMailboxChangeResponse = MailboxChangeResponse(
created: mailboxChangeResponse.created,
destroyed: mailboxChangeResponse.destroyed,
updated: newListMailboxUpdated,
newState: mailboxChangeResponse.newState,
updatedProperties: mailboxChangeResponse.updatedProperties
);
return newMailboxChangeResponse;
}
return mailboxChangeResponse;
}
Mailbox? findMailbox(MailboxId mailboxId, List<Mailbox> mailboxList) {
try {
return mailboxList.firstWhere((element) => element.id == mailboxId);
} catch (e) {
return null;
}
}
Future<void> asyncUpdateCache(MailboxChangeResponse mailboxChangeResponse) async {
if (mailboxChangeResponse.newState != null) {
final stateCacheExist = await _stateCacheClient.isExistTable();
if (stateCacheExist) {
_stateCacheClient.updateItem(
StateType.mailbox.value,
mailboxChangeResponse.newState!.toStateDao(StateType.mailbox));
} else {
_stateCacheClient.insertItem(
StateType.mailbox.value,
mailboxChangeResponse.newState!.toStateDao(StateType.mailbox));
}
}
final mailboxCacheExist = await _mailboxCacheClient.isExistTable();
if (mailboxCacheExist) {
final updatedCacheMailboxes = mailboxChangeResponse.updated
?.map((mailbox) => mailbox.toMailboxCache())
.toList() ?? <MailboxCache>[];
final createdCacheMailboxes = mailboxChangeResponse.created
?.map((mailbox) => mailbox.toMailboxCache())
.toList() ?? <MailboxCache>[];
final destroyedCacheMailboxes = mailboxChangeResponse.destroyed
?.map((mailboxId) => mailboxId.id.value)
.toList() ?? <String>[];
await Future.wait([
_mailboxCacheClient.updateMultipleItem(updatedCacheMailboxes.toMap()),
_mailboxCacheClient.insertMultipleItem(createdCacheMailboxes.toMap()),
_mailboxCacheClient.deleteMultipleItem(destroyedCacheMailboxes)
]);
} else {
final createdCacheMailboxes = mailboxChangeResponse.created
?.map((mailbox) => mailbox.toMailboxCache())
.toList() ?? <MailboxCache>[];
await _mailboxCacheClient.insertMultipleItem(createdCacheMailboxes.toMap());
}
}
}
@@ -1,17 +1,65 @@
import 'package:core/core.dart';
import 'package:model/model.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/mailbox_datasource.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
class MailboxRepositoryImpl extends MailboxRepository {
final MailboxDataSource mailboxDataSource;
final Map<DataSourceType, MailboxDataSource> mapDataSource;
MailboxRepositoryImpl(this.mailboxDataSource);
MailboxRepositoryImpl(this.mapDataSource);
@override
Future<List<Mailbox>> getAllMailbox(AccountId accountId, {Properties? properties}) {
return mailboxDataSource.getAllMailbox(accountId, properties: properties);
Stream<List<Mailbox>> getAllMailbox(AccountId accountId, {Properties? properties}) async* {
final mailboxCacheResponse = await mapDataSource[DataSourceType.local]!.getAllMailboxCache();
final mailboxList = mailboxCacheResponse.mailboxCaches != null
? mailboxCacheResponse.mailboxCaches!.toMailboxList()
: <Mailbox>[];
final oldState = mailboxCacheResponse.oldState?.state;
yield mailboxList;
if (mailboxList.isNotEmpty && oldState != null) {
final changesResponse = await mapDataSource[DataSourceType.network]!.getChanges(accountId, State(oldState));
final newChangesResponse = await mapDataSource[DataSourceType.local]!.combineMailboxCache(changesResponse, mailboxList);
await mapDataSource[DataSourceType.local]!.asyncUpdateCache(newChangesResponse);
} else {
final getMailboxResponse = await mapDataSource[DataSourceType.network]!.getAllMailbox(accountId);
await mapDataSource[DataSourceType.local]!.asyncUpdateCache(MailboxChangeResponse(
created: getMailboxResponse?.list,
newState: getMailboxResponse?.state));
}
final newMailboxCacheResponse = await mapDataSource[DataSourceType.local]!.getAllMailboxCache();
final newMailboxList = newMailboxCacheResponse.mailboxCaches != null
? newMailboxCacheResponse.mailboxCaches!.toMailboxList()
: <Mailbox>[];
yield newMailboxList;
}
@override
Stream<List<Mailbox>> refresh(AccountId accountId, State currentState) async* {
final changesResponse = await mapDataSource[DataSourceType.network]!.getChanges(accountId, currentState);
await mapDataSource[DataSourceType.local]!.asyncUpdateCache(changesResponse);
final newMailboxCacheResponse = await mapDataSource[DataSourceType.local]!.getAllMailboxCache();
final newMailboxList = newMailboxCacheResponse.mailboxCaches != null
? newMailboxCacheResponse.mailboxCaches!.toMailboxList()
: <Mailbox>[];
yield newMailboxList;
}
}