Skip to content

Commit 00f2ec4

Browse files
committed
[WIP]
1 parent 11a9b8c commit 00f2ec4

File tree

4 files changed

+135
-154
lines changed

4 files changed

+135
-154
lines changed

src/Files.Uwp/DataModels/NavigationControlItems/LocationItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using CommunityToolkit.Mvvm.ComponentModel;
44
using Microsoft.Toolkit.Uwp;
55
using System;
6-
using System.Collections.ObjectModel;
76
using Windows.UI.Xaml.Media;
87
using Windows.UI.Xaml.Media.Imaging;
98
using Files.Uwp.Helpers;
@@ -20,7 +19,7 @@ public BitmapImage Icon
2019
set => SetProperty(ref icon, value);
2120
}
2221

23-
public Uri IconSource { get; set; }
22+
//public Uri IconSource { get; set; }
2423
public byte[] IconData { get; set; }
2524

2625
public string Text { get; set; }

src/Files.Uwp/DataModels/SidebarPinnedModel.cs

Lines changed: 62 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Files.Uwp.Filesystem;
55
using Files.Uwp.Helpers;
66
using Files.Backend.Services.Settings;
7-
using Files.Uwp.UserControls;
87
using Files.Uwp.ViewModels;
98
using CommunityToolkit.Mvvm.DependencyInjection;
109
using Microsoft.Toolkit.Uwp;
@@ -18,6 +17,7 @@
1817
using Windows.ApplicationModel.Core;
1918
using Windows.Storage;
2019
using Windows.UI.Xaml.Media.Imaging;
20+
using System.Collections.Specialized;
2121

2222
namespace Files.Uwp.DataModels
2323
{
@@ -27,22 +27,32 @@ public class SidebarPinnedModel
2727

2828
private SidebarPinnedController controller;
2929

30-
private LocationItem favoriteSection;
31-
3230
[JsonIgnore]
3331
public MainViewModel MainViewModel => App.MainViewModel;
3432

3533
[JsonProperty("items")]
3634
public List<string> FavoriteItems { get; set; } = new List<string>();
3735

36+
private readonly List<INavigationControlItem> favoriteList = new List<INavigationControlItem>();
37+
38+
public IReadOnlyList<INavigationControlItem> Favorites
39+
{
40+
get
41+
{
42+
lock (favoriteList)
43+
{
44+
return favoriteList.ToList().AsReadOnly();
45+
}
46+
}
47+
}
48+
3849
public void SetController(SidebarPinnedController controller)
3950
{
4051
this.controller = controller;
4152
}
4253

4354
public SidebarPinnedModel()
4455
{
45-
favoriteSection = SidebarControl.SideBarItems.FirstOrDefault(x => x.Text == "SidebarFavorites".GetLocalized()) as LocationItem;
4656
}
4757

4858
/// <summary>
@@ -57,32 +67,6 @@ public void AddDefaultItems()
5767
FavoriteItems.Add(udp.Documents);
5868
}
5969

60-
private void RemoveFavoritesSideBarSection()
61-
{
62-
try
63-
{
64-
var item = (from n in SidebarControl.SideBarItems where n.Text.Equals("SidebarFavorites".GetLocalized()) select n).FirstOrDefault();
65-
if (!UserSettingsService.AppearanceSettingsService.ShowFavoritesSection && item != null)
66-
{
67-
SidebarControl.SideBarItems.Remove(item);
68-
}
69-
}
70-
catch (Exception)
71-
{ }
72-
}
73-
74-
public async void UpdateFavoritesSectionVisibility()
75-
{
76-
if (UserSettingsService.AppearanceSettingsService.ShowFavoritesSection)
77-
{
78-
await AddAllItemsToSidebar();
79-
}
80-
else
81-
{
82-
RemoveFavoritesSideBarSection();
83-
}
84-
}
85-
8670
/// <summary>
8771
/// Gets the items from the navigation page
8872
/// </summary>
@@ -97,20 +81,11 @@ public List<string> GetItems()
9781
/// <param name="item">Item to remove</param>
9882
public async void AddItem(string item)
9983
{
100-
await SidebarControl.SideBarItemsSemaphore.WaitAsync();
101-
102-
try
103-
{
104-
if (!string.IsNullOrEmpty(item) && !FavoriteItems.Contains(item))
105-
{
106-
FavoriteItems.Add(item);
107-
await AddItemToSidebarAsync(item);
108-
Save();
109-
}
110-
}
111-
finally
84+
if (!string.IsNullOrEmpty(item) && !FavoriteItems.Contains(item))
11285
{
113-
SidebarControl.SideBarItemsSemaphore.Release();
86+
FavoriteItems.Add(item);
87+
await AddItemToSidebarAsync(item);
88+
Save();
11489
}
11590
}
11691

