TF-654 Use StreamController to trigger onReceiveProgress in dio
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:core/core.dart';
|
import 'package:core/core.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:external_path/external_path.dart';
|
import 'package:external_path/external_path.dart';
|
||||||
import 'package:flutter_downloader/flutter_downloader.dart';
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
||||||
@@ -36,14 +38,16 @@ import 'package:model/oidc/token.dart';
|
|||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart';
|
import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart';
|
||||||
import 'package:tmail_ui_user/features/email/domain/model/move_to_mailbox_request.dart';
|
import 'package:tmail_ui_user/features/email/domain/model/move_to_mailbox_request.dart';
|
||||||
|
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
||||||
|
|
||||||
class EmailAPI {
|
class EmailAPI {
|
||||||
|
|
||||||
final HttpClient _httpClient;
|
final HttpClient _httpClient;
|
||||||
final DownloadManager _downloadManager;
|
final DownloadManager _downloadManager;
|
||||||
|
final DioClient _dioClient;
|
||||||
|
|
||||||
EmailAPI(this._httpClient, this._downloadManager);
|
EmailAPI(this._httpClient, this._downloadManager, this._dioClient);
|
||||||
|
|
||||||
Future<Email> getEmailContent(AccountId accountId, EmailId emailId) async {
|
Future<Email> getEmailContent(AccountId accountId, EmailId emailId) async {
|
||||||
final processingInvocation = ProcessingInvocation();
|
final processingInvocation = ProcessingInvocation();
|
||||||
@@ -226,21 +230,42 @@ class EmailAPI {
|
|||||||
cancelToken: cancelToken);
|
cancelToken: cancelToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> downloadAttachmentForWeb(
|
Future<Uint8List> downloadAttachmentForWeb(
|
||||||
|
DownloadTaskId taskId,
|
||||||
Attachment attachment,
|
Attachment attachment,
|
||||||
AccountId accountId,
|
AccountId accountId,
|
||||||
String baseDownloadUrl,
|
String baseDownloadUrl,
|
||||||
AccountRequest accountRequest,
|
AccountRequest accountRequest,
|
||||||
|
StreamController<Either<Failure, Success>> onReceiveController,
|
||||||
) async {
|
) async {
|
||||||
final authentication = accountRequest.authenticationType == AuthenticationType.oidc
|
final authentication = accountRequest.authenticationType == AuthenticationType.oidc
|
||||||
? accountRequest.bearerToken
|
? accountRequest.bearerToken
|
||||||
: accountRequest.basicAuth;
|
: accountRequest.basicAuth;
|
||||||
|
final downloadUrl = attachment.getDownloadUrl(baseDownloadUrl, accountId);
|
||||||
|
log('EmailAPI::downloadAttachmentForWeb(): downloadUrl: $downloadUrl');
|
||||||
|
|
||||||
return _downloadManager.downloadFileForWeb(
|
final headerParam = _dioClient.getHeaders();
|
||||||
attachment.getDownloadUrl(baseDownloadUrl, accountId),
|
headerParam[HttpHeaders.authorizationHeader] = authentication;
|
||||||
attachment.name ?? '',
|
headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader;
|
||||||
authentication,
|
|
||||||
|
final bytesDownloaded = await _dioClient.get(
|
||||||
|
downloadUrl,
|
||||||
|
options: Options(
|
||||||
|
headers: headerParam,
|
||||||
|
responseType: ResponseType.bytes),
|
||||||
|
onReceiveProgress: (downloaded, total) {
|
||||||
|
final progress = (downloaded / total) * 100;
|
||||||
|
log('DownloadClient::downloadFileForWeb(): progress = ${progress.round()}%');
|
||||||
|
onReceiveController.add(Right(DownloadingAttachmentForWeb(
|
||||||
|
taskId,
|
||||||
|
attachment,
|
||||||
|
progress,
|
||||||
|
downloaded,
|
||||||
|
total)));
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return bytesDownloaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<EmailId>> moveToMailbox(AccountId accountId, MoveToMailboxRequest moveRequest) async {
|
Future<List<EmailId>> moveToMailbox(AccountId accountId, MoveToMailboxRequest moveRequest) async {
|
||||||
|
|||||||
Reference in New Issue
Block a user