Skip to content

Commit

Permalink
Enable refresh button on homepage (#9235)
Browse files Browse the repository at this point in the history
  • Loading branch information
gave92 committed May 22, 2022
1 parent 0f90352 commit eeac058
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 42 deletions.
24 changes: 0 additions & 24 deletions src/Files.Uwp/Helpers/WidgetsHelpers.cs
@@ -1,12 +1,7 @@
using Files.Backend.Services.Settings;
using Files.Uwp.Filesystem;
using Files.Uwp.UserControls.Widgets;
using Files.Uwp.UserControls.Widgets;
using Files.Uwp.ViewModels.Widgets;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace Files.Uwp.Helpers
{
Expand Down Expand Up @@ -67,24 +62,5 @@ public static TWidget TryGetWidget<TWidget>(IWidgetsSettingsService widgetsSetti

return false;
}

public static class WidgetCards
{
/// <summary>
/// Loads an icon for each IWidgetCardItem included
/// </summary>
/// <typeparam name="T">The type of IWidgetCardItem in the collection</typeparam>
/// <typeparam name="U">The type of the INavigationControlItem backing every card item</typeparam>
/// <param name="cardItems">A collection of widget card items to load thumbnails for</param>
/// <returns></returns>
public static async Task LoadCardIcons<T, U>(IList<T> cardItems)
where T : IWidgetCardItem<U>
{
foreach (T cardItem in cardItems.ToList()) // ToList() is necessary
{
await cardItem.LoadCardThumbnailAsync();
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Files.Uwp/UserControls/AddressToolbar.xaml
Expand Up @@ -411,7 +411,7 @@
Height="32"
x:FieldModifier="public"
AccessKey="R"
AutomationProperties.Name="{helpers:ResourceString Name=NavRefreshButton/AutomationProperties/Name}"
AutomationProperties.Name="{helpers:ResourceString Name=NavResfreshButton/AutomationProperties/Name}"
Background="Transparent"
Command="{x:Bind ViewModel.RefreshClickCommand, Mode=OneWay}"
IsEnabled="{x:Bind ViewModel.CanRefresh, Mode=OneWay}"
Expand Down
6 changes: 6 additions & 0 deletions src/Files.Uwp/UserControls/Widgets/BundlesWidget.xaml.cs
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Toolkit.Uwp;
using System;
using Windows.UI.Xaml.Controls;
using System.Threading.Tasks;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

Expand Down Expand Up @@ -35,6 +36,11 @@ public BundlesWidget()
this.ViewModel = new BundlesViewModel();
}

public Task RefreshWidget()
{
return Task.CompletedTask;
}

#region IDisposable

public void Dispose()
Expand Down
10 changes: 8 additions & 2 deletions src/Files.Uwp/UserControls/Widgets/DrivesWidget.xaml.cs
@@ -1,7 +1,6 @@
using Files.Uwp.DataModels.NavigationControlItems;
using Files.Uwp.Helpers;
using Files.Backend.Services.Settings;
using Files.Uwp.ViewModels;
using Files.Uwp.ViewModels.Widgets;
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Toolkit.Uwp;
Expand All @@ -20,7 +19,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Windows.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel.Core;
using Files.Uwp.UserControls.Widgets;
using System.Collections.Specialized;

namespace Files.Uwp.UserControls.Widgets
Expand Down Expand Up @@ -286,6 +284,14 @@ private async Task<bool> CheckEmptyDrive(string drivePath)
return false;
}

public async Task RefreshWidget()
{
foreach (var item in ItemsAdded)
{
await item.Item.UpdatePropertiesAsync();
}
}

public void Dispose()
{

Expand Down
11 changes: 9 additions & 2 deletions src/Files.Uwp/UserControls/Widgets/FolderWidget.xaml.cs
Expand Up @@ -21,7 +21,6 @@
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Imaging;
using Files.Uwp.DataModels.NavigationControlItems;
using Files.Uwp.UserControls.Widgets;

namespace Files.Uwp.UserControls.Widgets
{
Expand Down Expand Up @@ -183,7 +182,10 @@ private async void FolderWidget_Loaded(object sender, RoutedEventArgs e)
SelectCommand = LibraryCardCommand
});

await WidgetsHelpers.WidgetCards.LoadCardIcons<FolderCardItem, LocationItem>(ItemsAdded);
foreach (var cardItem in ItemsAdded.ToList()) // ToList() is necessary
{
await cardItem.LoadCardThumbnailAsync();
}

ItemsAdded.EndBulkOperation();
}
Expand Down Expand Up @@ -266,6 +268,11 @@ private async Task OpenLibraryCard(FolderCardItem item)
LibraryCardInvoked?.Invoke(this, new LibraryCardInvokedEventArgs { Path = item.Path });
}

public Task RefreshWidget()
{
return Task.CompletedTask;
}

public void Dispose()
{

Expand Down
10 changes: 8 additions & 2 deletions src/Files.Uwp/UserControls/Widgets/RecentFilesWidget.xaml.cs
Expand Up @@ -49,7 +49,7 @@ public RecentFilesWidget()

recentItemsCollection.Clear();

PopulateRecentsList();
_ = PopulateRecentsList();
}

private void OpenFileLocation_Click(object sender, RoutedEventArgs e)
Expand All @@ -68,7 +68,7 @@ private void OpenFileLocation_Click(object sender, RoutedEventArgs e)
}
}

