From 34fd9b58423303addab6eb83c6b6b9ac163c28e7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 10 May 2019 14:33:33 -0400 Subject: [PATCH] stub attachments page --- src/App/App.csproj | 3 + src/App/Pages/Vault/AttachmentsPage.xaml | 57 ++++++++++++ src/App/Pages/Vault/AttachmentsPage.xaml.cs | 37 ++++++++ .../Pages/Vault/AttachmentsPageViewModel.cs | 87 +++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 src/App/Pages/Vault/AttachmentsPage.xaml create mode 100644 src/App/Pages/Vault/AttachmentsPage.xaml.cs create mode 100644 src/App/Pages/Vault/AttachmentsPageViewModel.cs diff --git a/src/App/App.csproj b/src/App/App.csproj index d1cb1ead4..45f805654 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -38,6 +38,9 @@ GeneratorPage.xaml + + AttachmentsPage.xaml + CollectionsPage.xaml diff --git a/src/App/Pages/Vault/AttachmentsPage.xaml b/src/App/Pages/Vault/AttachmentsPage.xaml new file mode 100644 index 000000000..a5f8f752e --- /dev/null +++ b/src/App/Pages/Vault/AttachmentsPage.xaml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/App/Pages/Vault/AttachmentsPage.xaml.cs b/src/App/Pages/Vault/AttachmentsPage.xaml.cs new file mode 100644 index 000000000..b237375ef --- /dev/null +++ b/src/App/Pages/Vault/AttachmentsPage.xaml.cs @@ -0,0 +1,37 @@ +using Xamarin.Forms; + +namespace Bit.App.Pages +{ + public partial class AttachmentsPage : BaseContentPage + { + private AttachmentsPageViewModel _vm; + + public AttachmentsPage(string cipherId) + { + InitializeComponent(); + _vm = BindingContext as AttachmentsPageViewModel; + _vm.Page = this; + _vm.CipherId = cipherId; + SetActivityIndicator(); + } + + protected override async void OnAppearing() + { + base.OnAppearing(); + await LoadOnAppearedAsync(_scrollView, true, () => _vm.LoadAsync()); + } + + protected override void OnDisappearing() + { + base.OnDisappearing(); + } + + private async void Save_Clicked(object sender, System.EventArgs e) + { + if(DoOnce()) + { + await _vm.SubmitAsync(); + } + } + } +} diff --git a/src/App/Pages/Vault/AttachmentsPageViewModel.cs b/src/App/Pages/Vault/AttachmentsPageViewModel.cs new file mode 100644 index 000000000..4f5af4672 --- /dev/null +++ b/src/App/Pages/Vault/AttachmentsPageViewModel.cs @@ -0,0 +1,87 @@ +using Bit.App.Abstractions; +using Bit.App.Resources; +using Bit.Core.Abstractions; +using Bit.Core.Exceptions; +using Bit.Core.Models.Domain; +using Bit.Core.Models.View; +using Bit.Core.Utilities; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Bit.App.Pages +{ + public class AttachmentsPageViewModel : BaseViewModel + { + private readonly IDeviceActionService _deviceActionService; + private readonly ICipherService _cipherService; + private readonly ICollectionService _collectionService; + private readonly IPlatformUtilsService _platformUtilsService; + private CipherView _cipher; + private Cipher _cipherDomain; + private bool _hasCollections; + + public AttachmentsPageViewModel() + { + _deviceActionService = ServiceContainer.Resolve("deviceActionService"); + _cipherService = ServiceContainer.Resolve("cipherService"); + _platformUtilsService = ServiceContainer.Resolve("platformUtilsService"); + _collectionService = ServiceContainer.Resolve("collectionService"); + Collections = new ExtendedObservableCollection(); + PageTitle = AppResources.Collections; + } + + public string CipherId { get; set; } + public ExtendedObservableCollection Collections { get; set; } + public bool HasCollections + { + get => _hasCollections; + set => SetProperty(ref _hasCollections, value); + } + + public async Task LoadAsync() + { + _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 + { + Collection = c, + Checked = collectionIds.Contains(c.Id) + }).ToList(); + Collections.ResetWithRange(collections); + HasCollections = Collections.Any(); + } + + public async Task SubmitAsync() + { + if(!Collections.Any(c => c.Checked)) + { + await Page.DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.SelectOneCollection, + AppResources.Ok); + return false; + } + + _cipherDomain.CollectionIds = new HashSet( + Collections.Where(c => c.Checked).Select(c => c.Collection.Id)); + try + { + await _deviceActionService.ShowLoadingAsync(AppResources.Saving); + await _cipherService.SaveCollectionsWithServerAsync(_cipherDomain); + await _deviceActionService.HideLoadingAsync(); + _platformUtilsService.ShowToast("success", null, AppResources.ItemUpdated); + await Page.Navigation.PopModalAsync(); + return true; + } + catch(ApiException e) + { + await _deviceActionService.HideLoadingAsync(); + await Page.DisplayAlert(AppResources.AnErrorHasOccurred, e.Error.GetSingleMessage(), AppResources.Ok); + } + return false; + } + } +}