Skip to content
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

Code quality: Refactoring and improvements #10948

Merged
merged 27 commits into from
Feb 3, 2023
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f0dabbb
Code Refactoring
ferrariofilippo Jan 7, 2023
1c0fbdc
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 7, 2023
1cef856
Restore await in OpenPath
gave92 Jan 8, 2023
6698d88
Merge branch 'main' of https://github.com/files-community/Files into …
gave92 Jan 8, 2023
f35bb82
More Refactoring
ferrariofilippo Jan 8, 2023
79d8cfd
Requested Changes & Binding Error
ferrariofilippo Jan 8, 2023
56461a5
Improved ContextMenu Efficiency
ferrariofilippo Jan 8, 2023
c6b9747
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 9, 2023
466f072
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 13, 2023
1134027
Build Error
ferrariofilippo Jan 14, 2023
3cfc415
Minor Optimization
ferrariofilippo Jan 14, 2023
61120b0
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 15, 2023
354de6f
Merge & Requested Changes
ferrariofilippo Jan 19, 2023
06259e7
Requested Changes
ferrariofilippo Jan 19, 2023
aae49b9
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 19, 2023
5beb0e6
Reverted Single Line Methods
ferrariofilippo Jan 19, 2023
4d18fe3
Reverted Single Line Methods
ferrariofilippo Jan 19, 2023
9f765d9
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 19, 2023
60b4ec2
Build Errors
ferrariofilippo Jan 20, 2023
e02d35f
Merge branch 'Code_Refactoring' of https://github.com/ferrariofilippo…
ferrariofilippo Jan 20, 2023
ad5f52c
Merge branch 'main' into Code_Refactoring
QuaintMako Jan 22, 2023
a9ed779
Merge branch 'main' into Code_Refactoring
ferrariofilippo Jan 29, 2023
bb873cc
Requested changes
ferrariofilippo Jan 29, 2023
9a2c348
Merge branch 'Code_Refactoring' of https://github.com/ferrariofilippo…
ferrariofilippo Jan 29, 2023
b4c2b04
Merge branch 'main' into Code_Refactoring
ferrariofilippo Feb 2, 2023
4b403ae
Merge
ferrariofilippo Feb 2, 2023
c292e27
Merge branch 'main' into Code_Refactoring
ferrariofilippo Feb 3, 2023
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
Prev Previous commit
Next Next commit
Merge
ferrariofilippo committed Feb 2, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 4b403ae7cbd56af960995c24123eb6438c46af49
115 changes: 3 additions & 112 deletions src/Files.App/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@ namespace Files.App.DataModels
{
public class SidebarPinnedModel
{
private readonly IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
private IUserSettingsService userSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
private IQuickAccessService QuickAccessService { get; } = Ioc.Default.GetRequiredService<IQuickAccessService>();

public EventHandler<NotifyCollectionChangedEventArgs>? DataChanged;

@@ -46,29 +47,6 @@ public IReadOnlyList<INavigationControlItem> Favorites
/// </summary>
public async Task UpdateItemsWithExplorer()
{
var udp = UserDataPaths.GetDefault();

FavoriteItems.Add(CommonPaths.DesktopPath);
FavoriteItems.Add(CommonPaths.DownloadsPath);
FavoriteItems.Add(udp.Documents);
FavoriteItems.Add(CommonPaths.RecycleBinPath);
}

/// <summary>
/// Gets the items from the navigation page
/// </summary>
public List<string> GetItems()
{
return FavoriteItems;
}

/// <summary>
/// Adds the item to the navigation page
/// </summary>
/// <param name="item">Item to remove</param>
public async void AddItem(string? item)
{
// add to `FavoriteItems` and `favoritesList` must be atomic
await addSyncSemaphore.WaitAsync();

try
@@ -82,75 +60,7 @@ public async void AddItem(string? item)
addSyncSemaphore.Release();
}
}

/// <summary>
/// Removes the item from the navigation page
/// </summary>
/// <param name="item">Item to remove</param>
public void RemoveItem(string? item)
{
if (string.IsNullOrWhiteSpace(item) || !FavoriteItems.Contains(item))
return;
FavoriteItems.Remove(item);
RemoveStaleSidebarItems();
Save();
}

/// <summary>
/// Moves the location item in the Favorites sidebar section from the old position to the new position
/// </summary>
/// <param name="locationItem">Location item to move</param>
/// <param name="oldIndex">The old position index of the location item</param>
/// <param name="newIndex">The new position index of the location item</param>
/// <returns>True if the move was successful</returns>
public bool MoveItem(INavigationControlItem locationItem, int oldIndex, int newIndex)
{
if (locationItem is null || newIndex > FavoriteItems.Count)
return false;

// A backup of the items, because the swapping of items requires removing and inserting them in the correct position
var sidebarItemsBackup = new List<string>(FavoriteItems);

try
{
FavoriteItems.RemoveAt(oldIndex);
FavoriteItems.Insert(newIndex, locationItem.Path);
lock (favoriteList)
{
favoriteList.RemoveAt(oldIndex);
favoriteList.Insert(newIndex, locationItem);
}
var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, locationItem, newIndex, oldIndex);
controller?.DataChanged?.Invoke(SectionType.Favorites, e);
Save();
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"An error occurred while moving pinned items in the Favorites sidebar section. {ex.Message}");
FavoriteItems = sidebarItemsBackup;
RemoveStaleSidebarItems();
_ = AddAllItemsToSidebar();
return false;
}
}

