TF-3450 Add text attachment preview

This commit is contained in:
DatDang
2025-02-20 10:24:58 +07:00
committed by Dat H. Pham
parent 3645fe58f9
commit ef1d37f7ac
9 changed files with 150 additions and 0 deletions
@@ -0,0 +1,31 @@
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});
}
@@ -0,0 +1,62 @@
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();
}
}