Example #1
0
        private void LoadSections(Cipher[] ciphers, CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            var sections = ciphers.GroupBy(c => c.NameGroup.ToUpperInvariant())
                           .Select(g => new Section <Cipher>(g.ToList(), g.Key));

            ct.ThrowIfCancellationRequested();
            Device.BeginInvokeOnMainThread(() =>
            {
                PresentationSections.ResetWithRange(sections);
                if (PresentationSections.Count > 0 || !string.IsNullOrWhiteSpace(Search.Text))
                {
                    Content = ResultsStackLayout;

                    if (string.IsNullOrWhiteSpace(_uri) && !_folder && string.IsNullOrWhiteSpace(_folderId) &&
                        string.IsNullOrWhiteSpace(_collectionId) && !_favorites)
                    {
                        Search.Focus();
                    }
                }
                else if (_syncService.SyncInProgress)
                {
                    Content = LoadingIndicator;
                }
                else
                {
                    Content = NoDataStackLayout;
                }
            });
        }
Example #2
0
        private CancellationTokenSource FetchAndLoadVault()
        {
            var cts = new CancellationTokenSource();

            _filterResultsCancellationTokenSource?.Cancel();

            Task.Run(async() =>
            {
                var sections        = new List <Section <Grouping> >();
                var ciphers         = await _cipherService.GetAllAsync();
                var collectionsDict = (await _collectionService.GetAllCipherAssociationsAsync())
                                      .GroupBy(c => c.Item2).ToDictionary(g => g.Key, v => v.ToList());

                var folderCounts = new Dictionary <string, int> {
                    ["none"] = 0
                };
                foreach (var cipher in ciphers)
                {
                    if (cipher.FolderId != null)
                    {
                        if (!folderCounts.ContainsKey(cipher.FolderId))
                        {
                            folderCounts.Add(cipher.FolderId, 0);
                        }
                        folderCounts[cipher.FolderId]++;
                    }
                    else
                    {
                        folderCounts["none"]++;
                    }
                }

                var folders         = await _folderService.GetAllAsync();
                var folderGroupings = folders?
                                      .Select(f => new Grouping(f, folderCounts.ContainsKey(f.Id) ? folderCounts[f.Id] : 0))
                                      .OrderBy(g => g.Name).ToList();
                folderGroupings.Add(new Grouping(AppResources.FolderNone, folderCounts["none"]));
                sections.Add(new Section <Grouping>(folderGroupings, AppResources.Folders));

                var collections         = await _collectionService.GetAllAsync();
                var collectionGroupings = collections?
                                          .Select(c => new Grouping(c,
                                                                    collectionsDict.ContainsKey(c.Id) ? collectionsDict[c.Id].Count() : 0))
                                          .OrderBy(g => g.Name).ToList();
                if (collectionGroupings?.Any() ?? false)
                {
                    sections.Add(new Section <Grouping>(collectionGroupings, AppResources.Collections));
                }

                Device.BeginInvokeOnMainThread(() =>
                {
                    PresentationSections.ResetWithRange(sections);

                    if (ciphers.Any() || folders.Any())
                    {
                        Content = ListView;
                    }
                    else if (_syncService.SyncInProgress)
                    {
                        Content = LoadingIndicator;
                    }
                    else
                    {
                        Content = NoDataStackLayout;
                    }
                });
            }, cts.Token);

            return(cts);
        }
        private CancellationTokenSource FetchAndLoadVault()
        {
            var cts = new CancellationTokenSource();

            _filterResultsCancellationTokenSource?.Cancel();

            Task.Run(async() =>
            {
                var sections = new List <Section <GroupingOrCipher> >();
                var favoriteCipherGroupings = new List <GroupingOrCipher>();
                var noFolderCipherGroupings = new List <GroupingOrCipher>();
                var ciphers         = await _cipherService.GetAllAsync();
                var collectionsDict = (await _collectionService.GetAllCipherAssociationsAsync())
                                      .GroupBy(c => c.Item2).ToDictionary(g => g.Key, v => v.ToList());

                var folderCounts = new Dictionary <string, int>();
                foreach (var cipher in ciphers)
                {
                    if (cipher.Favorite)
                    {
                        favoriteCipherGroupings.Add(new GroupingOrCipher(new Cipher(cipher, _appSettingsService)));
                    }

                    if (cipher.FolderId != null)
                    {
                        if (!folderCounts.ContainsKey(cipher.FolderId))
                        {
                            folderCounts.Add(cipher.FolderId, 0);
                        }
                        folderCounts[cipher.FolderId]++;
                    }
                    else
                    {
                        noFolderCipherGroupings.Add(new GroupingOrCipher(new Cipher(cipher, _appSettingsService)));
                    }
                }

                if (favoriteCipherGroupings.Any())
                {
                    sections.Add(new Section <GroupingOrCipher>(
                                     favoriteCipherGroupings.OrderBy(g => g.Cipher.Name).ThenBy(g => g.Cipher.Subtitle).ToList(),
                                     AppResources.Favorites));
                }

                var folders     = await _folderService.GetAllAsync();
                var collections = await _collectionService.GetAllAsync();

                var fGroupings = folders
                                 .Select(f => new Grouping(f, folderCounts.ContainsKey(f.Id) ? folderCounts[f.Id] : 0))
                                 .OrderBy(g => g.Name);
                var folderGroupings = Helpers.GetAllNested(fGroupings)
                                      .Select(n => new GroupingOrCipher(n)).ToList();

                if (collections.Any() || noFolderCipherGroupings.Count >= 100)
                {
                    var noneFolderGrouping = new Grouping(AppResources.FolderNone, noFolderCipherGroupings.Count);
                    var noneFolderNode     = new Bit.App.Models.TreeNode <Grouping>(noneFolderGrouping,
                                                                                    noneFolderGrouping.Name, null);
                    folderGroupings.Add(new GroupingOrCipher(noneFolderNode));
                }

                if (folderGroupings.Any())
                {
                    sections.Add(new Section <GroupingOrCipher>(folderGroupings, AppResources.Folders));
                }

                var cGroupings = collections
                                 .Select(c => new Grouping(c, collectionsDict.ContainsKey(c.Id) ? collectionsDict[c.Id].Count() : 0))
                                 .OrderBy(g => g.Name);
                var collectionGroupings = Helpers.GetAllNested(cGroupings)
                                          .Select(n => new GroupingOrCipher(n)).ToList();

                if (collectionGroupings.Any())
                {
                    sections.Add(new Section <GroupingOrCipher>(collectionGroupings, AppResources.Collections));
                }
                else if (noFolderCipherGroupings.Count > 0 && noFolderCipherGroupings.Count < 100)
                {
                    sections.Add(new Section <GroupingOrCipher>(
                                     noFolderCipherGroupings.OrderBy(g => g.Cipher.Name).ThenBy(g => g.Cipher.Subtitle).ToList(),
                                     AppResources.FolderNone));
                }

                Device.BeginInvokeOnMainThread(() =>
                {
                    PresentationSections.ResetWithRange(sections);

                    if (ciphers.Any() || folders.Any())
                    {
                        ContentView.Content = ListView;
                    }
                    else if (_syncService.SyncInProgress)
                    {
                        ContentView.Content = LoadingIndicator;
                    }
                    else
                    {
                        ContentView.Content = NoDataStackLayout;
                    }
                });
            }, cts.Token);

            return(cts);
        }