TF-654 Update datasource/repository for download attachment on web

This commit is contained in:
dab246
2022-08-06 19:51:25 +07:00
committed by Dat H. Pham
parent 88ca9dbc9a
commit 6ad10cad4a
6 changed files with 121 additions and 21 deletions
@@ -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<bool> downloadAttachmentForWeb(
Future<Uint8List> downloadAttachmentForWeb(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
AccountRequest accountRequest,
StreamController<Either<Failure, Success>> onReceiveController
);
Future<List<EmailId>> moveToMailbox(AccountId accountId, MoveToMailboxRequest moveRequest);
@@ -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<bool> downloadAttachmentForWeb(Attachment attachment, AccountId accountId, String baseDownloadUrl, AccountRequest accountRequest) {
Future<Uint8List> downloadAttachmentForWeb(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
AccountRequest accountRequest,
StreamController<Either<Failure, Success>> 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;
});
@@ -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<bool> downloadAttachmentForWeb(Attachment attachment, AccountId accountId, String baseDownloadUrl, AccountRequest accountRequest) {
return emailDataSource.downloadAttachmentForWeb(attachment, accountId, baseDownloadUrl, accountRequest);
Future<Uint8List> downloadAttachmentForWeb(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
AccountRequest accountRequest,
StreamController<Either<Failure, Success>> onReceiveController
) {
return emailDataSource.downloadAttachmentForWeb(
taskId,
attachment,
accountId,
baseDownloadUrl,
accountRequest,
onReceiveController);
}
@override
@@ -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<bool> downloadAttachmentForWeb(
Future<Uint8List> downloadAttachmentForWeb(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
AccountRequest accountRequest,
StreamController<Either<Failure, Success>> onReceiveController
);
Future<List<EmailId>> moveToMailbox(AccountId accountId, MoveToMailboxRequest moveRequest);
@@ -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<Object> 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<Object> 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<Object> get props => [];
List<Object> 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<Object> get props => [exception];
List<Object> get props => [taskId, exception];
}
@@ -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<Either<Failure, Success>> execute(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl
String baseDownloadUrl,
StreamController<Either<Failure, Success>> onReceiveController
) async* {
try {
yield Right<Failure, Success>(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<Failure, Success>(DownloadAttachmentForWebSuccess());
} else {
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(null));
}
yield Right<Failure, Success>(DownloadAttachmentForWebSuccess(
taskId,
attachment,
bytesDownloaded));
} catch (exception) {
log('DownloadAttachmentForWebInteractor::execute(): exception: $exception');
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(exception));
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(
taskId,
exception));
}
}
}