@@ -134,18 +109,20 @@ public async Task ShowHideRecycleBinItemAsync(bool show)
134109
};
135110
// Add recycle bin to sidebar, title is read from LocalSettings (provided by the fulltrust process)
136111
// TODO: the very first time the app is launched localized name not available
137-
if (!favoriteSection.ChildItems.Any(x => x.Path == CommonPaths.RecycleBinPath))
112+
if (!favoriteList.Any(x => x.Path == CommonPaths.RecycleBinPath))
138113
{
139-
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Add(recycleBinItem));
114+
favoriteList.Add(recycleBinItem);
115+
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, recycleBinItem));
140116
}
141117
}
142118
else
143119
{
144-
foreach (INavigationControlItem item in favoriteSection.ChildItems.ToList())
120+
foreach (INavigationControlItem item in favoriteList.ToList())
145121
{
146122
if (item is LocationItem && item.Path == CommonPaths.RecycleBinPath)
147123
{
148-
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Remove(item));
124+
favoriteList.Remove(item);
125+
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
149126
}
150127
}
151128
}
@@ -188,16 +165,12 @@ public bool MoveItem(INavigationControlItem locationItem, int oldIndex, int newI
188165
{
189166
FavoriteItems.RemoveAt(oldIndex - 1);
190167
FavoriteItems.Insert(newIndex - 1, locationItem.Path);
191-
favoriteSection.ChildItems.RemoveAt(oldIndex);
192-
favoriteSection.ChildItems.Insert(newIndex, locationItem);
168+
favoriteList.RemoveAt(oldIndex);
169+
favoriteList.Insert(newIndex, locationItem);
170+
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, locationItem, newIndex, oldIndex));
193171
Save();
194172
}
195-
catch (Exception ex) when (
196-
ex is ArgumentException // Pinned item was invalid
197-
|| ex is FileNotFoundException // Pinned item was deleted
198-
|| ex is System.Runtime.InteropServices.COMException // Pinned item's drive was ejected
199-
|| (uint)ex.HResult == 0x8007000F // The system cannot find the drive specified
200-
|| (uint)ex.HResult == 0x800700A1) // The specified path is invalid (usually an mtp device was disconnected)
173+
catch (Exception ex)
201174
{
202175
Debug.WriteLine($"An error occurred while moving pinned items in the Favorites sidebar section. {ex.Message}");
203176
FavoriteItems = sidebarItemsBackup;
@@ -238,7 +211,7 @@ public void SwapItems(INavigationControlItem firstLocationItem, INavigationContr
238211
/// <returns>Index of the item</returns>
239212
public int IndexOfItem(INavigationControlItem locationItem)
240213
{
241-
return favoriteSection.ChildItems.IndexOf(locationItem);
214+
return favoriteList.FindIndex(x => x.Path == locationItem.Path);
242215
}
243216

244217
/// <summary>
@@ -266,8 +239,8 @@ public async Task AddItemToSidebarAsync(string path)
266239
{
267240
var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
268241
var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item));
269-
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
270-
int insertIndex = lastItem != null ? favoriteSection.ChildItems.IndexOf(lastItem) + 1 : 0;
242+
var lastItem = favoriteList.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
243+
int insertIndex = lastItem != null ? favoriteList.IndexOf(lastItem) + 1 : 0;
271244
var locationItem = new LocationItem
272245
{
273246
Font = MainViewModel.FontName,
@@ -314,9 +287,10 @@ public async Task AddItemToSidebarAsync(string path)
314287
Debug.WriteLine($"Pinned item was invalid {res.ErrorCode}, item: {path}");
315288
}
316289

317-
if (!favoriteSection.ChildItems.Any(x => x.Path == locationItem.Path))
290+
if (!favoriteList.Any(x => x.Path == locationItem.Path))
318291
{
319-
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Insert(insertIndex, locationItem));
292+
favoriteList.Insert(insertIndex, locationItem);
293+
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, locationItem, insertIndex));
320294
}
321295
}
322296