/// <summary>
/// Swaps two location items in the navigation sidebar
/// </summary>
/// <param name="firstLocationItem">The first location item</param>
/// <param name="secondLocationItem">The second location item</param>
public void SwapItems(INavigationControlItem firstLocationItem, INavigationControlItem secondLocationItem)
{
if (firstLocationItem is null || secondLocationItem is null)
return;

var indexOfFirstItemInMainPage = IndexOfItem(firstLocationItem);
var indexOfSecondItemInMainPage = IndexOfItem(secondLocationItem);

// Moves the items in the MainPage
MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);
}


/// <summary>
/// Returns the index of the location item in the navigation sidebar
@@ -165,25 +75,6 @@ public int IndexOfItem(INavigationControlItem locationItem)
}
}

/// <summary>
/// Returns the index of the location item in the collection containing Navigation control items
/// </summary>
/// <param name="locationItem">The location item</param>
/// <param name="collection">The collection in which to find the location item</param>
/// <returns>Index of the item</returns>
public int IndexOfItem(INavigationControlItem locationItem, List<INavigationControlItem> collection)
{
return collection.IndexOf(locationItem);
}

/// <summary>
/// Saves the model
/// </summary>
public void Save()
{
controller?.SaveModel();
}

/// <summary>
/// Adds the item (from a path) to the navigation sidebar
/// </summary>
3 changes: 2 additions & 1 deletion src/Files.App/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -35,7 +35,8 @@ namespace Files.App.UserControls
{
public sealed partial class SidebarControl : NavigationView, INotifyPropertyChanged
{
private readonly IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
public IUserSettingsService userSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
public IQuickAccessService QuickAccessService { get; } = Ioc.Default.GetRequiredService<IQuickAccessService>();

public delegate void SidebarItemInvokedEventHandler(object sender, SidebarItemInvokedEventArgs e);

11 changes: 7 additions & 4 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
@@ -64,8 +64,9 @@ public async Task LoadCardThumbnailAsync()

public sealed partial class DrivesWidget : UserControl, IWidgetItemModel, INotifyPropertyChanged
{
private readonly IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();

private IUserSettingsService userSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
private IQuickAccessService QuickAccessService { get; } = Ioc.Default.GetRequiredService<IQuickAccessService>();

public delegate void DrivesWidgetInvokedEventHandler(object sender, DrivesWidgetInvokedEventArgs e);

public event DrivesWidgetInvokedEventHandler DrivesWidgetInvoked;
@@ -165,15 +166,17 @@ private async void PinToFavorites_Click(object sender, RoutedEventArgs e)
var item = ((MenuFlyoutItem)sender).DataContext as DriveItem;
if (await DriveHelpers.CheckEmptyDrive(item?.Path))
return;
App.SidebarPinnedController.Model.AddItem(item?.Path);

_ = QuickAccessService.PinToSidebar(item.Path);
}

private async void UnpinFromFavorites_Click(object sender, RoutedEventArgs e)
{
var item = ((MenuFlyoutItem)sender).DataContext as DriveItem;
if (await DriveHelpers.CheckEmptyDrive(item?.Path))
return;
App.SidebarPinnedController.Model.RemoveItem(item?.Path);

_ = QuickAccessService.UnpinFromSidebar(item.Path);
}

private void OpenDriveProperties_Click(object sender, RoutedEventArgs e)
You are viewing a condensed version of this merge commit. You can view the full changes here.