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

Feature: Update the Drives Widget & Tags Widget when refreshing the Home Page #16732

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
2 changes: 0 additions & 2 deletions src/Files.App/Data/Contracts/IAddressToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public interface IAddressToolbarViewModel

public event ItemDraggedOverPathItemEventHandler ItemDraggedOverPathItem;

public event EventHandler RefreshRequested;

public event EventHandler RefreshWidgetsRequested;

public void SwitchSearchBoxVisibility();
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public sealed partial class WidgetFileTagsContainerItem : ObservableObject, IAsy

public ObservableCollection<WidgetFileTagCardItem> Tags { get; }

public string Uid => _tagUid;

private string? _Color;
public string? Color
{
Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/ViewModels/HomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public void RefreshWidgetList()
ReloadWidgets();
}

public Task RefreshWidgetProperties()
{
return Task.WhenAll(WidgetItems.Select(w => w.WidgetItemModel.RefreshWidgetAsync()));
}

private bool InsertWidget(WidgetContainerItem widgetModel, int atIndex)
{
// The widget must not be null and must implement IWidgetItemModel
Expand Down
11 changes: 0 additions & 11 deletions src/Files.App/ViewModels/UserControls/AddressToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public sealed class AddressToolbarViewModel : ObservableObject, IAddressToolbarV

public event PathBoxItemDroppedEventHandler? PathBoxItemDropped;

public event EventHandler? RefreshRequested;

public event EventHandler? RefreshWidgetsRequested;

public ObservableCollection<PathBoxItem> PathComponents { get; } = [];
Expand Down Expand Up @@ -201,8 +199,6 @@ public Style LayoutThemedIcon

public AddressToolbarViewModel()
{
RefreshClickCommand = new RelayCommand<RoutedEventArgs>(e => RefreshRequested?.Invoke(this, EventArgs.Empty));

dispatcherQueue = DispatcherQueue.GetForCurrentThread();
dragOverTimer = dispatcherQueue.CreateTimer();

Expand Down Expand Up @@ -230,11 +226,6 @@ private async void UpdateService_OnPropertyChanged(object? sender, PropertyChang
IsUpdating = UpdateService.IsUpdating;
}

public void RefreshWidgets()
{
RefreshWidgetsRequested?.Invoke(this, EventArgs.Empty);
}

private void UserSettingsService_OnSettingChangedEvent(object? sender, SettingChangedEventArgs e)
{
switch (e.SettingName)
Expand Down Expand Up @@ -449,8 +440,6 @@ public string PathControlDisplayText
set => SetProperty(ref pathControlDisplayText, value);
}

public ICommand RefreshClickCommand { get; }

public void PathItemSeparator_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
var pathSeparatorIcon = sender as FontIcon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class BaseWidgetViewModel : ObservableObject
protected IHomePageContext HomePageContext { get; } = Ioc.Default.GetRequiredService<IHomePageContext>();
protected IContentPageContext ContentPageContext { get; } = Ioc.Default.GetRequiredService<IContentPageContext>();
protected IFileTagsService FileTagsService { get; } = Ioc.Default.GetRequiredService<IFileTagsService>();
protected IFileTagsSettingsService FileTagsSettingsService { get; } = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
protected DrivesViewModel DrivesViewModel { get; } = Ioc.Default.GetRequiredService<DrivesViewModel>();
protected INetworkService NetworkService { get; } = Ioc.Default.GetRequiredService<INetworkService>();
protected ICommandManager CommandManager { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Files.App.ViewModels.UserControls.Widgets
/// </summary>
public sealed partial class FileTagsWidgetViewModel : BaseWidgetViewModel, IWidgetViewModel
{
private CancellationTokenSource _updateCTS;

// Properties

public ObservableCollection<WidgetFileTagsContainerItem> Containers { get; } = [];
Expand All @@ -34,6 +36,8 @@ public FileTagsWidgetViewModel()
{
_ = InitializeWidget();

FileTagsSettingsService.OnTagsUpdated += FileTagsSettingsService_OnTagsUpdated;

PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand);
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
OpenFileLocationCommand = new RelayCommand<WidgetCardItem>(ExecuteOpenFileLocationCommand);
Expand All @@ -46,20 +50,44 @@ public async Task InitializeWidget()
{
await foreach (var item in FileTagsService.GetTagsAsync())
{
var container = new WidgetFileTagsContainerItem(item.Uid)
{
Name = item.Name,
Color = item.Color
};
CreateTagContainerItem(item);
}
}

public async Task RefreshWidgetAsync()
{
_updateCTS?.Cancel();
_updateCTS = new CancellationTokenSource();
await foreach (var item in FileTagsService.GetTagsAsync())
{
if (_updateCTS.IsCancellationRequested)
break;

Containers.Add(container);
_ = container.InitAsync();
var matchingItem = Containers.First(c => c.Uid == item.Uid);
if (matchingItem is null)
{
CreateTagContainerItem(item);
}
else
{
matchingItem.Name = item.Name;
matchingItem.Color = item.Color;
matchingItem.Tags.Clear();
_ = matchingItem.InitAsync(_updateCTS.Token);
}
}
}

public Task RefreshWidgetAsync()
private void CreateTagContainerItem(TagViewModel tag)
{
return Task.CompletedTask;
var container = new WidgetFileTagsContainerItem(tag.Uid)
{
Name = tag.Name,
Color = tag.Color
};

Containers.Add(container);
_ = container.InitAsync();
}

public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCardItem item, bool isPinned, bool isFolder = false)
Expand Down Expand Up @@ -189,10 +217,16 @@ private void ExecuteOpenFileLocationCommand(WidgetCardItem? item)
});
}

