Skip to content

Commit

Permalink
make home page view model transient (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
huynhsontung authored Nov 21, 2024
1 parent 9860e80 commit af25154
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Screenbox.Core/Common/ServiceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static void PopulateCoreServices(ServiceCollection services)
services.AddTransient<NotificationViewModel>();
services.AddTransient<LivelyWallpaperPlayerViewModel>();
services.AddTransient<LivelyWallpaperSelectorViewModel>();
services.AddTransient<HomePageViewModel>();
services.AddSingleton<CommonViewModel>(); // Shared between many pages
services.AddSingleton<VolumeViewModel>(); // Avoid thread lock
services.AddSingleton<HomePageViewModel>(); // Prevent recent media reload on every page navigation
services.AddSingleton<MediaListViewModel>(); // Global playlist

// Factories
Expand Down
61 changes: 3 additions & 58 deletions Screenbox.Core/ViewModels/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@ public sealed partial class HomePageViewModel : ObservableRecipient,

private readonly MediaViewModelFactory _mediaFactory;
private readonly IFilesService _filesService;
private readonly ILibraryService _libraryService;
private readonly ISettingsService _settingsService;
private readonly CoreDispatcher _dispatcher;
private readonly Dictionary<string, string> _pathToMruMappings;
private bool _isLoaded; // Assume this class is a singleton

public HomePageViewModel(MediaViewModelFactory mediaFactory,
IFilesService filesService,
ISettingsService settingsService,
ILibraryService libraryService)
public HomePageViewModel(MediaViewModelFactory mediaFactory, IFilesService filesService,
ISettingsService settingsService)
{
_mediaFactory = mediaFactory;
_filesService = filesService;
_settingsService = settingsService;
_libraryService = libraryService;
_dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
_pathToMruMappings = new Dictionary<string, string>();
Recent = new ObservableCollection<MediaViewModel>();
Expand All @@ -62,16 +57,6 @@ public async void Receive(PlaylistCurrentItemChangedMessage message)

public async void OnLoaded()
{
// Only run once. Assume this class is a singleton.
if (_isLoaded)
{
foreach (MediaViewModel media in Recent)
{
await media.LoadThumbnailAsync();
}
return;
}
_isLoaded = true;
await UpdateContentAsync();
}

Expand Down Expand Up @@ -113,55 +98,15 @@ private void OpenUrl(Uri? url)

private async Task UpdateContentAsync()
{
// Pre-fetch libraries
List<Task> tasks = new(3) { PrefetchMusicLibraryAsync(), PrefetchVideosLibraryAsync() };

// Update recent media
if (_settingsService.ShowRecent)
{
tasks.Add(UpdateRecentMediaListAsync(true));
await UpdateRecentMediaListAsync(true);
}
else
{
Recent.Clear();
}

// Await for all of them
await Task.WhenAll(tasks);
}

private async Task PrefetchMusicLibraryAsync()
{
try
{
await _libraryService.FetchMusicAsync();
}
catch (UnauthorizedAccessException)
{
Messenger.Send(new RaiseLibraryAccessDeniedNotificationMessage(KnownLibraryId.Music));
}
catch (Exception e)
{
Messenger.Send(new ErrorMessage(null, e.Message));
LogService.Log(e);
}
}

private async Task PrefetchVideosLibraryAsync()
{
try
{
await _libraryService.FetchVideosAsync();
}
catch (UnauthorizedAccessException)
{
Messenger.Send(new RaiseLibraryAccessDeniedNotificationMessage(KnownLibraryId.Videos));
}
catch (Exception e)
{
Messenger.Send(new ErrorMessage(null, e.Message));
LogService.Log(e);
}
}

private async Task UpdateRecentMediaListAsync(bool loadMediaDetails)
Expand Down
46 changes: 45 additions & 1 deletion Screenbox.Core/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
Expand All @@ -35,11 +37,14 @@ public sealed partial class MainPageViewModel : ObservableRecipient,

private readonly ISearchService _searchService;
private readonly INavigationService _navigationService;
private readonly ILibraryService _libraryService;

public MainPageViewModel(ISearchService searchService, INavigationService navigationService)
public MainPageViewModel(ISearchService searchService, INavigationService navigationService,
ILibraryService libraryService)
{
_searchService = searchService;
_navigationService = navigationService;
_libraryService = libraryService;
_searchQuery = string.Empty;
_criticalErrorMessage = string.Empty;
IsActive = true;
Expand Down Expand Up @@ -194,7 +199,46 @@ private static double GetRanking(string text, string query)
.Where(i => i >= 0)
.Average();
return index * 0.1 + wordRank;
}

public Task FetchLibraries()
{
List<Task> tasks = new() { FetchMusicLibraryAsync(), FetchVideosLibraryAsync() };
return Task.WhenAll(tasks);
}

private async Task FetchMusicLibraryAsync()
{
try
{
await _libraryService.FetchMusicAsync();
}
catch (UnauthorizedAccessException)
{
Messenger.Send(new RaiseLibraryAccessDeniedNotificationMessage(KnownLibraryId.Music));
}
catch (Exception e)
{
Messenger.Send(new ErrorMessage(null, e.Message));
LogService.Log(e);
}
}

private async Task FetchVideosLibraryAsync()
{
try
{
await _libraryService.FetchVideosAsync();
}
catch (UnauthorizedAccessException)
{
Messenger.Send(new RaiseLibraryAccessDeniedNotificationMessage(KnownLibraryId.Videos));
}
catch (Exception e)
{
Messenger.Send(new ErrorMessage(null, e.Message));
LogService.Log(e);
}
}
}
}
3 changes: 2 additions & 1 deletion Screenbox/Pages/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
using muxc = Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -144,6 +143,7 @@ private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
SetTitleBar();
NavView.SelectedItem = NavView.MenuItems[0];
_ = ViewModel.FetchLibraries();
}

if (ApplicationView.GetForCurrentView()?.TitleBar is { } titleBar)
Expand All @@ -164,6 +164,7 @@ private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e
if (ContentFrame.Content == null)
{
NavView.SelectedItem = NavView.MenuItems[0];
_ = ViewModel.FetchLibraries();
}
}

Expand Down

0 comments on commit af25154

Please sign in to comment.