TF-32 Add domain layer of download attachment for android platform

This commit is contained in:
dab246
2021-09-16 09:07:11 +07:00
committed by Dat H. Pham
parent 9b43417ac8
commit ddf8a32e66
16 changed files with 214 additions and 13 deletions
@@ -117,7 +117,7 @@ class ComposerController extends BaseController {
return null;
}
Tuple3<List<MessageContent>, List<AttachmentFile>, Session>? getContentEmailQuoted() {
Tuple3<List<MessageContent>, List<Attachment>, Session>? getContentEmailQuoted() {
if (composerArguments.value != null) {
final emailContent = composerArguments.value!.emailContent;
final session = composerArguments.value!.session;
@@ -196,10 +196,11 @@ class ComposerController extends BaseController {
final messageContent = contentEmail.value1.first;
final attachmentInlines = contentEmail.value2;
final session = contentEmail.value3;
final baseDownloadUrl = session.getDownloadUrl();
final accountId = session.accounts.keys.first;
final message = (attachmentInlines.isNotEmpty && messageContent.hasImageInlineWithCid())
? '${messageContent.getContentHasInlineAttachment(session, accountId, attachmentInlines)}'
? '${messageContent.getContentHasInlineAttachment(baseDownloadUrl, accountId, attachmentInlines)}'
: '${messageContent.content}';
trustAsHtml = htmlMessagePurifier.purifyHtmlMessage(message, allowAttributes: {'style', 'input', 'form'})
@@ -1,6 +1,6 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:model/email/read_actions.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart';
abstract class EmailRepository {
@@ -9,4 +9,11 @@ abstract class EmailRepository {
Future<bool> sendEmail(AccountId accountId, EmailRequest emailRequest);
Future<bool> markAsRead(AccountId accountId, EmailId emailId, ReadActions readAction);
Future<List<DownloadTaskId>> downloadAttachments(
List<Attachment> attachments,
AccountId accountId,
String baseDownloadUrl,
AccountRequest accountRequest
);
}
@@ -0,0 +1,20 @@
import 'package:core/core.dart';
import 'package:model/model.dart';
class DownloadAttachmentsSuccess extends UIState {
final List<DownloadTaskId> taskIds;
DownloadAttachmentsSuccess(this.taskIds);
@override
List<Object> get props => [taskIds];
}
class DownloadAttachmentsFailure extends FeatureFailure {
final exception;
DownloadAttachmentsFailure(this.exception);
@override
List<Object> get props => [exception];
}
@@ -0,0 +1,38 @@
import 'package:core/core.dart';
import 'package:model/model.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/email/domain/state/download_attachments_state.dart';
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
class DownloadAttachmentsInteractor {
final EmailRepository emailRepository;
final CredentialRepository credentialRepository;
DownloadAttachmentsInteractor(this.emailRepository, this.credentialRepository);
Future<Either<Failure, Success>> execute(
List<Attachment> attachments,
AccountId accountId,
String baseDownloadUrl
) async {
try {
final taskIds = await Future.wait(
[credentialRepository.getUserName(), credentialRepository.getPassword()],
eagerError: true
).then((List responses) async {
final accountRequest = AccountRequest(responses.first, responses.last);
return await emailRepository.downloadAttachments(
attachments,
accountId,
baseDownloadUrl,
accountRequest);
});
return Right<Failure, Success>(DownloadAttachmentsSuccess(taskIds));
} catch (e) {
return Left<Failure, Success>(DownloadAttachmentsFailure(e));
}
}
}
@@ -1,4 +1,6 @@
import 'package:core/core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
import 'package:tmail_ui_user/features/login/domain/usecases/get_credential_interactor.dart';
@@ -14,9 +16,18 @@ class HomeController extends GetxController {
@override
void onReady() {
super.onReady();
_initFlutterDownloader();
_getCredentialAction();
}
void _initFlutterDownloader() {
FlutterDownloader
.initialize(debug: kDebugMode)
.then((_) => FlutterDownloader.registerCallback(downloadCallback));
}
static void downloadCallback(String id, DownloadTaskStatus status, int progress) {}
void _getCredentialAction() async {
await _getCredentialInteractor.execute()
.then((response) => response.fold(