diff --git a/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart b/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart index 784d49540..9596ae82b 100644 --- a/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart +++ b/lib/features/email/domain/usecases/download_attachment_for_web_interactor.dart @@ -41,41 +41,39 @@ class DownloadAttachmentForWebInteractor { onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment))); final currentAccount = await _accountRepository.getCurrentAccount(); + AccountRequest? accountRequest; - final bytesDownloaded = await Future.wait([ - if (currentAccount.authenticationType == AuthenticationType.oidc) - _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id) - else - credentialRepository.getAuthenticationInfoStored() - ], eagerError: true).then((List responses) async { - AccountRequest accountRequest; - - if (currentAccount.authenticationType == AuthenticationType.oidc) { - final tokenOidc = responses.first as TokenOIDC; + if (currentAccount.authenticationType == AuthenticationType.oidc) { + final tokenOidc = await _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id); + accountRequest = AccountRequest( + token: tokenOidc.toToken(), + authenticationType: AuthenticationType.oidc); + } else { + final authenticationInfoCache = await credentialRepository.getAuthenticationInfoStored(); + if (authenticationInfoCache != null) { accountRequest = AccountRequest( - token: tokenOidc.toToken(), - authenticationType: AuthenticationType.oidc); - } else { - accountRequest = AccountRequest( - userName: responses.first as UserName, - password: responses.last as Password, + userName: UserName(authenticationInfoCache.username), + password: Password(authenticationInfoCache.password), authenticationType: AuthenticationType.basic); } + } - return await emailRepository.downloadAttachmentForWeb( + if (accountRequest != null) { + final bytesDownloaded = await emailRepository.downloadAttachmentForWeb( taskId, attachment, accountId, baseDownloadUrl, accountRequest, - onReceiveController - ); - }); + onReceiveController); - yield Right(DownloadAttachmentForWebSuccess( - taskId, - attachment, - bytesDownloaded)); + yield Right(DownloadAttachmentForWebSuccess( + taskId, + attachment, + bytesDownloaded)); + } else { + yield Left(DownloadAttachmentForWebFailure(taskId, null)); + } } catch (exception) { yield Left(DownloadAttachmentForWebFailure( taskId, diff --git a/lib/features/email/domain/usecases/download_attachments_interactor.dart b/lib/features/email/domain/usecases/download_attachments_interactor.dart index 4b1c56556..312a09359 100644 --- a/lib/features/email/domain/usecases/download_attachments_interactor.dart +++ b/lib/features/email/domain/usecases/download_attachments_interactor.dart @@ -32,39 +32,36 @@ class DownloadAttachmentsInteractor { String baseDownloadUrl ) async* { try { - final account = await _accountRepository.getCurrentAccount(); + final currentAccount = await _accountRepository.getCurrentAccount(); - log('ExportAttachmentInteractor::execute(): account: $account'); + AccountRequest? accountRequest; - final taskIds = await Future.wait([ - if (account.authenticationType == AuthenticationType.oidc) - _authenticationOIDCRepository.getStoredTokenOIDC(account.id) - else - credentialRepository.getAuthenticationInfoStored() - ], eagerError: true - ).then((List responses) async { - AccountRequest accountRequest; - - if (account.authenticationType == AuthenticationType.oidc) { - final tokenOidc = responses.first as TokenOIDC; + if (currentAccount.authenticationType == AuthenticationType.oidc) { + final tokenOidc = await _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id); + accountRequest = AccountRequest( + token: tokenOidc.toToken(), + authenticationType: AuthenticationType.oidc); + } else { + final authenticationInfoCache = await credentialRepository.getAuthenticationInfoStored(); + if (authenticationInfoCache != null) { accountRequest = AccountRequest( - token: tokenOidc.toToken(), - authenticationType: AuthenticationType.oidc); - } else { - accountRequest = AccountRequest( - userName: responses.first as UserName, - password: responses.last as Password, + userName: UserName(authenticationInfoCache.username), + password: Password(authenticationInfoCache.password), authenticationType: AuthenticationType.basic); } + } - return await emailRepository.downloadAttachments( + if (accountRequest != null) { + final taskIds = await emailRepository.downloadAttachments( attachments, accountId, baseDownloadUrl, accountRequest); - }); - yield Right(DownloadAttachmentsSuccess(taskIds)); + yield Right(DownloadAttachmentsSuccess(taskIds)); + } else { + yield Left(DownloadAttachmentsFailure(null)); + } } catch (exception) { log('DownloadAttachmentsInteractor::execute(): $exception'); if (exception is DownloadAttachmentHasTokenExpiredException) { diff --git a/lib/features/email/domain/usecases/export_attachment_interactor.dart b/lib/features/email/domain/usecases/export_attachment_interactor.dart index 5710372b6..34a1e7608 100644 --- a/lib/features/email/domain/usecases/export_attachment_interactor.dart +++ b/lib/features/email/domain/usecases/export_attachment_interactor.dart @@ -31,39 +31,37 @@ class ExportAttachmentInteractor { CancelToken cancelToken ) async* { try { - final account = await _accountRepository.getCurrentAccount(); + final currentAccount = await _accountRepository.getCurrentAccount(); - log('ExportAttachmentInteractor::execute(): account: $account'); + AccountRequest? accountRequest; - final downloadedResponse = await Future.wait([ - if (account.authenticationType == AuthenticationType.oidc) - _authenticationOIDCRepository.getStoredTokenOIDC(account.id) - else - credentialRepository.getAuthenticationInfoStored() - ], eagerError: true - ).then((List responses) async { - AccountRequest accountRequest; - - if (account.authenticationType == AuthenticationType.oidc) { - final tokenOidc = responses.first as TokenOIDC; + if (currentAccount.authenticationType == AuthenticationType.oidc) { + final tokenOidc = await _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id); + accountRequest = AccountRequest( + token: tokenOidc.toToken(), + authenticationType: AuthenticationType.oidc); + } else { + final authenticationInfoCache = await credentialRepository.getAuthenticationInfoStored(); + if (authenticationInfoCache != null) { accountRequest = AccountRequest( - token: tokenOidc.toToken(), - authenticationType: AuthenticationType.oidc); - } else { - accountRequest = AccountRequest( - userName: responses.first as UserName, - password: responses.last as Password, + userName: UserName(authenticationInfoCache.username), + password: Password(authenticationInfoCache.password), authenticationType: AuthenticationType.basic); } + } - return await emailRepository.exportAttachment( + if (accountRequest != null) { + final downloadedResponse = await emailRepository.exportAttachment( attachment, accountId, baseDownloadUrl, accountRequest, cancelToken); - }); - yield Right(ExportAttachmentSuccess(downloadedResponse)); + + yield Right(ExportAttachmentSuccess(downloadedResponse)); + } else { + yield Left(ExportAttachmentFailure(null)); + } } catch (exception) { log('ExportAttachmentInteractor::execute(): exception: $exception'); yield Left(ExportAttachmentFailure(exception));