From dd0d1bbe872bb5c95fe73154ab727336f57a1075 Mon Sep 17 00:00:00 2001 From: dab246 Date: Tue, 28 Sep 2021 15:00:00 +0700 Subject: [PATCH] TF-123 Create objects for caching mailbox --- model/lib/caching/caching_constants.dart | 7 ++ model/lib/caching/mailbox/mailbox_cache.dart | 74 +++++++++++++++++++ .../caching/mailbox/mailbox_rights_cache.dart | 62 ++++++++++++++++ model/lib/caching/state/state_dao.dart | 21 ++++++ model/lib/caching/state/state_type.dart | 21 ++++++ .../list_mailbox_cache_extension.dart | 16 ++++ .../extensions/mailbox_cache_extension.dart | 23 ++++++ model/lib/extensions/mailbox_extension.dart | 57 ++++++++++++++ .../extensions/mailbox_name_extension.dart | 0 .../mailbox_rights_cache_extension.dart | 18 +++++ .../extensions/mailbox_rights_extension.dart | 18 +++++ .../lib/extensions/properties_extension.dart | 7 ++ model/lib/extensions/state_extension.dart | 9 +++ model/lib/mailbox/mailbox_property.dart | 14 ++++ model/lib/model.dart | 18 ++++- model/pubspec.yaml | 5 ++ 16 files changed, 368 insertions(+), 2 deletions(-) create mode 100644 model/lib/caching/caching_constants.dart create mode 100644 model/lib/caching/mailbox/mailbox_cache.dart create mode 100644 model/lib/caching/mailbox/mailbox_rights_cache.dart create mode 100644 model/lib/caching/state/state_dao.dart create mode 100644 model/lib/caching/state/state_type.dart create mode 100644 model/lib/extensions/list_mailbox_cache_extension.dart create mode 100644 model/lib/extensions/mailbox_cache_extension.dart create mode 100644 model/lib/extensions/mailbox_extension.dart rename {lib/features/mailbox/domain => model/lib}/extensions/mailbox_name_extension.dart (100%) create mode 100644 model/lib/extensions/mailbox_rights_cache_extension.dart create mode 100644 model/lib/extensions/mailbox_rights_extension.dart create mode 100644 model/lib/extensions/properties_extension.dart create mode 100644 model/lib/extensions/state_extension.dart create mode 100644 model/lib/mailbox/mailbox_property.dart diff --git a/model/lib/caching/caching_constants.dart b/model/lib/caching/caching_constants.dart new file mode 100644 index 000000000..1b98ffc4a --- /dev/null +++ b/model/lib/caching/caching_constants.dart @@ -0,0 +1,7 @@ + +class CachingConstants { + static const int MAILBOX_CACHE_IDENTIFY = 1; + static const int MAILBOX_RIGHTS_CACHE_IDENTIFY = 2; + static const int STATE_DAO_IDENTIFY = 3; + static const int STATE_TYPE_IDENTIFY = 4; +} \ No newline at end of file diff --git a/model/lib/caching/mailbox/mailbox_cache.dart b/model/lib/caching/mailbox/mailbox_cache.dart new file mode 100644 index 000000000..058586ab4 --- /dev/null +++ b/model/lib/caching/mailbox/mailbox_cache.dart @@ -0,0 +1,74 @@ + +import 'package:equatable/equatable.dart'; +import 'package:hive/hive.dart'; +import 'package:model/caching/mailbox/mailbox_rights_cache.dart'; +import 'package:model/model.dart'; + +part 'mailbox_cache.g.dart'; + +@HiveType(typeId: CachingConstants.MAILBOX_CACHE_IDENTIFY) +class MailboxCache extends HiveObject with EquatableMixin { + + @HiveField(0) + final String id; + + @HiveField(1) + final String? name; + + @HiveField(2) + final String? parentId; + + @HiveField(3) + final String? role; + + @HiveField(4) + final int? sortOrder; + + @HiveField(5) + final int? totalEmails; + + @HiveField(6) + final int? unreadEmails; + + @HiveField(7) + final int? totalThreads; + + @HiveField(8) + final int? unreadThreads; + + @HiveField(9) + final MailboxRightsCache? myRights; + + @HiveField(10) + final bool? isSubscribed; + + @HiveField(11) + final DateTime? lastOpened; + + MailboxCache( + this.id, + { + this.name, + this.parentId, + this.role, + this.sortOrder, + this.totalEmails, + this.unreadEmails, + this.totalThreads, + this.unreadThreads, + this.myRights, + this.isSubscribed, + this.lastOpened + } + ); + + @override + List get props => [ + id, + name, + parentId, + role, + unreadEmails, + lastOpened, + ]; +} \ No newline at end of file diff --git a/model/lib/caching/mailbox/mailbox_rights_cache.dart b/model/lib/caching/mailbox/mailbox_rights_cache.dart new file mode 100644 index 000000000..50a402623 --- /dev/null +++ b/model/lib/caching/mailbox/mailbox_rights_cache.dart @@ -0,0 +1,62 @@ + +import 'package:equatable/equatable.dart'; +import 'package:hive/hive.dart'; +import 'package:model/model.dart'; + +part 'mailbox_rights_cache.g.dart'; + +@HiveType(typeId: CachingConstants.MAILBOX_RIGHTS_CACHE_IDENTIFY) +class MailboxRightsCache extends HiveObject with EquatableMixin { + + @HiveField(0) + final bool mayReadItems; + + @HiveField(1) + final bool mayAddItems; + + @HiveField(2) + final bool mayRemoveItems; + + @HiveField(3) + final bool maySetSeen; + + @HiveField(4) + final bool maySetKeywords; + + @HiveField(5) + final bool mayCreateChild; + + @HiveField(6) + final bool mayRename; + + @HiveField(7) + final bool mayDelete; + + @HiveField(8) + final bool maySubmit; + + MailboxRightsCache( + this.mayReadItems, + this.mayAddItems, + this.mayRemoveItems, + this.maySetSeen, + this.maySetKeywords, + this.mayCreateChild, + this.mayRename, + this.mayDelete, + this.maySubmit + ); + + @override + List get props => [ + mayReadItems, + mayAddItems, + mayRemoveItems, + maySetSeen, + maySetKeywords, + mayCreateChild, + mayRename, + mayDelete, + maySubmit + ]; +} \ No newline at end of file diff --git a/model/lib/caching/state/state_dao.dart b/model/lib/caching/state/state_dao.dart new file mode 100644 index 000000000..b842aa7d8 --- /dev/null +++ b/model/lib/caching/state/state_dao.dart @@ -0,0 +1,21 @@ + +import 'package:equatable/equatable.dart'; +import 'package:hive/hive.dart'; +import 'package:model/model.dart'; + +part 'state_dao.g.dart'; + +@HiveType(typeId: CachingConstants.STATE_DAO_IDENTIFY) +class StateDao extends HiveObject with EquatableMixin { + + @HiveField(0) + final StateType type; + + @HiveField(1) + final String state; + + StateDao(this.type, this.state); + + @override + List get props => [type, state]; +} \ No newline at end of file diff --git a/model/lib/caching/state/state_type.dart b/model/lib/caching/state/state_type.dart new file mode 100644 index 000000000..3ef70a065 --- /dev/null +++ b/model/lib/caching/state/state_type.dart @@ -0,0 +1,21 @@ + +import 'package:hive/hive.dart'; +import 'package:model/model.dart'; + +part 'state_type.g.dart'; + +@HiveType(typeId: CachingConstants.STATE_TYPE_IDENTIFY) +enum StateType { + + @HiveField(0) + mailbox +} + +extension StateTypeExtension on StateType { + String get value { + switch(this) { + case StateType.mailbox: + return 'mailbox'; + } + } +} \ No newline at end of file diff --git a/model/lib/extensions/list_mailbox_cache_extension.dart b/model/lib/extensions/list_mailbox_cache_extension.dart new file mode 100644 index 000000000..7885355b2 --- /dev/null +++ b/model/lib/extensions/list_mailbox_cache_extension.dart @@ -0,0 +1,16 @@ + +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; +import 'package:model/model.dart'; + +extension ListMailboxCacheExtension on List { + Map toMap() { + return Map.fromIterable( + this, + key: (mailboxCache) => mailboxCache.id, + value: (mailboxCache) => mailboxCache); + } + + List toMailboxList() { + return map((mailboxCache) => mailboxCache.toMailbox()).toList(); + } +} \ No newline at end of file diff --git a/model/lib/extensions/mailbox_cache_extension.dart b/model/lib/extensions/mailbox_cache_extension.dart new file mode 100644 index 000000000..b8606dfa6 --- /dev/null +++ b/model/lib/extensions/mailbox_cache_extension.dart @@ -0,0 +1,23 @@ + +import 'package:jmap_dart_client/jmap/core/id.dart'; +import 'package:jmap_dart_client/jmap/core/unsigned_int.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; +import 'package:model/model.dart'; + +extension MailboxCacheExtension on MailboxCache { + Mailbox toMailbox() { + return Mailbox( + MailboxId(Id(id)), + name != null ? MailboxName(name!) : null, + parentId != null ? MailboxId(Id(parentId!)) : null, + role != null ? Role(role!) : null, + sortOrder != null ? SortOrder(sortValue: sortOrder!) : null, + totalEmails != null ? TotalEmails(UnsignedInt(totalEmails!)) : null, + unreadEmails != null ? UnreadEmails(UnsignedInt(unreadEmails!)) : null, + totalThreads != null ? TotalThreads(UnsignedInt(totalThreads!)) : null, + unreadThreads != null ? UnreadThreads(UnsignedInt(unreadThreads!)) : null, + myRights != null ? myRights!.toMailboxRights() : null, + isSubscribed != null ? IsSubscribed(isSubscribed!) : null + ); + } +} \ No newline at end of file diff --git a/model/lib/extensions/mailbox_extension.dart b/model/lib/extensions/mailbox_extension.dart new file mode 100644 index 000000000..38be79f62 --- /dev/null +++ b/model/lib/extensions/mailbox_extension.dart @@ -0,0 +1,57 @@ +import 'package:jmap_dart_client/jmap/core/properties/properties.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; +import 'package:model/model.dart'; + +extension MailboxExtension on Mailbox { + + bool hasRole() => role != null && role!.value.isNotEmpty; + + PresentationMailbox toPresentationMailbox({SelectMode selectMode = SelectMode.INACTIVE}) { + return PresentationMailbox( + id, + name: name, + parentId: parentId, + role: role, + sortOrder: sortOrder, + totalEmails: totalEmails, + unreadEmails: unreadEmails, + totalThreads: totalThreads, + unreadThreads: unreadThreads, + myRights: myRights, + isSubscribed: isSubscribed, + selectMode: selectMode + ); + } + + MailboxCache toMailboxCache() { + return MailboxCache( + id.id.value, + name: name?.name, + parentId: parentId?.id.value, + role: role?.value, + sortOrder: sortOrder?.value.value.round(), + totalEmails: totalEmails?.value.value.round(), + unreadEmails: unreadEmails?.value.value.round(), + totalThreads: totalThreads?.value.value.round(), + unreadThreads: unreadThreads?.value.value.round(), + myRights: myRights != null ? myRights!.toMailboxRightsCache() : null, + isSubscribed: isSubscribed?.value + ); + } + + Mailbox combineMailbox(Mailbox newMailbox, Properties updatedProperties) { + return Mailbox( + newMailbox.id, + updatedProperties.contain(MailboxProperty.name) ? newMailbox.name : name, + updatedProperties.contain(MailboxProperty.parentId) ? newMailbox.parentId : parentId, + updatedProperties.contain(MailboxProperty.role) ? newMailbox.role : role, + updatedProperties.contain(MailboxProperty.sortOrder) ? newMailbox.sortOrder : sortOrder, + updatedProperties.contain(MailboxProperty.totalEmails) ? newMailbox.totalEmails : totalEmails, + updatedProperties.contain(MailboxProperty.unreadEmails) ? newMailbox.unreadEmails : unreadEmails, + updatedProperties.contain(MailboxProperty.totalThreads) ? newMailbox.totalThreads : totalThreads, + updatedProperties.contain(MailboxProperty.unreadThreads) ? newMailbox.unreadThreads : unreadThreads, + updatedProperties.contain(MailboxProperty.myRights) ? newMailbox.myRights : myRights, + updatedProperties.contain(MailboxProperty.isSubscribed) ? newMailbox.isSubscribed : isSubscribed, + ); + } +} \ No newline at end of file diff --git a/lib/features/mailbox/domain/extensions/mailbox_name_extension.dart b/model/lib/extensions/mailbox_name_extension.dart similarity index 100% rename from lib/features/mailbox/domain/extensions/mailbox_name_extension.dart rename to model/lib/extensions/mailbox_name_extension.dart diff --git a/model/lib/extensions/mailbox_rights_cache_extension.dart b/model/lib/extensions/mailbox_rights_cache_extension.dart new file mode 100644 index 000000000..da38f9ec7 --- /dev/null +++ b/model/lib/extensions/mailbox_rights_cache_extension.dart @@ -0,0 +1,18 @@ +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox_rights.dart'; +import 'package:model/caching/mailbox/mailbox_rights_cache.dart'; + +extension MailboxRightsCacheExtension on MailboxRightsCache { + MailboxRights toMailboxRights() { + return MailboxRights( + mayReadItems, + mayAddItems, + mayRemoveItems, + maySetSeen, + maySetKeywords, + mayCreateChild, + mayRename, + mayDelete, + maySubmit + ); + } +} \ No newline at end of file diff --git a/model/lib/extensions/mailbox_rights_extension.dart b/model/lib/extensions/mailbox_rights_extension.dart new file mode 100644 index 000000000..234c28786 --- /dev/null +++ b/model/lib/extensions/mailbox_rights_extension.dart @@ -0,0 +1,18 @@ +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox_rights.dart'; +import 'package:model/caching/mailbox/mailbox_rights_cache.dart'; + +extension MailboxRightsExtension on MailboxRights { + MailboxRightsCache toMailboxRightsCache() { + return MailboxRightsCache( + mayReadItems, + mayAddItems, + mayRemoveItems, + maySetSeen, + maySetKeywords, + mayCreateChild, + mayRename, + mayDelete, + maySubmit + ); + } +} \ No newline at end of file diff --git a/model/lib/extensions/properties_extension.dart b/model/lib/extensions/properties_extension.dart new file mode 100644 index 000000000..92601ba99 --- /dev/null +++ b/model/lib/extensions/properties_extension.dart @@ -0,0 +1,7 @@ + +import 'package:jmap_dart_client/jmap/core/properties/properties.dart'; + +extension PropertiesExtension on Properties { + + bool contain(String key) => value.contains(key); +} \ No newline at end of file diff --git a/model/lib/extensions/state_extension.dart b/model/lib/extensions/state_extension.dart new file mode 100644 index 000000000..6f7c721d8 --- /dev/null +++ b/model/lib/extensions/state_extension.dart @@ -0,0 +1,9 @@ + +import 'package:jmap_dart_client/jmap/core/state.dart'; +import 'package:model/model.dart'; + +extension StateExtension on State { + StateDao toStateDao(StateType stateType) { + return StateDao(stateType, value); + } +} \ No newline at end of file diff --git a/model/lib/mailbox/mailbox_property.dart b/model/lib/mailbox/mailbox_property.dart new file mode 100644 index 000000000..a410c5dec --- /dev/null +++ b/model/lib/mailbox/mailbox_property.dart @@ -0,0 +1,14 @@ + +class MailboxProperty { + static const String id = 'id'; + static const String name = 'name'; + static const String parentId = 'parentId'; + static const String role = 'role'; + static const String sortOrder = 'sortOrder'; + static const String totalEmails = 'totalEmails'; + static const String unreadEmails = 'unreadEmails'; + static const String totalThreads = 'totalThreads'; + static const String unreadThreads = 'unreadThreads'; + static const String myRights = 'myRights'; + static const String isSubscribed = 'isSubscribed'; +} \ No newline at end of file diff --git a/model/lib/model.dart b/model/lib/model.dart index dd99340ca..21711d4c3 100644 --- a/model/lib/model.dart +++ b/model/lib/model.dart @@ -14,6 +14,7 @@ export 'user/user_profile_response.dart'; export 'mailbox/presentation_mailbox.dart'; export 'mailbox/select_mode.dart'; export 'mailbox/expand_mode.dart'; +export 'mailbox/mailbox_property.dart'; // Email export 'email/presentation_email.dart'; @@ -41,7 +42,14 @@ export 'extensions/email_body_part_extension.dart'; export 'extensions/attachment_extension.dart'; export 'extensions/list_email_id_extension.dart'; export 'extensions/mailbox_id_extension.dart'; -export 'extensions/list_email_id_extension.dart'; +export 'extensions/mailbox_extension.dart'; +export 'extensions/mailbox_name_extension.dart'; +export 'extensions/list_mailbox_cache_extension.dart'; +export 'extensions/state_extension.dart'; +export 'extensions/mailbox_rights_extension.dart'; +export 'extensions/mailbox_rights_cache_extension.dart'; +export 'extensions/mailbox_cache_extension.dart'; +export 'extensions/properties_extension.dart'; // Download export 'download/download_task_id.dart'; @@ -53,4 +61,10 @@ export 'contact/device_contact.dart'; // Upload export 'upload/file_info.dart'; export 'upload/upload_request.dart'; -export 'upload/upload_response.dart'; \ No newline at end of file +export 'upload/upload_response.dart'; + +// Caching +export 'caching/caching_constants.dart'; +export 'caching/mailbox/mailbox_cache.dart'; +export 'caching/state/state_dao.dart'; +export 'caching/state/state_type.dart'; \ No newline at end of file diff --git a/model/pubspec.yaml b/model/pubspec.yaml index ef8a5b102..4401c05b1 100644 --- a/model/pubspec.yaml +++ b/model/pubspec.yaml @@ -55,6 +55,9 @@ dependencies: # http_parser http_parser: 4.0.0 + # hive + hive: 2.0.4 + dev_dependencies: flutter_test: sdk: flutter @@ -62,6 +65,8 @@ dev_dependencies: build_runner: 2.0.5 json_serializable: 4.1.3 + + hive_generator: 1.1.1 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec