Skip to content

Notify when pinned items config changes #8712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Files.Uwp/Controllers/SidebarPinnedController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Files.DataModels;
using Files.Shared.Enums;
using Files.Filesystem;
using Microsoft.Toolkit.Uwp;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Storage;
using Windows.Storage.Search;
using Files.Shared.Extensions;

namespace Files.Controllers
Expand All @@ -15,6 +18,10 @@ public class SidebarPinnedController : IJson
{
public SidebarPinnedModel Model { get; set; }

private StorageFileQueryResult query;

private bool suppressChangeEvent;

public string JsonFileName { get; } = "PinnedItems.json";

private string folderPath => Path.Combine(ApplicationData.Current.LocalFolder.Path, "settings");
Expand All @@ -28,6 +35,7 @@ public SidebarPinnedController()
public async Task InitializeAsync()
{
await LoadAsync();
await StartWatchConfigChangeAsync();
}

public async Task ReloadAsync()
Expand Down Expand Up @@ -99,8 +107,36 @@ private async Task LoadAsync()
await Model.AddAllItemsToSidebar();
}

private async Task StartWatchConfigChangeAsync()
{
var queryOptions = new QueryOptions();
queryOptions.ApplicationSearchFilter = "System.FileName:" + JsonFileName;

var settingsFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("settings");
query = settingsFolder.CreateFileQueryWithOptions(queryOptions);
query.ContentsChanged += Query_ContentsChanged;
await query.GetFilesAsync();
}

private async void Query_ContentsChanged(IStorageQueryResultBase sender, object args)
{
if (suppressChangeEvent)
{
suppressChangeEvent = false;
return;
}

// watched file changed externally, reload the sidebar items
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async () =>
{
await ReloadAsync();
});
}


public void SaveModel()
{
suppressChangeEvent = true;
try
{
using (var file = File.CreateText(Path.Combine(folderPath, JsonFileName)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.ObjectModel;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Files.Helpers;

namespace Files.DataModels.NavigationControlItems
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public string Path
public FontFamily Font { get; set; }
public NavigationControlItemType ItemType => NavigationControlItemType.Location;
public bool IsDefaultLocation { get; set; }
public ObservableCollection<INavigationControlItem> ChildItems { get; set; }
public BulkConcurrentObservableCollection<INavigationControlItem> ChildItems { get; set; }

public bool SelectsOnInvoked { get; set; } = true;

Expand Down
46 changes: 28 additions & 18 deletions src/Files.Uwp/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -98,11 +97,20 @@ public List<string> GetItems()
/// <param name="item">Item to remove</param>
public async void AddItem(string item)
{
if (!string.IsNullOrEmpty(item) && !FavoriteItems.Contains(item))
await SidebarControl.SideBarItemsSemaphore.WaitAsync();

try
{
FavoriteItems.Add(item);
await AddItemToSidebarAsync(item);
Save();
if (!string.IsNullOrEmpty(item) && !FavoriteItems.Contains(item))
{
FavoriteItems.Add(item);
await AddItemToSidebarAsync(item);
Save();
}
}
finally
{
SidebarControl.SideBarItemsSemaphore.Release();
}
}

Expand Down Expand Up @@ -331,7 +339,7 @@ public async Task AddAllItemsToSidebar()
IsDefaultLocation = true,
Icon = await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => new BitmapImage(new Uri("ms-appx:///Assets/FluentIcons/Home.png"))),
Path = "Home".GetLocalized(),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
favoriteSection ??= new LocationItem()
{
Expand All @@ -340,7 +348,7 @@ public async Task AddAllItemsToSidebar()
SelectsOnInvoked = false,
Icon = await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIHelpers.GetIconResource(Constants.Shell32.QuickAccess)),
Font = MainViewModel.FontName,
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};

if (homeSection != null)
Expand All @@ -355,19 +363,19 @@ public async Task AddAllItemsToSidebar()
SidebarControl.SideBarItems.Insert(index, favoriteSection);
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => SidebarControl.SideBarItems.EndBulkOperation());
}

