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: Improved Widget code #14458

Merged
merged 15 commits into from
Feb 8, 2024
11 changes: 5 additions & 6 deletions src/Files.App/Data/Contexts/HomePage/HomePageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using Files.App.ViewModels.Widgets;
using Microsoft.UI.Xaml.Controls;
using System.Collections.Immutable;

namespace Files.App.Data.Contexts
{
internal class HomePageContext : ObservableObject, IHomePageContext
{
private static readonly IImmutableList<FileTagsItemViewModel> emptyTaggedItems = Enumerable.Empty<FileTagsItemViewModel>().ToImmutableList();
private static readonly IImmutableList<WidgetFileTagCardItem> emptyTaggedItems = Enumerable.Empty<WidgetFileTagCardItem>().ToImmutableList();

public bool IsAnyItemRightClicked => rightClickedItem is not null;

Expand All @@ -20,20 +19,20 @@ internal class HomePageContext : ObservableObject, IHomePageContext
private CommandBarFlyout? itemContextFlyoutMenu = null;
public CommandBarFlyout? ItemContextFlyoutMenu => itemContextFlyoutMenu;

private IReadOnlyList<FileTagsItemViewModel> selectedTaggedItems = emptyTaggedItems;
public IReadOnlyList<FileTagsItemViewModel> SelectedTaggedItems
private IReadOnlyList<WidgetFileTagCardItem> selectedTaggedItems = emptyTaggedItems;
public IReadOnlyList<WidgetFileTagCardItem> SelectedTaggedItems
{
get => selectedTaggedItems;
set => selectedTaggedItems = value ?? emptyTaggedItems;
}

public HomePageContext()
{
HomePageWidget.RightClickedItemChanged += HomePageWidget_RightClickedItemChanged;
BaseWidgetViewModel.RightClickedItemChanged += HomePageWidget_RightClickedItemChanged;
FileTagsWidget.SelectedTaggedItemsChanged += FileTagsWidget_SelectedTaggedItemsChanged;
}

private void FileTagsWidget_SelectedTaggedItemsChanged(object? sender, IEnumerable<FileTagsItemViewModel> e)
private void FileTagsWidget_SelectedTaggedItemsChanged(object? sender, IEnumerable<WidgetFileTagCardItem> e)
{
SetProperty(ref selectedTaggedItems, e.ToList());
}
Expand Down
4 changes: 1 addition & 3 deletions src/Files.App/Data/Contexts/HomePage/IHomePageContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using Files.App.ViewModels.Widgets;
using Microsoft.UI.Xaml.Controls;

namespace Files.App.Data.Contexts
Expand All @@ -22,7 +20,7 @@ internal interface IHomePageContext
/// <summary>
/// An list containing all the selected tagged items
/// </summary>
IReadOnlyList<FileTagsItemViewModel> SelectedTaggedItems { get; }
IReadOnlyList<WidgetFileTagCardItem> SelectedTaggedItems { get; }

/// <summary>
/// Tells whether any item has been right clicked
Expand Down
5 changes: 2 additions & 3 deletions src/Files.App/Data/Contexts/Tags/TagsContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.ViewModels.Widgets;
using System.Collections.Immutable;

namespace Files.App.Data.Contexts
{
sealed class TagsContext : ITagsContext
sealed class TagsContext : ITagsContext
{
private static readonly IReadOnlyList<(string path, bool isFolder)> _emptyTaggedItemsList
= Enumerable.Empty<(string path, bool isFolder)>().ToImmutableList();
Expand All @@ -29,7 +28,7 @@ sealed class TagsContext : ITagsContext

public TagsContext()
{
FileTagsContainerViewModel.SelectedTagChanged += SelectedTagsChanged;
WidgetFileTagsContainerItem.SelectedTagChanged += SelectedTagsChanged;
SidebarViewModel.SelectedTagChanged += SelectedTagsChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Sidebar;
using Microsoft.UI.Xaml.Controls;

namespace Files.App.Data.Items
namespace Files.App.Data.Contracts
{

public interface INavigationControlItem : IComparable<INavigationControlItem>, INotifyPropertyChanged, ISidebarItemModel
{
public new string Text { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Media.Imaging;
using System.Threading.Tasks;

namespace Files.App.UserControls.Widgets
namespace Files.App.Data.Contracts
{
public interface IWidgetCardItem<T>
{
Expand All @@ -14,4 +13,4 @@ public interface IWidgetCardItem<T>

Task LoadCardThumbnailAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

using Microsoft.UI.Xaml.Controls;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Contracts
{
public interface IWidgetItem : IDisposable
public interface IWidgetViewModel : IDisposable
{
string WidgetName { get; }

Expand Down
38 changes: 38 additions & 0 deletions src/Files.App/Data/EventArguments/QuickAccessCardEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.EventArguments
{
public class QuickAccessCardEventArgs : EventArgs
{
public LocationItem? Item { get; set; }
}

public class QuickAccessCardInvokedEventArgs : EventArgs
{
public string? Path { get; set; }
}

public class ModifyQuickAccessEventArgs : EventArgs
{
public string[]? Paths { get; set; }
public ShellFileItem[]? Items { get; set; }
public bool Add;
public bool Pin = true;
public bool Reset = false;
public bool Reorder = false;

public ModifyQuickAccessEventArgs(string[] paths, bool add)
{
Paths = paths;
Add = add;
}

public ModifyQuickAccessEventArgs(ShellFileItem[] items, bool add)
{
Paths = items.Select(x => x.FilePath).ToArray();
Items = items;
Add = add;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using Microsoft.UI.Xaml.Controls;

namespace Files.App.Data.EventArguments
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Data/Items/LocationItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ public RecycleBinLocationItem()
RecycleBinManager.Default.RecycleBinItemDeleted += RefreshSpaceUsed;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Utils.Library
namespace Files.App.Data.Items
{
public class LibraryLocationItem : LocationItem
{
Expand Down Expand Up @@ -57,4 +57,4 @@ public async Task LoadLibraryIconAsync()
public override bool Equals(object obj)
=> obj is LibraryLocationItem other && GetType() == obj.GetType() && string.Equals(Path, other.Path, System.StringComparison.OrdinalIgnoreCase);
}
}
}
1 change: 0 additions & 1 deletion src/Files.App/Data/Items/TagsListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Files.App.Data.Items
{

public class TagsListItem
{
public bool IsTag
Expand Down
15 changes: 15 additions & 0 deletions src/Files.App/Data/Items/WidgetCardItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.Items
{
/// <summary>
/// Represents base item for widget card item.
/// </summary>
public abstract class WidgetCardItem : ObservableObject
{
public virtual string? Path { get; set; }

public virtual object? Item { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@

using Microsoft.UI.Xaml.Controls;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Items
{
public class WidgetsListControlItemViewModel : ObservableObject, IDisposable
/// <summary>
/// Represents an item of Files widget container.
/// </summary>
public class WidgetContainerItem : ObservableObject, IDisposable
{
private readonly Action<bool> _expanderValueChangedCallback;
// Fields

private readonly Action<bool> _expanderValueChangedCallback;
private readonly Func<bool> _expanderValueRequestedCallback;

// Properties

public IWidgetViewModel WidgetItemModel
=> WidgetControl as IWidgetViewModel;

public string WidgetAutomationProperties
=> WidgetItemModel.AutomationProperties;

public bool ShowMenuFlyout
=> WidgetItemModel.ShowMenuFlyout;

public MenuFlyoutItem MenuFlyoutItem
=> WidgetItemModel.MenuFlyoutItem;

private object _WidgetControl;
public object WidgetControl
{
get => _WidgetControl;
set => SetProperty(ref _WidgetControl, value);
}

public WidgetsListControlItemViewModel(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
{
WidgetControl = widgetControl;
_expanderValueChangedCallback = expanderValueChangedCallback;
_expanderValueRequestedCallback = expanderValueRequestedCallback;
}

public bool IsExpanded
{
get => _expanderValueRequestedCallback?.Invoke() ?? true;
Expand All @@ -35,26 +46,18 @@ public bool IsExpanded
}
}

public IWidgetItemModel WidgetItemModel
{
get => WidgetControl as IWidgetItemModel;
}
// Constructor

public string WidgetAutomationProperties
public WidgetContainerItem(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
{
get => WidgetItemModel.AutomationProperties;
}
_expanderValueChangedCallback = expanderValueChangedCallback;
_expanderValueRequestedCallback = expanderValueRequestedCallback;

public bool ShowMenuFlyout
{
get => WidgetItemModel.ShowMenuFlyout;
}

public MenuFlyoutItem MenuFlyoutItem
{
get => WidgetItemModel.MenuFlyoutItem;
WidgetControl = widgetControl;
}

// Disposer

public void Dispose()
{
(WidgetControl as IDisposable)?.Dispose();
Expand Down
39 changes: 39 additions & 0 deletions src/Files.App/Data/Items/WidgetDriveCardItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Media.Imaging;

namespace Files.App.Data.Items
{
public class WidgetDriveCardItem : WidgetCardItem, IWidgetCardItem<DriveItem>, IComparable<WidgetDriveCardItem>
{
private byte[] thumbnailData;

public new DriveItem Item { get; private set; }

private BitmapImage thumbnail;
public BitmapImage Thumbnail
{
get => thumbnail;
set => SetProperty(ref thumbnail, value);
}

public WidgetDriveCardItem(DriveItem item)
{
Item = item;
Path = item.Path;
}

public async Task LoadCardThumbnailAsync()
{
thumbnailData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Item.Path, Constants.DefaultIconSizes.Jumbo, true, true);

// Thumbnail data is valid, set the item icon
if (thumbnailData is not null && thumbnailData.Length > 0)
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}

public int CompareTo(WidgetDriveCardItem? other)
=> Item.Path.CompareTo(other?.Item?.Path);
}
}
Loading
Loading