Browse Source

PM-2149 Added cancellation support to upload attachment, which also includes adding cancellation to SendAsync in the ApiService

github-services/pull/2533/head
Federico Maccaroni 3 years ago
parent
commit
07b428f478
No known key found for this signature in database
GPG Key ID: 5D233F8F2B034536
  1. 14
      src/Android/Services/DeviceActionService.cs
  2. 5
      src/App/Abstractions/IDeviceActionService.cs
  3. 15
      src/App/Pages/Vault/AttachmentsPageViewModel.cs
  4. 45
      src/App/Resources/AppResources.Designer.cs
  5. 3
      src/App/Resources/AppResources.resx
  6. 8
      src/Core/Services/ApiService.cs
  7. 11
      src/iOS.Core/Services/DeviceActionService.cs

14
src/Android/Services/DeviceActionService.cs

@ -83,7 +83,7 @@ namespace Bit.Droid.Services @@ -83,7 +83,7 @@ namespace Bit.Droid.Services
return launchIntentSender != null;
}
public async Task ShowLoadingAsync(string text)
public async Task ShowLoadingAsync(string text, System.Threading.CancellationTokenSource cts = null, string cancelButtonText = null)
{
if (_progressDialog != null)
{
@ -98,10 +98,16 @@ namespace Bit.Droid.Services @@ -98,10 +98,16 @@ namespace Bit.Droid.Services
txtLoading.Text = text;
txtLoading.SetTextColor(ThemeHelpers.TextColor);
_progressDialog = new AlertDialog.Builder(activity)
var progressDialogBuilder = new AlertDialog.Builder(activity)
.SetView(dialogView)
.SetCancelable(false)
.Create();
.SetCancelable(cts != null);
if (cts != null)
{
progressDialogBuilder.SetNegativeButton(cancelButtonText ?? AppResources.Cancel, (sender, args) => cts?.Cancel());
}
_progressDialog = progressDialogBuilder.Create();
_progressDialog.Show();
}

5
src/App/Abstractions/IDeviceActionService.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Bit.Core.Enums;
using Bit.Core.Models;
@ -13,7 +14,7 @@ namespace Bit.App.Abstractions @@ -13,7 +14,7 @@ namespace Bit.App.Abstractions
string GetBuildNumber();
void Toast(string text, bool longDuration = false);
Task ShowLoadingAsync(string text);
Task ShowLoadingAsync(string text, CancellationTokenSource cts = null, string cancelButtonText = null);
Task HideLoadingAsync();
Task<string> DisplayPromptAync(string title = null, string description = null, string text = null,
string okButtonText = null, string cancelButtonText = null, bool numericKeyboard = false,

15
src/App/Pages/Vault/AttachmentsPageViewModel.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Bit.App.Abstractions;
@ -31,6 +32,7 @@ namespace Bit.App.Pages @@ -31,6 +32,7 @@ namespace Bit.App.Pages
private bool _hasUpdatedKey;
private bool _canAccessAttachments;
private string _fileName;
private CancellationTokenSource _uploadCts;
public AttachmentsPageViewModel()
{
@ -119,11 +121,15 @@ namespace Bit.App.Pages @@ -119,11 +121,15 @@ namespace Bit.App.Pages
AppResources.AnErrorHasOccurred);
return false;
}
_uploadCts = new CancellationTokenSource();
var uploadCts = _uploadCts;
try
{
await _deviceActionService.ShowLoadingAsync(AppResources.Saving);
await _deviceActionService.ShowLoadingAsync(AppResources.Saving, uploadCts);
_cipherDomain = await _cipherService.SaveAttachmentRawWithServerAsync(
_cipherDomain, FileName, FileData);
_cipherDomain, FileName, FileData, uploadCts.Token);
Cipher = await _cipherDomain.DecryptAsync();
await _deviceActionService.HideLoadingAsync();
_platformUtilsService.ShowToast("success", null, AppResources.AttachementAdded);
@ -132,6 +138,11 @@ namespace Bit.App.Pages @@ -132,6 +138,11 @@ namespace Bit.App.Pages
FileName = null;
return true;
}
catch (OperationCanceledException)
{
await _deviceActionService.HideLoadingAsync();
await _platformUtilsService.ShowDialogAsync(AppResources.UploadHasBeenCanceled, AppResources.Attachments);
}
catch (ApiException e)
{
_logger.Exception(e);

45
src/App/Resources/AppResources.Designer.cs generated

@ -202,6 +202,24 @@ namespace Bit.App.Resources { @@ -202,6 +202,24 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Biometric unlock for this account is disabled pending verification of master password..
/// </summary>
public static string AccountBiometricInvalidated {
get {
return ResourceManager.GetString("AccountBiometricInvalidated", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Autofill biometric unlock for this account is disabled pending verification of master password..
/// </summary>
public static string AccountBiometricInvalidatedExtension {
get {
return ResourceManager.GetString("AccountBiometricInvalidatedExtension", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Your new account has been created! You may now log in..
/// </summary>
@ -967,24 +985,6 @@ namespace Bit.App.Resources { @@ -967,24 +985,6 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Biometric unlock disabled pending verification of master password..
/// </summary>
public static string AccountBiometricInvalidated {
get {
return ResourceManager.GetString("AccountBiometricInvalidated", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Biometric unlock for autofill disabled pending verification of master password..
/// </summary>
public static string AccountBiometricInvalidatedExtension {
get {
return ResourceManager.GetString("AccountBiometricInvalidatedExtension", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Biometrics.
/// </summary>
@ -6515,6 +6515,15 @@ namespace Bit.App.Resources { @@ -6515,6 +6515,15 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Upload has been canceled.
/// </summary>
public static string UploadHasBeenCanceled {
get {
return ResourceManager.GetString("UploadHasBeenCanceled", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Uppercase (A to Z).
/// </summary>

3
src/App/Resources/AppResources.resx

@ -2631,4 +2631,7 @@ Do you want to switch to this account?</value> @@ -2631,4 +2631,7 @@ Do you want to switch to this account?</value>
<data name="CurrentMasterPassword" xml:space="preserve">
<value>Current master password</value>
</data>
<data name="UploadHasBeenCanceled" xml:space="preserve">
<value>Upload has been canceled</value>
</data>
</root>

8
src/Core/Services/ApiService.cs

@ -699,6 +699,14 @@ namespace Bit.Core.Services @@ -699,6 +699,14 @@ namespace Bit.Core.Services
{
response = await _httpClient.SendAsync(requestMessage, cancellationToken);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex) when (ex.Message?.Contains("Socket closed") == true)
{
throw new OperationCanceledException();
}
catch (Exception e)
{
throw new ApiException(HandleWebError(e));

11
src/iOS.Core/Services/DeviceActionService.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Resources;
@ -61,7 +62,7 @@ namespace Bit.iOS.Core.Services @@ -61,7 +62,7 @@ namespace Bit.iOS.Core.Services
};
}
public Task ShowLoadingAsync(string text)
public Task ShowLoadingAsync(string text, CancellationTokenSource cts = null, string cancelButtonText = null)
{
if (_progressAlert != null)
{
@ -84,6 +85,14 @@ namespace Bit.iOS.Core.Services @@ -84,6 +85,14 @@ namespace Bit.iOS.Core.Services
_progressAlert = UIAlertController.Create(null, text, UIAlertControllerStyle.Alert);
_progressAlert.View.TintColor = UIColor.Black;
_progressAlert.View.Add(loadingIndicator);
if (cts != null)
{
_progressAlert.AddAction(UIAlertAction.Create(cancelButtonText ?? AppResources.Cancel, UIAlertActionStyle.Cancel, x =>
{
cts.Cancel();
result.TrySetResult(0);
}));
}
vc.PresentViewController(_progressAlert, false, () => result.TrySetResult(0));
return result.Task;

Loading…
Cancel
Save