From 6ad10cad4a483af93cd9f83ea5c4f09fa17d15ea Mon Sep 17 00:00:00 2001 From: dab246 Date: Sat, 6 Aug 2022 19:51:25 +0700 Subject: [PATCH] TF-654 Update datasource/repository for download attachment on web --- .../data/datasource/email_datasource.dart | 8 ++- .../email_datasource_impl.dart | 21 +++++++- .../repository/email_repository_impl.dart | 21 +++++++- .../domain/repository/email_repository.dart | 8 ++- .../download_attachment_for_web_state.dart | 54 +++++++++++++++++-- ...ownload_attachment_for_web_interactor.dart | 30 +++++++---- 6 files changed, 121 insertions(+), 21 deletions(-) diff --git a/lib/features/email/data/datasource/email_datasource.dart b/lib/features/email/data/datasource/email_datasource.dart index 4bb166a72..67445e856 100644 --- a/lib/features/email/data/datasource/email_datasource.dart +++ b/lib/features/email/data/datasource/email_datasource.dart @@ -1,4 +1,8 @@ +import 'dart:async'; +import 'dart:typed_data'; + import 'package:core/core.dart'; +import 'package:dartz/dartz.dart'; import 'package:dio/dio.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; @@ -28,11 +32,13 @@ abstract class EmailDataSource { CancelToken cancelToken ); - Future downloadAttachmentForWeb( + Future downloadAttachmentForWeb( + DownloadTaskId taskId, Attachment attachment, AccountId accountId, String baseDownloadUrl, AccountRequest accountRequest, + StreamController> onReceiveController ); Future> moveToMailbox(AccountId accountId, MoveToMailboxRequest moveRequest); diff --git a/lib/features/email/data/datasource_impl/email_datasource_impl.dart b/lib/features/email/data/datasource_impl/email_datasource_impl.dart index 832369024..6897e51b3 100644 --- a/lib/features/email/data/datasource_impl/email_datasource_impl.dart +++ b/lib/features/email/data/datasource_impl/email_datasource_impl.dart @@ -1,4 +1,8 @@ +import 'dart:async'; +import 'dart:typed_data'; + import 'package:core/core.dart'; +import 'package:dartz/dartz.dart'; import 'package:dio/dio.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; @@ -116,9 +120,22 @@ class EmailDataSourceImpl extends EmailDataSource { } @override - Future downloadAttachmentForWeb(Attachment attachment, AccountId accountId, String baseDownloadUrl, AccountRequest accountRequest) { + Future downloadAttachmentForWeb( + DownloadTaskId taskId, + Attachment attachment, + AccountId accountId, + String baseDownloadUrl, + AccountRequest accountRequest, + StreamController> onReceiveController + ) { return Future.sync(() async { - return await emailAPI.downloadAttachmentForWeb(attachment, accountId, baseDownloadUrl, accountRequest); + return await emailAPI.downloadAttachmentForWeb( + taskId, + attachment, + accountId, + baseDownloadUrl, + accountRequest, + onReceiveController); }).catchError((error) { throw error; }); diff --git a/lib/features/email/data/repository/email_repository_impl.dart b/lib/features/email/data/repository/email_repository_impl.dart index 4c47dad52..03bdd4ba5 100644 --- a/lib/features/email/data/repository/email_repository_impl.dart +++ b/lib/features/email/data/repository/email_repository_impl.dart @@ -1,5 +1,9 @@ +import 'dart:async'; +import 'dart:typed_data'; + import 'package:core/core.dart'; +import 'package:dartz/dartz.dart'; import 'package:dio/dio.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; @@ -106,8 +110,21 @@ class EmailRepositoryImpl extends EmailRepository { } @override - Future downloadAttachmentForWeb(Attachment attachment, AccountId accountId, String baseDownloadUrl, AccountRequest accountRequest) { - return emailDataSource.downloadAttachmentForWeb(attachment, accountId, baseDownloadUrl, accountRequest); + Future downloadAttachmentForWeb( + DownloadTaskId taskId, + Attachment attachment, + AccountId accountId, + String baseDownloadUrl, + AccountRequest accountRequest, + StreamController> onReceiveController + ) { + return emailDataSource.downloadAttachmentForWeb( + taskId, + attachment, + accountId, + baseDownloadUrl, + accountRequest, + onReceiveController); } @override diff --git a/lib/features/email/domain/repository/email_repository.dart b/lib/features/email/domain/repository/email_repository.dart index 0dd4f28c2..5a6fab765 100644 --- a/lib/features/email/domain/repository/email_repository.dart +++ b/lib/features/email/domain/repository/email_repository.dart @@ -1,4 +1,8 @@ +import 'dart:async'; +import 'dart:typed_data'; + import 'package:core/core.dart'; +import 'package:dartz/dartz.dart'; import 'package:dio/dio.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; @@ -28,11 +32,13 @@ abstract class EmailRepository { CancelToken cancelToken ); - Future downloadAttachmentForWeb( + Future downloadAttachmentForWeb( + DownloadTaskId taskId, Attachment attachment, AccountId accountId, String baseDownloadUrl, AccountRequest accountRequest, + StreamController> onReceiveController ); Future> moveToMailbox(AccountId accountId, MoveToMailboxRequest moveRequest); diff --git a/lib/features/email/domain/state/download_attachment_for_web_state.dart b/lib/features/email/domain/state/download_attachment_for_web_state.dart index 3c1cfbef9..2c62ef9b0 100644 --- a/lib/features/email/domain/state/download_attachment_for_web_state.dart +++ b/lib/features/email/domain/state/download_attachment_for_web_state.dart @@ -1,18 +1,64 @@ +import 'dart:typed_data'; + import 'package:core/core.dart'; +import 'package:model/model.dart'; + +class StartDownloadAttachmentForWeb extends UIState { + + final DownloadTaskId taskId; + final Attachment attachment; + + StartDownloadAttachmentForWeb(this.taskId, this.attachment); + + @override + List get props => [taskId, attachment]; +} + +class DownloadingAttachmentForWeb extends UIState { + + final DownloadTaskId taskId; + final Attachment attachment; + final double progress; + final int downloaded; + final int total; + + DownloadingAttachmentForWeb( + this.taskId, + this.attachment, + this.progress, + this.downloaded, + this.total + ); + + @override + List get props => [ + taskId, + attachment, + progress, + downloaded, + total + ]; +} class DownloadAttachmentForWebSuccess extends UIState { - DownloadAttachmentForWebSuccess(); + final DownloadTaskId taskId; + final Attachment attachment; + final Uint8List bytes; + + DownloadAttachmentForWebSuccess(this.taskId, this.attachment, this.bytes); @override - List get props => []; + List get props => [taskId, attachment, bytes]; } class DownloadAttachmentForWebFailure extends FeatureFailure { + + final DownloadTaskId taskId; final dynamic exception; - DownloadAttachmentForWebFailure(this.exception); + DownloadAttachmentForWebFailure(this.taskId, this.exception); @override - List get props => [exception]; + List get props => [taskId, exception]; } \ No newline at end of file diff --git a/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart b/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart index e0c66b2ef..6509459e5 100644 --- a/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart +++ b/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart @@ -2,13 +2,13 @@ import 'dart:async'; import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; -import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:model/account/account_request.dart'; import 'package:model/account/authentication_type.dart'; import 'package:model/account/password.dart'; import 'package:model/account/user_name.dart'; +import 'package:model/download/download_task_id.dart'; import 'package:model/email/attachment.dart'; import 'package:model/oidc/token_oidc.dart'; import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart'; @@ -30,14 +30,19 @@ class DownloadAttachmentForWebInteractor { this._authenticationOIDCRepository); Stream> execute( + DownloadTaskId taskId, Attachment attachment, AccountId accountId, - String baseDownloadUrl + String baseDownloadUrl, + StreamController> onReceiveController ) async* { try { + yield Right(StartDownloadAttachmentForWeb(taskId, attachment)); + onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment))); + final currentAccount = await _accountRepository.getCurrentAccount(); - final result = await Future.wait([ + final bytesDownloaded = await Future.wait([ if (currentAccount.authenticationType == AuthenticationType.oidc) _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id) else @@ -61,20 +66,23 @@ class DownloadAttachmentForWebInteractor { } return await emailRepository.downloadAttachmentForWeb( + taskId, attachment, accountId, baseDownloadUrl, - accountRequest); + accountRequest, + onReceiveController + ); }); - if (result) { - yield Right(DownloadAttachmentForWebSuccess()); - } else { - yield Left(DownloadAttachmentForWebFailure(null)); - } + yield Right(DownloadAttachmentForWebSuccess( + taskId, + attachment, + bytesDownloaded)); } catch (exception) { - log('DownloadAttachmentForWebInteractor::execute(): exception: $exception'); - yield Left(DownloadAttachmentForWebFailure(exception)); + yield Left(DownloadAttachmentForWebFailure( + taskId, + exception)); } } } \ No newline at end of file