TF-3450 Refactor image and text preview

This commit is contained in:
DatDang
2025-02-26 11:19:25 +07:00
committed by Dat H. Pham
parent ef1d37f7ac
commit aabfe6a3a5
14 changed files with 94 additions and 285 deletions
@@ -12,11 +12,12 @@ class StartDownloadAttachmentForWeb extends UIState {
final DownloadTaskId taskId;
final Attachment attachment;
final CancelToken? cancelToken;
final bool previewerSupported;
StartDownloadAttachmentForWeb(this.taskId, this.attachment, [this.cancelToken]);
StartDownloadAttachmentForWeb(this.taskId, this.attachment, [this.cancelToken, this.previewerSupported = false]);
@override
List<Object?> get props => [taskId, attachment, cancelToken];
List<Object?> get props => [taskId, attachment, cancelToken, previewerSupported];
}
class DownloadingAttachmentForWeb extends UIState {
@@ -50,11 +51,12 @@ class DownloadAttachmentForWebSuccess extends UIState {
final DownloadTaskId taskId;
final Attachment attachment;
final Uint8List bytes;
final bool previewerSupported;
DownloadAttachmentForWebSuccess(this.taskId, this.attachment, this.bytes);
DownloadAttachmentForWebSuccess(this.taskId, this.attachment, this.bytes, this.previewerSupported);
@override
List<Object> get props => [taskId, attachment, bytes];
List<Object> get props => [taskId, attachment, bytes, previewerSupported];
}
class DownloadAttachmentForWebFailure extends FeatureFailure {
@@ -1,31 +0,0 @@
import 'dart:typed_data';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:model/email/attachment.dart';
class GettingImageDataFromAttachment extends LoadingState {
GettingImageDataFromAttachment({required this.attachment});
final Attachment attachment;
@override
List<Object> get props => [attachment];
}
class GetImageDataFromAttachmentSuccess extends UIState {
GetImageDataFromAttachmentSuccess({
required this.attachment,
required this.data,
});
final Attachment attachment;
final Uint8List data;
@override
List<Object> get props => [attachment, data];
}
class GetImageDataFromAttachmentFailure extends FeatureFailure {
GetImageDataFromAttachmentFailure({super.exception});
}
@@ -1,31 +0,0 @@
import 'dart:typed_data';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:model/email/attachment.dart';
class GettingTextDataFromAttachment extends LoadingState {
GettingTextDataFromAttachment({required this.attachment});
final Attachment attachment;
@override
List<Object> get props => [attachment];
}
class GetTextDataFromAttachmentSuccess extends UIState {
GetTextDataFromAttachmentSuccess({
required this.attachment,
required this.data,
});
final Attachment attachment;
final Uint8List data;
@override
List<Object> get props => [attachment, data];
}
class GetTextDataFromAttachmentFailure extends FeatureFailure {
GetTextDataFromAttachmentFailure({super.exception});
}
@@ -35,11 +35,12 @@ class DownloadAttachmentForWebInteractor {
AccountId accountId,
String baseDownloadUrl,
StreamController<Either<Failure, Success>> onReceiveController,
{CancelToken? cancelToken}
) async* {
{CancelToken? cancelToken,
bool previewerSupported = false,
}) async* {
try {
yield Right<Failure, Success>(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken));
onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken)));
yield Right<Failure, Success>(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported));
onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported)));
final currentAccount = await _accountRepository.getCurrentAccount();
AccountRequest? accountRequest;
@@ -69,7 +70,8 @@ class DownloadAttachmentForWebInteractor {
DownloadAttachmentForWebSuccess(
taskId,
attachment,
bytesDownloaded
bytesDownloaded,
previewerSupported,
)
);
} catch (exception) {
@@ -1,62 +0,0 @@
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:dio/dio.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:model/download/download_task_id.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_image_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
class GetImageDataFromAttachmentInteractor {
const GetImageDataFromAttachmentInteractor(this._downloadAttachmentForWebInteractor);
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
Stream<Either<Failure, Success>> execute(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
{CancelToken? cancelToken,}
) async* {
final onProgressStream = StreamController<Either<Failure, Success>>();
try {
yield Right(GettingImageDataFromAttachment(attachment: attachment));
final downloadState = await _downloadAttachmentForWebInteractor.execute(
taskId,
attachment,
accountId,
baseDownloadUrl,
onProgressStream,
cancelToken: cancelToken,
).last;
yield downloadState.fold(
(failure) {
return Left(GetImageDataFromAttachmentFailure(
exception: failure is FeatureFailure ? failure.exception : null,
));
},
(success) {
if (success is! DownloadAttachmentForWebSuccess) {
return Left(GetImageDataFromAttachmentFailure());
}
return Right(GetImageDataFromAttachmentSuccess(
attachment: attachment,
data: success.bytes,
));
},
);
} catch (e) {
logError('GetImageDataFromAttachmentInteractor::execute(): $e');
yield Left(GetImageDataFromAttachmentFailure(exception: e));
}
onProgressStream.close();
}
}
@@ -1,62 +0,0 @@
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:dio/dio.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:model/download/download_task_id.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_text_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
class GetTextDataFromAttachmentInteractor {
const GetTextDataFromAttachmentInteractor(this._downloadAttachmentForWebInteractor);
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
Stream<Either<Failure, Success>> execute(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
{CancelToken? cancelToken,}
) async* {
final onProgressStream = StreamController<Either<Failure, Success>>();
try {
yield Right(GettingTextDataFromAttachment(attachment: attachment));
final downloadState = await _downloadAttachmentForWebInteractor.execute(
taskId,
attachment,
accountId,
baseDownloadUrl,
onProgressStream,
cancelToken: cancelToken,
).last;
yield downloadState.fold(
(failure) {
return Left(GetTextDataFromAttachmentFailure(
exception: failure is FeatureFailure ? failure.exception : null,
));
},
(success) {
if (success is! DownloadAttachmentForWebSuccess) {
return Left(GetTextDataFromAttachmentFailure());
}
return Right(GetTextDataFromAttachmentSuccess(
attachment: attachment,
data: success.bytes,
));
},
);
} catch (e) {
logError('GetTextDataFromAttachmentInteractor::execute(): $e');
yield Left(GetTextDataFromAttachmentFailure(exception: e));
}
onProgressStream.close();
}
}