TF-1900 Auto sync Sending Queue after work manager completed

(cherry picked from commit e68a8fdc49c600224b93e25f60ed6b8ecf1afe84)
This commit is contained in:
dab246
2023-06-04 12:21:27 +07:00
committed by Dat Vu
parent 38bd86019e
commit b2f96c8142
11 changed files with 92 additions and 20 deletions
@@ -254,9 +254,9 @@ class EmailHiveCacheDataSourceImpl extends EmailDataSource {
}
@override
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName) {
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName, {bool needToReopen = false}) {
return Future.sync(() async {
final sendingEmailsCache = await _sendingEmailCacheManager.getAllSendingEmails(accountId, userName);
final sendingEmailsCache = await _sendingEmailCacheManager.getAllSendingEmails(accountId, userName, needToReopen: needToReopen);
return sendingEmailsCache.toSendingEmails();
}).catchError(_exceptionThrower.throwException);
}
@@ -443,7 +443,7 @@ class MailboxDashBoardController extends ReloadableController {
_getVacationResponse();
spamReportController.getSpamReportStateAction();
if (PlatformInfo.isMobile) {
_getAllSendingEmails();
getAllSendingEmails();
}
if (!BuildUtils.isWeb && !_notificationManager.isNotificationClickedOnTerminate) {
_handleClickLocalNotificationOnTerminated();
@@ -1225,7 +1225,7 @@ class MailboxDashBoardController extends ReloadableController {
_getVacationResponse();
spamReportController.getSpamReportStateAction();
if (PlatformInfo.isMobile) {
_getAllSendingEmails();
getAllSendingEmails();
}
}
@@ -1830,7 +1830,7 @@ class MailboxDashBoardController extends ReloadableController {
}
_addSendingEmailToSendingQueue(success.sendingEmail);
_getAllSendingEmails();
getAllSendingEmails();
}
void _addSendingEmailToSendingQueue(SendingEmail sendingEmail) async {
@@ -1853,12 +1853,13 @@ class MailboxDashBoardController extends ReloadableController {
}
}
void _getAllSendingEmails() {
void getAllSendingEmails({bool needToReopen = false}) {
if (accountId.value != null && sessionCurrent != null) {
log('MailboxDashBoardController::_getAllSendingEmails():accountId: ${accountId.value} | userName: ${sessionCurrent?.username}');
log('MailboxDashBoardController::getAllSendingEmails():accountId: ${accountId.value} | userName: ${sessionCurrent?.username}');
consumeState(_getAllSendingEmailInteractor.execute(
accountId.value!,
sessionCurrent!.username
sessionCurrent!.username,
needToReopen: needToReopen
));
}
}
@@ -1866,6 +1867,15 @@ class MailboxDashBoardController extends ReloadableController {
void _handleGetAllSendingEmailsSuccess(GetAllSendingEmailSuccess success) {
log('MailboxDashBoardController::_handleGetAllSendingEmailsSuccess():COUNT: ${success.sendingEmails.length}');
listSendingEmails.value = success.sendingEmails;
if (success.sendingEmails.isEmpty && dashboardRoute.value == DashboardRoutes.sendingQueue) {
_openDefaultMailbox();
}
}
void _openDefaultMailbox() {
dispatchRoute(DashboardRoutes.thread);
dispatchMailboxUIAction(SelectMailboxDefaultAction());
}
@override
@@ -1 +1,3 @@
class SendingEmailFromWorkmanagerException implements Exception {}
class UnableToGetDataFromInWorkManagerException implements Exception {}
class UnableBindingInWorkManagerException implements Exception {}
@@ -24,8 +24,15 @@ class SendingEmailCacheManager {
return _hiveCacheClient.insertItem(keyCache, sendingEmailHiveCache);
}
Future<List<SendingEmailHiveCache>> getAllSendingEmails(AccountId accountId, UserName userName) async {
final sendingEmailsCache = await _hiveCacheClient.getListByTupleKey(accountId.asString, userName.value);
Future<List<SendingEmailHiveCache>> getAllSendingEmails(
AccountId accountId,
UserName userName,
{bool needToReopen = false}
) async {
final sendingEmailsCache = await _hiveCacheClient.getListByTupleKey(
accountId.asString,
userName.value,
needToReopen: needToReopen);
log('SendingEmailCacheManager::getAllSendingEmails():COUNT: ${sendingEmailsCache.length}');
sendingEmailsCache.sortByLatestTime();
return sendingEmailsCache;
@@ -26,8 +26,10 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/m
import 'package:tmail_ui_user/features/offline_mode/biding/sending_email_biding.dart';
import 'package:tmail_ui_user/features/offline_mode/exceptions/workmanager_exception.dart';
import 'package:tmail_ui_user/features/offline_mode/observer/work_observer.dart';
import 'package:tmail_ui_user/features/offline_mode/scheduler/worker_state.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_config.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/utils/sending_queue_isolate_manager.dart';
import 'package:tmail_ui_user/features/session/domain/state/get_session_state.dart';
import 'package:tmail_ui_user/features/session/domain/usecases/get_session_interactor.dart';
import 'package:tmail_ui_user/main/bindings/main_bindings.dart';
@@ -46,6 +48,7 @@ class SendingEmailObserver extends WorkObserver {
AuthorizationInterceptors? _authorizationInterceptors;
GetSessionInteractor? _getSessionInteractor;
DeleteSendingEmailInteractor? _deleteSendingEmailInteractor;
SendingQueueIsolateManager? _sendingQueueIsolateManager;
Completer<bool>? _completer;
@@ -115,9 +118,10 @@ class SendingEmailObserver extends WorkObserver {
_getSessionInteractor = getBinding<GetSessionInteractor>();
_sendEmailInteractor = getBinding<SendEmailInteractor>();
_deleteSendingEmailInteractor = getBinding<DeleteSendingEmailInteractor>();
_sendingQueueIsolateManager = getBinding<SendingQueueIsolateManager>();
} catch (e) {
logError('SendingEmailObserver::_getInteractorBindings(): ${e.toString()}');
return _completer?.completeError(false);
_completer?.completeError(UnableBindingInWorkManagerException());
}
}
@@ -227,12 +231,15 @@ class SendingEmailObserver extends WorkObserver {
void _handleDeleteSendingEmailSuccess() async {
log('SendingEmailObserver::_handleDeleteSendingEmailSuccess(): Success');
_showLocalNotification();
return _completer?.complete(true);
_sendingQueueIsolateManager?.addEvent(WorkerState.success.name);
_completer?.complete(true);
}
void _clearDataQueue() async {
log('SendingEmailObserver::_clearDataQueue():');
_sendingEmail = null;
_completer?.completeError(SendingEmailFromWorkmanagerException());
_sendingQueueIsolateManager?.addEvent(WorkerState.failed.name);
_completer?.completeError(UnableToGetDataFromInWorkManagerException());
}
void _showLocalNotification() {
@@ -0,0 +1,5 @@
enum WorkerState {
success,
failed;
}
@@ -11,7 +11,7 @@ class SendingQueueRepositoryImpl extends SendingQueueRepository {
SendingQueueRepositoryImpl(this._emailHiveCacheDataSourceImpl);
@override
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName) {
return _emailHiveCacheDataSourceImpl.getAllSendingEmails(accountId, userName);
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName, {bool needToReopen = false}) {
return _emailHiveCacheDataSourceImpl.getAllSendingEmails(accountId, userName, needToReopen: needToReopen);
}
}
@@ -4,5 +4,5 @@ import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/composer/domain/model/sending_email.dart';
abstract class SendingQueueRepository {
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName);
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName, {bool needToReopen = false});
}
@@ -11,10 +11,10 @@ class GetAllSendingEmailInteractor {
GetAllSendingEmailInteractor(this._sendingQueueRepository);
Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName) async* {
Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName, {bool needToReopen = false}) async* {
try {
yield Right<Failure, Success>(GetAllSendingEmailLoading());
final sendingEmails = await _sendingQueueRepository.getAllSendingEmails(accountId, userName);
final sendingEmails = await _sendingQueueRepository.getAllSendingEmails(accountId, userName, needToReopen: needToReopen);
yield Right<Failure, Success>(GetAllSendingEmailSuccess(sendingEmails));
} catch (e) {
yield Left<Failure, Success>(GetAllSendingEmailFailure(e));
@@ -1,22 +1,54 @@
import 'package:collection/collection.dart';
import 'package:core/utils/app_logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:tmail_ui_user/features/base/base_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/offline_mode/scheduler/worker_state.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/utils/sending_queue_isolate_manager.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
class SendingQueueController extends BaseController {
final dashboardController = getBinding<MailboxDashBoardController>();
final sendingQueueIsolateManager = getBinding<SendingQueueIsolateManager>();
final listSendingEmailController = ScrollController();
@override
void onInit() {
super.onInit();
sendingQueueIsolateManager?.initial(
onData: _handleSendingQueueEvent,
onError: (error, stackTrace) {
logError('SendingQueueController::onInit():onError:error: $error | stackTrace: $stackTrace');
}
);
}
void _handleSendingQueueEvent(Object? event) {
log('SendingQueueController::_handleSendingQueueEvent():event: $event');
if (event is String) {
final workState = WorkerState.values.firstWhereOrNull((state) => state.name == event);
log('SendingQueueController::_handleSendingQueueEvent():workState: $workState');
if (workState != null) {
_refreshSendingQueue(needToReopen: true);
}
}
}
void _refreshSendingQueue({bool needToReopen = false}) {
dashboardController?.getAllSendingEmails(needToReopen: needToReopen);
}
void openMailboxMenu() {
dashboardController!.openMailboxMenuDrawer();
dashboardController?.openMailboxMenuDrawer();
}
@override
void onClose() {
listSendingEmailController.dispose();
sendingQueueIsolateManager?.release();
super.onClose();
}
}
@@ -5,10 +5,12 @@ import 'package:core/presentation/utils/app_toast.dart';
import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:core/utils/config/app_config_loader.dart';
import 'package:core/utils/file_utils.dart';
import 'package:core/utils/platform_info.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tmail_ui_user/features/email/data/local/html_analyzer.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/utils/sending_queue_isolate_manager.dart';
import 'package:tmail_ui_user/main/utils/email_receive_manager.dart';
import 'package:uuid/uuid.dart';
@@ -24,6 +26,7 @@ class CoreBindings extends Bindings {
_bindingDeviceManager();
_bindingReceivingSharingStream();
_bindingUtils();
_bindingIsolate();
}
void _bindingAppImagePaths() {
@@ -61,4 +64,10 @@ class CoreBindings extends Bindings {
Get.put(AppConfigLoader());
Get.put(FileUtils());
}
void _bindingIsolate() {
if (PlatformInfo.isMobile) {
Get.put(SendingQueueIsolateManager());
}
}
}