diff --git a/Screenbox.Core/Common/ServiceHelpers.cs b/Screenbox.Core/Common/ServiceHelpers.cs index 011007087..2d5d2ecad 100644 --- a/Screenbox.Core/Common/ServiceHelpers.cs +++ b/Screenbox.Core/Common/ServiceHelpers.cs @@ -36,9 +36,9 @@ public static void PopulateCoreServices(ServiceCollection services) services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddSingleton(); // Shared between many pages services.AddSingleton(); // Avoid thread lock - services.AddSingleton(); // Prevent recent media reload on every page navigation services.AddSingleton(); // Global playlist // Factories diff --git a/Screenbox.Core/ViewModels/HomePageViewModel.cs b/Screenbox.Core/ViewModels/HomePageViewModel.cs index 3e18e2f6a..c0a08b77b 100644 --- a/Screenbox.Core/ViewModels/HomePageViewModel.cs +++ b/Screenbox.Core/ViewModels/HomePageViewModel.cs @@ -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 _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(); Recent = new ObservableCollection(); @@ -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(); } @@ -113,55 +98,15 @@ private void OpenUrl(Uri? url) private async Task UpdateContentAsync() { - // Pre-fetch libraries - List 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) diff --git a/Screenbox.Core/ViewModels/MainPageViewModel.cs b/Screenbox.Core/ViewModels/MainPageViewModel.cs index c8d056165..4a5a09dfa 100644 --- a/Screenbox.Core/ViewModels/MainPageViewModel.cs +++ b/Screenbox.Core/ViewModels/MainPageViewModel.cs @@ -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; @@ -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; @@ -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 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); + } } } } diff --git a/Screenbox/Pages/MainPage.xaml.cs b/Screenbox/Pages/MainPage.xaml.cs index 4c9f53e7d..13e518bcf 100644 --- a/Screenbox/Pages/MainPage.xaml.cs +++ b/Screenbox/Pages/MainPage.xaml.cs @@ -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; @@ -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) @@ -164,6 +164,7 @@ private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e if (ContentFrame.Content == null) { NavView.SelectedItem = NavView.MenuItems[0]; + _ = ViewModel.FetchLibraries(); } }