@@ -326,12 +300,13 @@ public async Task AddItemToSidebarAsync(string path)
326300
/// <param name="section">The section.</param>
327301
private void AddLocationItemToSidebar(LocationItem section)
328302
{
329-
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
330-
int insertIndex = lastItem != null ? favoriteSection.ChildItems.IndexOf(lastItem) + 1 : 0;
303+
var lastItem = favoriteList.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
304+
int insertIndex = lastItem != null ? favoriteList.IndexOf(lastItem) + 1 : 0;
331305

332-
if (!favoriteSection.ChildItems.Any(x => x.Section == section.Section))
306+
if (!favoriteList.Any(x => x.Section == section.Section))
333307
{
334-
favoriteSection.ChildItems.Insert(insertIndex, section);
308+
favoriteList.Insert(insertIndex, section);
309+
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, section, insertIndex));
335310
}
336311
}
337312

@@ -345,61 +320,28 @@ public async Task AddAllItemsToSidebar()
345320
return;
346321
}
347322

348-
await SidebarControl.SideBarItemsSemaphore.WaitAsync();
349-
try
323+
var homeSection = new LocationItem()
350324
{
351-
var homeSection = new LocationItem()
352-
{
353-
Text = "Home".GetLocalized(),
354-
Section = SectionType.Home,
355-
MenuOptions = new ContextMenuOptions
356-
{
357-
IsLocationItem = true
358-
},
359-
Font = MainViewModel.FontName,
360-
IsDefaultLocation = true,
361-
Icon = await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => new BitmapImage(new Uri("ms-appx:///Assets/FluentIcons/Home.png"))),
362-
Path = "Home".GetLocalized()
363-
};
364-
favoriteSection ??= new LocationItem()
365-
{
366-
Text = "SidebarFavorites".GetLocalized(),
367-
Section = SectionType.Favorites,
368-
MenuOptions = new ContextMenuOptions
369-
{
370-
ShowHideSection = true
371-
},
372-
SelectsOnInvoked = false,
373-
Icon = await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIHelpers.GetIconResource(Constants.Shell32.QuickAccess)),
374-
Font = MainViewModel.FontName,
375-
ChildItems = new BulkConcurrentObservableCollection<INavigationControlItem>()
376-
};
377-
378-
if (homeSection != null)
379-
{
380-
AddLocationItemToSidebar(homeSection);
381-
}
382-
383-
if (!SidebarControl.SideBarItems.Any(x => x.Text == "SidebarFavorites".GetLocalized()))
384-
{
385-
SidebarControl.SideBarItems.BeginBulkOperation();
386-
var index = 0; // First section
387-
SidebarControl.SideBarItems.Insert(index, favoriteSection);
388-
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => SidebarControl.SideBarItems.EndBulkOperation());
389-
}
390-
391-
for (int i = 0; i < FavoriteItems.Count; i++)
325+
Text = "Home".GetLocalized(),
326+
Section = SectionType.Home,
327+
MenuOptions = new ContextMenuOptions
392328
{
393-
string path = FavoriteItems[i];
394-
await AddItemToSidebarAsync(path);
395-
}
329+
IsLocationItem = true
330+
},
331+
Font = MainViewModel.FontName,
332+
IsDefaultLocation = true,
333+
Icon = await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => new BitmapImage(new Uri("ms-appx:///Assets/FluentIcons/Home.png"))),
334+
Path = "Home".GetLocalized()
335+
};
336+
AddLocationItemToSidebar(homeSection);
396337

