From c4161ac5a50702c8e20686f70f4af3d4af6832ed Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 18 May 2016 20:29:03 -0400 Subject: [PATCH] Extended observable collection for bulk addrange --- src/App/App.csproj | 1 + src/App/Models/Page/VaultListPageModel.cs | 12 +++---- src/App/Pages/VaultListSitesPage.cs | 17 ++++------ .../Utilities/ExtendedObservableCollection.cs | 33 +++++++++++++++++++ 4 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 src/App/Utilities/ExtendedObservableCollection.cs diff --git a/src/App/App.csproj b/src/App/App.csproj index 8db515103..fcb66e696 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -124,6 +124,7 @@ + diff --git a/src/App/Models/Page/VaultListPageModel.cs b/src/App/Models/Page/VaultListPageModel.cs index 4e8092c18..3576aaed8 100644 --- a/src/App/Models/Page/VaultListPageModel.cs +++ b/src/App/Models/Page/VaultListPageModel.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using Bit.App.Resources; +using Bit.App.Utilities; +using System.Linq; namespace Bit.App.Models.Page { @@ -27,14 +28,12 @@ namespace Bit.App.Models.Page public string Uri { get; set; } } - public class Folder : ObservableCollection + public class Folder : List { public Folder(IEnumerable sites, string folderId = null) { - foreach(var site in sites) - { - Items.Add(new Site(site, folderId)); - } + var pageSites = sites.Select(s => new Site(s, folderId)); + AddRange(pageSites); } public Folder(Models.Folder folder, IEnumerable sites) @@ -46,7 +45,6 @@ namespace Bit.App.Models.Page public string Id { get; set; } public string Name { get; set; } = AppResources.FolderNone; - public string FirstLetter { get { return Name.Substring(0, 1); } } } } } diff --git a/src/App/Pages/VaultListSitesPage.cs b/src/App/Pages/VaultListSitesPage.cs index 3f37e4d9b..e5517d8c4 100644 --- a/src/App/Pages/VaultListSitesPage.cs +++ b/src/App/Pages/VaultListSitesPage.cs @@ -9,6 +9,7 @@ using Bit.App.Models.Page; using Bit.App.Resources; using Xamarin.Forms; using XLabs.Ioc; +using Bit.App.Utilities; namespace Bit.App.Pages { @@ -29,7 +30,7 @@ namespace Bit.App.Pages Init(); } - public ObservableCollection Folders { get; private set; } = new ObservableCollection(); + public ExtendedObservableCollection Folders { get; private set; } = new ExtendedObservableCollection(); private void Init() { @@ -59,20 +60,14 @@ namespace Bit.App.Pages private async Task LoadFoldersAsync() { - Folders.Clear(); - var folders = await _folderService.GetAllAsync(); var sites = await _siteService.GetAllAsync(); - foreach(var folder in folders) - { - var f = new VaultListPageModel.Folder(folder, sites.Where(s => s.FolderId == folder.Id)); - Folders.Add(f); - } - - // add the sites with no folder + var pageFolders = folders.Select(f => new VaultListPageModel.Folder(f, sites.Where(s => s.FolderId == f.Id))).ToList(); var noneFolder = new VaultListPageModel.Folder(sites.Where(s => s.FolderId == null)); - Folders.Add(noneFolder); + pageFolders.Add(noneFolder); + + Folders.Reset(pageFolders); } private void SiteSelected(object sender, SelectedItemChangedEventArgs e) diff --git a/src/App/Utilities/ExtendedObservableCollection.cs b/src/App/Utilities/ExtendedObservableCollection.cs new file mode 100644 index 000000000..191161c8d --- /dev/null +++ b/src/App/Utilities/ExtendedObservableCollection.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; + +namespace Bit.App.Utilities +{ + public class ExtendedObservableCollection : ObservableCollection + { + public ExtendedObservableCollection() : base() { } + public ExtendedObservableCollection(IEnumerable collection) : base(collection) { } + public ExtendedObservableCollection(List list) : base(list) { } + + public void AddRange(IEnumerable range) + { + foreach(var item in range) + { + Items.Add(item); + } + + OnPropertyChanged(new PropertyChangedEventArgs("Count")); + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + } + + public void Reset(IEnumerable range) + { + Items.Clear(); + AddRange(range); + } + } +}