for (int i = 0; i < FavoriteItems.Count; i++)
{
string path = FavoriteItems[i];
await AddItemToSidebarAsync(path);
}

await ShowHideRecycleBinItemAsync(UserSettingsService.AppearanceSettingsService.PinRecycleBinToSidebar);
}
finally
{
SidebarControl.SideBarItemsSemaphore.Release();
}

for (int i = 0; i < FavoriteItems.Count; i++)
{
string path = FavoriteItems[i];
await AddItemToSidebarAsync(path);
}

await ShowHideRecycleBinItemAsync(UserSettingsService.AppearanceSettingsService.PinRecycleBinToSidebar);
}

/// <summary>
Expand All @@ -376,11 +384,13 @@ public async Task AddAllItemsToSidebar()
public void RemoveStaleSidebarItems()
{
// Remove unpinned items from sidebar
for (int i = 0; i < favoriteSection.ChildItems.Count; i++)
// Reverse iteration to avoid skipping elements while removing
for (int i = favoriteSection.ChildItems.Count - 1; i >= 0; i--)
{
if (favoriteSection.ChildItems[i] is LocationItem)
var childItem = favoriteSection.ChildItems[i];
if (childItem is LocationItem)
{
var item = favoriteSection.ChildItems[i] as LocationItem;
var item = childItem as LocationItem;
if (!item.IsDefaultLocation && !FavoriteItems.Contains(item.Path))
{
favoriteSection.ChildItems.RemoveAt(i);
Expand Down
2 changes: 1 addition & 1 deletion src/Files.Uwp/Filesystem/CloudDrivesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
Section = SectionType.CloudDrives,
SelectsOnInvoked = false,
Icon = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/FluentIcons/CloudDrive.png")),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
var index = (SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Favorites) ? 1 : 0) +
(SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Library) ? 1 : 0) +
Expand Down
2 changes: 1 addition & 1 deletion src/Files.Uwp/Filesystem/Drives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
Section = SectionType.Drives,
SelectsOnInvoked = false,
Icon = await UIHelpers.GetIconResource(Constants.ImageRes.ThisPC),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
var index = (SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Favorites) ? 1 : 0) +
(SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Library) ? 1 : 0); // After libraries section
Expand Down
3 changes: 2 additions & 1 deletion src/Files.Uwp/Filesystem/FileTagsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Files.Helpers;

namespace Files.Filesystem
{
Expand Down Expand Up @@ -56,7 +57,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
Section = SectionType.FileTag,
SelectsOnInvoked = false,
Icon = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/FluentIcons/FileTags.png")),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
var index = (SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Favorites) ? 1 : 0) +
(SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Library) ? 1 : 0) +
Expand Down
2 changes: 1 addition & 1 deletion src/Files.Uwp/Filesystem/LibraryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
Section = SectionType.Library,
SelectsOnInvoked = false,
Icon = await UIHelpers.GetIconResource(Constants.ImageRes.Libraries),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
var index = (SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Favorites) ? 1 : 0); // After favorites section
SidebarControl.SideBarItems.BeginBulkOperation();
Expand Down
2 changes: 1 addition & 1 deletion src/Files.Uwp/Filesystem/NetworkDrivesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
Section = SectionType.Network,
SelectsOnInvoked = false,
Icon = await UIHelpers.GetIconResource(Constants.ImageRes.NetworkDrives),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
var index = (SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Favorites) ? 1 : 0) +
(SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Library) ? 1 : 0) +
Expand Down
3 changes: 2 additions & 1 deletion src/Files.Uwp/Filesystem/WSLDistroManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Windows.ApplicationModel.Core;
using Windows.Storage;
using Windows.UI.Core;
using Files.Helpers;

namespace Files.Filesystem
{
Expand Down Expand Up @@ -56,7 +57,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
Section = SectionType.WSL,
SelectsOnInvoked = false,
Icon = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/WSL/genericpng.png")),
ChildItems = new ObservableCollection<INavigationControlItem>()
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
};
var index = (SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Favorites) ? 1 : 0) +
(SidebarControl.SideBarItems.Any(item => item.Section == SectionType.Library) ? 1 : 0) +
Expand Down