397-
await ShowHideRecycleBinItemAsync(UserSettingsService.AppearanceSettingsService.PinRecycleBinToSidebar);
398-
}
399-
finally
338+
for (int i = 0; i < FavoriteItems.Count; i++)
400339
{
401-
SidebarControl.SideBarItemsSemaphore.Release();
340+
string path = FavoriteItems[i];
341+
await AddItemToSidebarAsync(path);
402342
}
343+
344+
await ShowHideRecycleBinItemAsync(UserSettingsService.AppearanceSettingsService.PinRecycleBinToSidebar);
403345
}
404346

405347
/// <summary>
@@ -409,17 +351,15 @@ public void RemoveStaleSidebarItems()
409351
{
410352
// Remove unpinned items from sidebar
411353
// Reverse iteration to avoid skipping elements while removing
412-
if (favoriteSection != null)
354+
for (int i = favoriteList.Count - 1; i >= 0; i--)
413355
{
414-
for (int i = favoriteSection.ChildItems.Count - 1; i >= 0; i--)
356+
var childItem = favoriteList[i];
357+
if (childItem is LocationItem item)
415358
{
416-
var childItem = favoriteSection.ChildItems[i];
417-
if (childItem is LocationItem item)
359+
if (!item.IsDefaultLocation && !FavoriteItems.Contains(item.Path))
418360
{
419-
if (!item.IsDefaultLocation && !FavoriteItems.Contains(item.Path))
420-
{
421-
favoriteSection.ChildItems.RemoveAt(i);
422-
}
361+
favoriteList.RemoveAt(i);
362+
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
423363
}
424364
}
425365
}

src/Files.Uwp/Filesystem/LibraryManager.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Files.Backend.Services.Settings;
33
using Files.Uwp.Helpers;
44
using Files.Shared;
5-
using Files.Shared.Extensions;
65
using Files.Uwp.ViewModels;
76
using Microsoft.Toolkit.Uwp;
87
using System;
@@ -39,8 +38,6 @@ public LibraryManager()
3938
{
4039
}
4140

42-
private static bool IsLibraryOnSidebar(LibraryLocationItem item) => item != null && !item.IsEmpty && item.IsDefaultLocation;
43-
4441
public void Dispose()
4542
{
4643
}
@@ -73,8 +70,8 @@ public Task HandleWin32LibraryEvent(ShellLibraryItem library, string oldPath)
7370
// library is null in case it was deleted
7471
if (library != null && !Libraries.Any(x => x.Path == library.FullPath))
7572
{
76-
var index = librariesList.AddSorted(new LibraryLocationItem(library));
77-
DataChanged?.Invoke(SectionType.Library, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, library, index));
73+
librariesList.Add(new LibraryLocationItem(library));
74+
DataChanged?.Invoke(SectionType.Library, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, library));
7875
}
7976
return Task.CompletedTask;
8077
}
@@ -99,8 +96,8 @@ public async Task<bool> CreateNewLibrary(string name)
9996
var newLib = await LibraryHelper.CreateLibrary(name);
10097
if (newLib != null)
10198
{
102-
var index = librariesList.AddSorted(newLib);
103-
DataChanged?.Invoke(SectionType.Library, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newLib, index));
99+
librariesList.Add(newLib);
100+
DataChanged?.Invoke(SectionType.Library, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newLib));
104101
return true;
105102
}
106103
return false;
@@ -115,7 +112,7 @@ public async Task<LibraryLocationItem> UpdateLibrary(string libraryPath, string
115112
if (libItem != null)
116113
{
117114
librariesList[librariesList.IndexOf(libItem)] = newLib;
118-
DataChanged?.Invoke(SectionType.Library, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newLib));
115+
DataChanged?.Invoke(SectionType.Library, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newLib, libItem));
119116
}
120117
return newLib;
121118
}

0 commit comments

Comments
 (0)