private async Task SyncSideBarItemsUI()
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                await MainPage.SideBarItemsSemaphore.WaitAsync();
                try
                {
                    MainPage.SideBarItems.BeginBulkOperation();

                    var drivesSnapshot = Drives.ToList();
                    var drivesSection  = MainPage.SideBarItems.FirstOrDefault(x => x is HeaderTextItem && x.Text == "SidebarNetworkDrives".GetLocalized());

                    if (drivesSection != null && drivesSnapshot.Count == 0)
                    {
                        //No drives - remove the header
                        MainPage.SideBarItems.Remove(drivesSection);
                    }

                    if (drivesSection == null && drivesSnapshot.Count > 0)
                    {
                        drivesSection = new HeaderTextItem()
                        {
                            Text = "SidebarNetworkDrives".GetLocalized()
                        };

                        MainPage.SideBarItems.Add(drivesSection);
                    }

                    var sectionStartIndex = MainPage.SideBarItems.IndexOf(drivesSection);
                    var insertAt          = sectionStartIndex + 1;

                    //Remove all existing network drives from the sidebar
                    while (insertAt < MainPage.SideBarItems.Count)
                    {
                        var item = MainPage.SideBarItems[insertAt];
                        if (item.ItemType != NavigationControlItemType.Drive)
                        {
                            break;
                        }
                        MainPage.SideBarItems.Remove(item);
                    }

                    //Add all network drives to the sidebar
                    foreach (var drive in drivesSnapshot
                             .OrderByDescending(o => string.Equals(o.Text, "Network".GetLocalized(), StringComparison.OrdinalIgnoreCase))
                             .ThenBy(o => o.Text))
                    {
                        MainPage.SideBarItems.Insert(insertAt, drive);
                        insertAt++;
                    }
                    MainPage.SideBarItems.EndBulkOperation();
                }
                finally
                {
                    MainPage.SideBarItemsSemaphore.Release();
                }
            });
        }
Exemple #2
0
        private async Task SyncSideBarItemsUI()
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                lock (MainPage.SideBarItems)
                {
                    var drivesSection = MainPage.SideBarItems.FirstOrDefault(x => x is HeaderTextItem && x.Text == "SidebarCloudDrives".GetLocalized());

                    if (drivesSection != null && Drives.Count == 0)
                    {
                        //No drives - remove the header
                        MainPage.SideBarItems.Remove(drivesSection);
                    }

                    if (drivesSection == null && Drives.Count > 0)
                    {
                        drivesSection = new HeaderTextItem()
                        {
                            Text = "SidebarCloudDrives".GetLocalized()
                        };

                        //Get the last location item in the sidebar
                        var lastLocationItem = MainPage.SideBarItems.LastOrDefault(x => x is LocationItem);

                        if (lastLocationItem != null)
                        {
                            //Get the index of the last location item
                            var lastLocationItemIndex = MainPage.SideBarItems.IndexOf(lastLocationItem);
                            //Insert the drives title beneath it
                            MainPage.SideBarItems.Insert(lastLocationItemIndex + 1, drivesSection);
                        }
                        else
                        {
                            MainPage.SideBarItems.Add(drivesSection);
                        }
                    }

                    var sectionStartIndex = MainPage.SideBarItems.IndexOf(drivesSection);

                    //Remove all existing cloud drives from the sidebar
                    foreach (var item in MainPage.SideBarItems
                             .Where(x => x.ItemType == NavigationControlItemType.CloudDrive)
                             .ToList())
                    {
                        MainPage.SideBarItems.Remove(item);
                    }

                    //Add all cloud drives to the sidebar
                    var insertAt = sectionStartIndex + 1;
                    foreach (var drive in Drives.OrderBy(o => o.Text))
                    {
                        MainPage.SideBarItems.Insert(insertAt, drive);
                        insertAt++;
                    }
                }
            });
        }