mirror of
https://github.com/bitwarden/mobile
synced 2025-12-20 10:13:42 +00:00
attachments page
This commit is contained in:
@@ -8,6 +8,7 @@ using Bit.Core.Utilities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Bit.App.Pages
|
||||
{
|
||||
@@ -15,65 +16,103 @@ namespace Bit.App.Pages
|
||||
{
|
||||
private readonly IDeviceActionService _deviceActionService;
|
||||
private readonly ICipherService _cipherService;
|
||||
private readonly ICollectionService _collectionService;
|
||||
private readonly ICryptoService _cryptoService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IPlatformUtilsService _platformUtilsService;
|
||||
private CipherView _cipher;
|
||||
private Cipher _cipherDomain;
|
||||
private bool _hasCollections;
|
||||
private bool _hasAttachments;
|
||||
private bool _hasUpdatedKey;
|
||||
private bool _canAccessAttachments;
|
||||
private string _fileName;
|
||||
|
||||
public AttachmentsPageViewModel()
|
||||
{
|
||||
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
||||
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
|
||||
_cryptoService = ServiceContainer.Resolve<ICryptoService>("cryptoService");
|
||||
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
|
||||
_collectionService = ServiceContainer.Resolve<ICollectionService>("collectionService");
|
||||
Collections = new ExtendedObservableCollection<CollectionViewModel>();
|
||||
PageTitle = AppResources.Collections;
|
||||
_userService = ServiceContainer.Resolve<IUserService>("userService");
|
||||
Attachments = new ExtendedObservableCollection<AttachmentView>();
|
||||
DeleteAttachmentCommand = new Command<AttachmentView>(DeleteAsync);
|
||||
PageTitle = AppResources.Attachments;
|
||||
}
|
||||
|
||||
public string CipherId { get; set; }
|
||||
public ExtendedObservableCollection<CollectionViewModel> Collections { get; set; }
|
||||
public bool HasCollections
|
||||
public CipherView Cipher
|
||||
{
|
||||
get => _hasCollections;
|
||||
set => SetProperty(ref _hasCollections, value);
|
||||
get => _cipher;
|
||||
set => SetProperty(ref _cipher, value);
|
||||
}
|
||||
public ExtendedObservableCollection<AttachmentView> Attachments { get; set; }
|
||||
public bool HasAttachments
|
||||
{
|
||||
get => _hasAttachments;
|
||||
set => SetProperty(ref _hasAttachments, value);
|
||||
}
|
||||
public string FileName
|
||||
{
|
||||
get => _fileName;
|
||||
set => SetProperty(ref _fileName, value);
|
||||
}
|
||||
public byte[] FileData { get; set; }
|
||||
public Command DeleteAttachmentCommand { get; set; }
|
||||
|
||||
public async Task LoadAsync()
|
||||
public async Task InitAsync()
|
||||
{
|
||||
_cipherDomain = await _cipherService.GetAsync(CipherId);
|
||||
var collectionIds = _cipherDomain.CollectionIds;
|
||||
_cipher = await _cipherDomain.DecryptAsync();
|
||||
var allCollections = await _collectionService.GetAllDecryptedAsync();
|
||||
var collections = allCollections
|
||||
.Where(c => !c.ReadOnly && c.OrganizationId == _cipher.OrganizationId)
|
||||
.Select(c => new CollectionViewModel
|
||||
Cipher = await _cipherDomain.DecryptAsync();
|
||||
LoadAttachments();
|
||||
_hasUpdatedKey = await _cryptoService.HasEncKeyAsync();
|
||||
var canAccessPremium = await _userService.CanAccessPremiumAsync();
|
||||
_canAccessAttachments = canAccessPremium || Cipher.OrganizationId != null;
|
||||
if(!_canAccessAttachments)
|
||||
{
|
||||
await _platformUtilsService.ShowDialogAsync(AppResources.PremiumRequired);
|
||||
}
|
||||
else if(!_hasUpdatedKey)
|
||||
{
|
||||
var confirmed = await _platformUtilsService.ShowDialogAsync(AppResources.UpdateKey,
|
||||
AppResources.FeatureUnavailable, AppResources.LearnMore, AppResources.Cancel);
|
||||
if(confirmed)
|
||||
{
|
||||
Collection = c,
|
||||
Checked = collectionIds.Contains(c.Id)
|
||||
}).ToList();
|
||||
Collections.ResetWithRange(collections);
|
||||
HasCollections = Collections.Any();
|
||||
_platformUtilsService.LaunchUri("https://help.bitwarden.com/article/update-encryption-key/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> SubmitAsync()
|
||||
{
|
||||
if(!Collections.Any(c => c.Checked))
|
||||
if(!_hasUpdatedKey)
|
||||
{
|
||||
await Page.DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.SelectOneCollection,
|
||||
AppResources.Ok);
|
||||
await _platformUtilsService.ShowDialogAsync(AppResources.UpdateKey,
|
||||
AppResources.AnErrorHasOccurred);
|
||||
return false;
|
||||
}
|
||||
if(FileData == null)
|
||||
{
|
||||
await _platformUtilsService.ShowDialogAsync(
|
||||
string.Format(AppResources.ValidationFieldRequired, AppResources.File),
|
||||
AppResources.AnErrorHasOccurred);
|
||||
return false;
|
||||
}
|
||||
if(FileData.Length > 104857600) // 100 MB
|
||||
{
|
||||
await _platformUtilsService.ShowDialogAsync(AppResources.MaxFileSize,
|
||||
AppResources.AnErrorHasOccurred);
|
||||
return false;
|
||||
}
|
||||
|
||||
_cipherDomain.CollectionIds = new HashSet<string>(
|
||||
Collections.Where(c => c.Checked).Select(c => c.Collection.Id));
|
||||
try
|
||||
{
|
||||
await _deviceActionService.ShowLoadingAsync(AppResources.Saving);
|
||||
await _cipherService.SaveCollectionsWithServerAsync(_cipherDomain);
|
||||
_cipherDomain = await _cipherService.SaveAttachmentRawWithServerAsync(
|
||||
_cipherDomain, FileName, FileData);
|
||||
Cipher = await _cipherDomain.DecryptAsync();
|
||||
await _deviceActionService.HideLoadingAsync();
|
||||
_platformUtilsService.ShowToast("success", null, AppResources.ItemUpdated);
|
||||
await Page.Navigation.PopModalAsync();
|
||||
_platformUtilsService.ShowToast("success", null, AppResources.AttachementAdded);
|
||||
LoadAttachments();
|
||||
FileData = null;
|
||||
FileName = null;
|
||||
return true;
|
||||
}
|
||||
catch(ApiException e)
|
||||
@@ -83,5 +122,44 @@ namespace Bit.App.Pages
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task ChooseFileAsync()
|
||||
{
|
||||
await _deviceActionService.SelectFileAsync();
|
||||
}
|
||||
|
||||
private async void DeleteAsync(AttachmentView attachment)
|
||||
{
|
||||
var confirmed = await _platformUtilsService.ShowDialogAsync(AppResources.DoYouReallyWantToDelete,
|
||||
null, AppResources.Yes, AppResources.No);
|
||||
if(!confirmed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await _deviceActionService.ShowLoadingAsync(AppResources.Deleting);
|
||||
await _cipherService.DeleteAttachmentWithServerAsync(Cipher.Id, attachment.Id);
|
||||
await _deviceActionService.HideLoadingAsync();
|
||||
_platformUtilsService.ShowToast("success", null, AppResources.AttachmentDeleted);
|
||||
var attachmentToRemove = Cipher.Attachments.FirstOrDefault(a => a.Id == attachment.Id);
|
||||
if(attachmentToRemove != null)
|
||||
{
|
||||
Cipher.Attachments.Remove(attachmentToRemove);
|
||||
LoadAttachments();
|
||||
}
|
||||
}
|
||||
catch(ApiException e)
|
||||
{
|
||||
await _deviceActionService.HideLoadingAsync();
|
||||
await Page.DisplayAlert(AppResources.AnErrorHasOccurred, e.Error.GetSingleMessage(), AppResources.Ok);
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadAttachments()
|
||||
{
|
||||
Attachments.ResetWithRange(Cipher.Attachments ?? new List<AttachmentView>());
|
||||
HasAttachments = Cipher.HasAttachments;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user