private async void FileTagsSettingsService_OnTagsUpdated(object? sender, EventArgs e)
{
await RefreshWidgetAsync();
}

// Disposer

public void Dispose()
{
FileTagsSettingsService.OnTagsUpdated -= FileTagsSettingsService_OnTagsUpdated;
}
}
}
15 changes: 2 additions & 13 deletions src/Files.App/Views/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
AppInstance.ToolbarViewModel.CanGoForward = AppInstance.CanNavigateForward;
AppInstance.ToolbarViewModel.CanNavigateToParent = false;

AppInstance.ToolbarViewModel.RefreshRequested -= ToolbarViewModel_RefreshRequested;
AppInstance.ToolbarViewModel.RefreshRequested += ToolbarViewModel_RefreshRequested;

// Set path of working directory empty
await AppInstance.ShellViewModel.SetWorkingDirectoryAsync("Home");
AppInstance.ShellViewModel.CheckForBackgroundImage();
Expand All @@ -74,6 +71,8 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)

AppInstance.ToolbarViewModel.PathComponents.Add(item);

await ViewModel.RefreshWidgetProperties();

base.OnNavigatedTo(e);
}

Expand All @@ -82,20 +81,10 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
Dispose();
}

private async void ToolbarViewModel_RefreshRequested(object? sender, EventArgs e)
{
AppInstance.ToolbarViewModel.CanRefresh = false;

await Task.WhenAll(ViewModel.WidgetItems.Select(w => w.WidgetItemModel.RefreshWidgetAsync()));

AppInstance.ToolbarViewModel.CanRefresh = true;
}

// Disposer

public void Dispose()
{
AppInstance.ToolbarViewModel.RefreshRequested -= ToolbarViewModel_RefreshRequested;
ViewModel?.Dispose();
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/Files.App/Views/Shells/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ public BaseShellPage(CurrentInstanceViewModel instanceViewModel)
ToolbarViewModel.AddressBarTextEntered += ShellPage_AddressBarTextEntered;
ToolbarViewModel.PathBoxItemDropped += ShellPage_PathBoxItemDropped;

ToolbarViewModel.RefreshRequested += ShellPage_RefreshRequested;
ToolbarViewModel.EditModeEnabled += NavigationToolbar_EditModeEnabled;
ToolbarViewModel.ItemDraggedOverPathItem += ShellPage_NavigationRequested;
ToolbarViewModel.PathBoxQuerySubmitted += NavigationToolbar_QuerySubmitted;
Expand Down Expand Up @@ -379,11 +378,6 @@ protected async void ShellPage_TextChanged(ISearchBoxViewModel sender, SearchBox
}
}

protected async void ShellPage_RefreshRequested(object sender, EventArgs e)
{
await Refresh_Click();
}

protected void AppSettings_SortDirectionPreferenceUpdated(object sender, SortDirection e)
{
ShellViewModel?.UpdateSortDirectionStatusAsync();
Expand Down Expand Up @@ -562,6 +556,12 @@ public async Task Refresh_Click()
ToolbarViewModel.CanRefresh = false;
ShellViewModel?.RefreshItems(null);
}
else if (ItemDisplay.Content is HomePage homePage)
{
ToolbarViewModel.CanRefresh = false;
await homePage.ViewModel.RefreshWidgetProperties();
ToolbarViewModel.CanRefresh = true;
}
}

public virtual void Back_Click()
Expand Down Expand Up @@ -831,7 +831,6 @@ public virtual void Dispose()
ToolbarViewModel.ToolbarPathItemLoaded -= ShellPage_ToolbarPathItemLoaded;
ToolbarViewModel.AddressBarTextEntered -= ShellPage_AddressBarTextEntered;
ToolbarViewModel.PathBoxItemDropped -= ShellPage_PathBoxItemDropped;
ToolbarViewModel.RefreshRequested -= ShellPage_RefreshRequested;
ToolbarViewModel.EditModeEnabled -= NavigationToolbar_EditModeEnabled;
ToolbarViewModel.ItemDraggedOverPathItem -= ShellPage_NavigationRequested;
ToolbarViewModel.PathBoxQuerySubmitted -= NavigationToolbar_QuerySubmitted;
Expand Down