Skip to content
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
13 changes: 4 additions & 9 deletions src/Files.Uwp/Controllers/SidebarPinnedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class SidebarPinnedController : IJson

private StorageFileQueryResult query;

private bool suppressChangeEvent;

private string configContent;

public string JsonFileName { get; } = "PinnedItems.json";
Expand Down Expand Up @@ -124,12 +122,6 @@ private async Task StartWatchConfigChangeAsync()

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

try
{
var configFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/settings/PinnedItems.json"));
Expand All @@ -155,18 +147,21 @@ private async void Query_ContentsChanged(IStorageQueryResultBase sender, object

public void SaveModel()
{
suppressChangeEvent = true;
try
{
using (var file = File.CreateText(Path.Combine(folderPath, JsonFileName)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(file, Model);

// update local configContent to avoid unnecessary refreshes
configContent = JsonConvert.SerializeObject(Model, Formatting.Indented);
}
}
catch
{
// ignored
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/Files.Uwp/Controllers/TerminalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class TerminalController : IJson

private StorageFileQueryResult query;

private bool suppressChangeEvent;

private string configContent;

public TerminalFileModel Model { get; set; }
Expand Down Expand Up @@ -103,12 +101,6 @@ private async Task StartWatchConfigChangeAsync()

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

try
{
var configFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/settings/terminal.json"));
Expand Down Expand Up @@ -201,18 +193,21 @@ public async static Task<bool> IsWindowsTerminalBuildInstalled()

public void SaveModel()
{
suppressChangeEvent = true;
try
{
using (var file = File.CreateText(Path.Combine(folderPath, JsonFileName)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(file, Model);

// update local configContent to avoid unnecessary refreshes
configContent = JsonConvert.SerializeObject(Model, Formatting.Indented);
}
}
catch
{
// ignored
}
}
}
Expand Down
51 changes: 27 additions & 24 deletions src/Files.Uwp/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;

namespace Files.Uwp.DataModels
{
Expand All @@ -27,6 +28,8 @@ public class SidebarPinnedModel

private SidebarPinnedController controller;

private SemaphoreSlim addSyncSemaphore;

[JsonIgnore]
public MainViewModel MainViewModel => App.MainViewModel;

Expand Down Expand Up @@ -54,6 +57,7 @@ public void SetController(SidebarPinnedController controller)

public SidebarPinnedModel()
{
addSyncSemaphore = new SemaphoreSlim(1, 1);
}

/// <summary>
Expand Down Expand Up @@ -82,11 +86,21 @@ public List<string> GetItems()
/// <param name="item">Item to remove</param>
public async void AddItem(string item)
{
if (!string.IsNullOrEmpty(item) && !FavoriteItems.Contains(item))
// add to `FavoriteItems` and `favoritesList` must be atomic
await addSyncSemaphore.WaitAsync();

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

Expand Down Expand Up @@ -245,7 +259,7 @@ public int IndexOfItem(INavigationControlItem locationItem, List<INavigationCont
public void Save() => controller?.SaveModel();

/// <summary>
/// Adds the item do the navigation sidebar
/// Adds the item (from a path) to the navigation sidebar
/// </summary>
/// <param name="path">The path which to save</param>
/// <returns>Task</returns>
Expand Down Expand Up @@ -299,38 +313,27 @@ public async Task AddItemToSidebarAsync(string path)
Debug.WriteLine($"Pinned item was invalid {res.ErrorCode}, item: {path}");
}

int insertIndex = -1;
lock (favoriteList)
{
if (favoriteList.Any(x => x.Path == locationItem.Path))
{
return;
}
var lastItem = favoriteList.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
insertIndex = lastItem != null ? favoriteList.IndexOf(lastItem) + 1 : 0;
favoriteList.Insert(insertIndex, locationItem);
}
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, locationItem, insertIndex));
AddLocationItemToSidebar(locationItem);
}

/// <summary>
/// Adds the item to sidebar asynchronous.
/// Adds the location item to the navigation sidebar
/// </summary>
/// <param name="section">The section.</param>
private void AddLocationItemToSidebar(LocationItem section)
/// <param name="locationItem">The location item which to save</param>
private void AddLocationItemToSidebar(LocationItem locationItem)
{
int insertIndex = -1;
lock (favoriteList)
{
if (favoriteList.Any(x => x.Section == section.Section))
if (favoriteList.Any(x => x.Path == locationItem.Path))
{
return;
}
var lastItem = favoriteList.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
insertIndex = lastItem != null ? favoriteList.IndexOf(lastItem) + 1 : 0;
favoriteList.Insert(insertIndex, section);
favoriteList.Insert(insertIndex, locationItem);
}
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, section, insertIndex));
controller.DataChanged?.Invoke(SectionType.Favorites, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, locationItem, insertIndex));
}

/// <summary>
Expand Down