private async void PopulateRecentsList()
private async Task PopulateRecentsList()
{
Empty.Visibility = Visibility.Collapsed;

Expand Down Expand Up @@ -222,6 +222,12 @@ private void ClearRecentItems_Click(object sender, RoutedEventArgs e)
Empty.Visibility = Visibility.Visible;
}

public async Task RefreshWidget()
{
recentItemsCollection.Clear();
await PopulateRecentsList();
}

public void Dispose()
{
}
Expand Down
3 changes: 3 additions & 0 deletions src/Files.Uwp/ViewModels/Widgets/IWidgetItemModel.cs
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace Files.Uwp.ViewModels.Widgets
{
Expand All @@ -11,5 +12,7 @@ public interface IWidgetItemModel : IDisposable
string AutomationProperties { get; }

bool IsWidgetSettingEnabled { get; }

Task RefreshWidget();
}
}
9 changes: 4 additions & 5 deletions src/Files.Uwp/Views/ColumnShellPage.xaml.cs
Expand Up @@ -788,10 +788,9 @@ private async void KeyboardAccelerator_Invoked(KeyboardAccelerator sender, Keybo

public async void Refresh_Click()
{
ToolbarViewModel.CanRefresh = false;

if (InstanceViewModel.IsPageTypeSearchResults)
{
ToolbarViewModel.CanRefresh = false;
var searchInstance = new FolderSearch
{
Query = InstanceViewModel.CurrentSearchQuery,
Expand All @@ -801,10 +800,10 @@ public async void Refresh_Click()
};
await FilesystemViewModel.SearchAsync(searchInstance);
}
else
else if (CurrentPageType != typeof(WidgetsPage))
{
var ContentOwnedViewModelInstance = FilesystemViewModel;
ContentOwnedViewModelInstance?.RefreshItems(null);
ToolbarViewModel.CanRefresh = false;
FilesystemViewModel?.RefreshItems(null);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Files.Uwp/Views/ModernShellPage.xaml.cs
Expand Up @@ -862,10 +862,9 @@ private async void KeyboardAccelerator_Invoked(KeyboardAccelerator sender, Keybo

public async void Refresh_Click()
{
ToolbarViewModel.CanRefresh = false;

if (InstanceViewModel.IsPageTypeSearchResults)
{
ToolbarViewModel.CanRefresh = false;
var searchInstance = new FolderSearch
{
Query = InstanceViewModel.CurrentSearchQuery,
Expand All @@ -875,10 +874,10 @@ public async void Refresh_Click()
};
await FilesystemViewModel.SearchAsync(searchInstance);
}
else
else if (CurrentPageType != typeof(WidgetsPage))
{
var ContentOwnedViewModelInstance = FilesystemViewModel;
ContentOwnedViewModelInstance?.RefreshItems(null);
ToolbarViewModel.CanRefresh = false;
FilesystemViewModel?.RefreshItems(null);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/Files.Uwp/Views/WidgetsPage.xaml.cs
Expand Up @@ -14,6 +14,7 @@
using System.Runtime.InteropServices;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;

namespace Files.Uwp.Views
{
Expand Down Expand Up @@ -220,11 +221,14 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
AppInstance.InstanceViewModel.IsPageTypeFtp = false;
AppInstance.InstanceViewModel.IsPageTypeZipFolder = false;
AppInstance.InstanceViewModel.IsPageTypeLibrary = false;
AppInstance.ToolbarViewModel.CanRefresh = false;
AppInstance.ToolbarViewModel.CanRefresh = true;
AppInstance.ToolbarViewModel.CanGoBack = AppInstance.CanNavigateBackward;
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.FilesystemViewModel.SetWorkingDirectoryAsync("Home".GetLocalized());

Expand All @@ -240,12 +244,26 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
AppInstance.ToolbarViewModel.PathComponents.Add(item);
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
AppInstance.ToolbarViewModel.RefreshRequested -= ToolbarViewModel_RefreshRequested;
}

private async void ToolbarViewModel_RefreshRequested(object sender, EventArgs e)
{
AppInstance.ToolbarViewModel.CanRefresh = false;
await Task.WhenAll(Widgets.ViewModel.Widgets.Select(w => w.WidgetItemModel.RefreshWidget()));
AppInstance.ToolbarViewModel.CanRefresh = true;
}

#region IDisposable

public void Dispose()
{
ViewModel.YourHomeLoadedInvoked -= ViewModel_YourHomeLoadedInvoked;
Widgets.ViewModel.WidgetListRefreshRequestedInvoked -= ViewModel_WidgetListRefreshRequestedInvoked;
AppInstance.ToolbarViewModel.RefreshRequested -= ToolbarViewModel_RefreshRequested;
ViewModel?.Dispose();
}

Expand Down

0 comments on commit eeac058

Please sign in to comment.