diff --git a/Screenbox.Core/Helpers/LastPositionTracker.cs b/Screenbox.Core/Helpers/LastPositionTracker.cs index 4ee6b0709..0da06d0fc 100644 --- a/Screenbox.Core/Helpers/LastPositionTracker.cs +++ b/Screenbox.Core/Helpers/LastPositionTracker.cs @@ -75,7 +75,7 @@ public async Task SaveToDiskAsync(IFilesService filesService) { try { - await filesService.SaveToDiskAsync(ApplicationData.Current.TemporaryFolder, SaveFileName, _lastPositions); + await filesService.SaveToDiskAsync(ApplicationData.Current.TemporaryFolder, SaveFileName, _lastPositions.ToList()); } catch (FileLoadException) { diff --git a/Screenbox.Core/ViewModels/AlbumDetailsPageViewModel.cs b/Screenbox.Core/ViewModels/AlbumDetailsPageViewModel.cs index 1ef274720..ebeb1041a 100644 --- a/Screenbox.Core/ViewModels/AlbumDetailsPageViewModel.cs +++ b/Screenbox.Core/ViewModels/AlbumDetailsPageViewModel.cs @@ -18,13 +18,13 @@ public sealed partial class AlbumDetailsPageViewModel : ObservableRecipient [NotifyPropertyChangedFor(nameof(Year))] [NotifyPropertyChangedFor(nameof(SongsCount))] [NotifyPropertyChangedFor(nameof(TotalDuration))] - private AlbumViewModel _source = null!; + private AlbumViewModel? _source; - public uint? Year => Source.Year; + public uint? Year => Source?.Year; - public int SongsCount => Source.RelatedSongs.Count; + public int SongsCount => Source?.RelatedSongs.Count ?? 0; - public TimeSpan TotalDuration => GetTotalDuration(Source.RelatedSongs); + public TimeSpan TotalDuration => Source != null ? GetTotalDuration(Source.RelatedSongs) : TimeSpan.Zero; public ObservableCollection SortedItems { get; } @@ -45,8 +45,15 @@ public void OnNavigatedTo(object? parameter) }; } - async partial void OnSourceChanged(AlbumViewModel value) + async partial void OnSourceChanged(AlbumViewModel? value) { + if (value == null) + { + SortedItems.Clear(); + _itemList = null; + return; + } + var sorted = value.RelatedSongs.OrderBy(m => m.MediaInfo.MusicProperties.TrackNumber != 0 // Track number should start with 1 ? m.MediaInfo.MusicProperties.TrackNumber @@ -75,7 +82,7 @@ private void Play(MediaViewModel item) [RelayCommand] private void ShuffleAndPlay() { - if (Source.RelatedSongs.Count == 0) return; + if (Source == null || Source.RelatedSongs.Count == 0) return; Random rnd = new(); List shuffledList = Source.RelatedSongs.OrderBy(_ => rnd.Next()).ToList(); Messenger.Send(new ClearPlaylistMessage()); diff --git a/Screenbox.Core/ViewModels/ArtistDetailsPageViewModel.cs b/Screenbox.Core/ViewModels/ArtistDetailsPageViewModel.cs index dbe189592..9cc978517 100644 --- a/Screenbox.Core/ViewModels/ArtistDetailsPageViewModel.cs +++ b/Screenbox.Core/ViewModels/ArtistDetailsPageViewModel.cs @@ -17,23 +17,22 @@ public sealed partial class ArtistDetailsPageViewModel : ObservableRecipient [ObservableProperty] [NotifyPropertyChangedFor(nameof(TotalDuration))] [NotifyPropertyChangedFor(nameof(SongsCount))] - private ArtistViewModel _source; + private ArtistViewModel? _source; [ObservableProperty] [NotifyPropertyChangedFor(nameof(AlbumsCount))] private List> _albums; - public TimeSpan TotalDuration => GetTotalDuration(Source.RelatedSongs); + public TimeSpan TotalDuration => Source != null ? GetTotalDuration(Source.RelatedSongs) : TimeSpan.Zero; public int AlbumsCount => Albums.Count; - public int SongsCount => Source.RelatedSongs.Count; + public int SongsCount => Source?.RelatedSongs.Count ?? 0; private List? _itemList; public ArtistDetailsPageViewModel() { - _source = new ArtistViewModel(); _albums = new List>(); } @@ -47,8 +46,14 @@ public void OnNavigatedTo(object? parameter) }; } - async partial void OnSourceChanged(ArtistViewModel value) + async partial void OnSourceChanged(ArtistViewModel? value) { + if (value == null) + { + Albums = new List>(); + return; + } + Albums = value.RelatedSongs .OrderBy(m => m.MediaInfo.MusicProperties.TrackNumber) .ThenBy(m => m.Name, StringComparer.CurrentCulture) @@ -71,7 +76,7 @@ private void Play(MediaViewModel? media) [RelayCommand] private void ShuffleAndPlay() { - if (Source.RelatedSongs.Count == 0) return; + if (Source == null || Source.RelatedSongs.Count == 0) return; Random rnd = new(); List shuffledList = Source.RelatedSongs.OrderBy(_ => rnd.Next()).ToList(); Messenger.Send(new ClearPlaylistMessage()); diff --git a/Screenbox.Core/ViewModels/ArtistViewModel.cs b/Screenbox.Core/ViewModels/ArtistViewModel.cs index f44f7fe30..171550c8b 100644 --- a/Screenbox.Core/ViewModels/ArtistViewModel.cs +++ b/Screenbox.Core/ViewModels/ArtistViewModel.cs @@ -22,18 +22,13 @@ public sealed partial class ArtistViewModel : ObservableRecipient [ObservableProperty] private bool _isPlaying; - public ArtistViewModel() + public ArtistViewModel(string artist) { - Name = string.Empty; + Name = artist; RelatedSongs = new ObservableCollection(); RelatedSongs.CollectionChanged += RelatedSongsOnCollectionChanged; } - public ArtistViewModel(string artist) : this() - { - Name = artist; - } - public override string ToString() { return Name; diff --git a/Screenbox.Core/ViewModels/HomePageViewModel.cs b/Screenbox.Core/ViewModels/HomePageViewModel.cs index c0a08b77b..e50e2fc06 100644 --- a/Screenbox.Core/ViewModels/HomePageViewModel.cs +++ b/Screenbox.Core/ViewModels/HomePageViewModel.cs @@ -260,6 +260,11 @@ private async Task OpenFolderAsync() { return null; } + catch (Exception e) + { + LogService.Log(e); + return null; + } } } } \ No newline at end of file diff --git a/Screenbox.Core/ViewModels/LivelyWallpaperPlayerViewModel.cs b/Screenbox.Core/ViewModels/LivelyWallpaperPlayerViewModel.cs index 34d1fb836..b85d1a03f 100644 --- a/Screenbox.Core/ViewModels/LivelyWallpaperPlayerViewModel.cs +++ b/Screenbox.Core/ViewModels/LivelyWallpaperPlayerViewModel.cs @@ -103,7 +103,7 @@ public async Task LoadAsync() public async Task NavigatePage(WebView2 webView) { - if (Source is null) + if (Source is null || webView.CoreWebView2 == null) return; if (string.IsNullOrEmpty(Source.Path) || string.IsNullOrEmpty(Source.Model.FileName)) @@ -189,7 +189,8 @@ public async Task UpdatePauseState(WebView2 webView, bool isPaused) // Ref: https://github.com/rocksdanister/lively/wiki/Web-Guide-V-:-System-Data#--system-nowplaying public async Task UpdateCurrentTrack(WebView2 webView) { - if (Source is null || Media == null || webView.CoreWebView2.IsSuspended || !Source.IsMusic) + if (Source is null || Media == null || webView.CoreWebView2 == null || + webView.CoreWebView2.IsSuspended || !Source.IsMusic) return; var model = new LivelyMusicModel diff --git a/Screenbox.Core/ViewModels/MediaViewModel.cs b/Screenbox.Core/ViewModels/MediaViewModel.cs index 8d7d89fb6..7e9c30027 100644 --- a/Screenbox.Core/ViewModels/MediaViewModel.cs +++ b/Screenbox.Core/ViewModels/MediaViewModel.cs @@ -225,17 +225,7 @@ public async Task LoadDetailsAsync(IFilesService filesService) case StorageFile file: MediaInfo = await filesService.GetMediaInfoAsync(file); break; - case Uri { IsFile: true, IsLoopback: true, IsAbsoluteUri: true } uri: - StorageFile uriFile; - try - { - uriFile = await StorageFile.GetFileFromPathAsync(uri.OriginalString); - } - catch (IOException) - { - return; - } - + case Uri uri when await TryGetStorageFileFromUri(uri) is { } uriFile: UpdateSource(uriFile); MediaInfo = await filesService.GetMediaInfoAsync(uriFile); break; @@ -276,17 +266,9 @@ public async Task LoadDetailsAsync(IFilesService filesService) public async Task LoadThumbnailAsync() { if (Thumbnail != null) return; - if (Source is Uri { IsFile: true, IsLoopback: true, IsAbsoluteUri: true } uri) + if (Source is Uri uri && await TryGetStorageFileFromUri(uri) is { } storageFile) { - try - { - StorageFile uriFile = await StorageFile.GetFileFromPathAsync(uri.OriginalString); - UpdateSource(uriFile); - } - catch (IOException) - { - return; - } + UpdateSource(storageFile); } if (Source is StorageFile file) @@ -456,5 +438,22 @@ private void UpdateCaptions() } } } + + private static async Task TryGetStorageFileFromUri(Uri uri) + { + if (uri is { IsFile: true, IsLoopback: true, IsAbsoluteUri: true }) + { + try + { + return await StorageFile.GetFileFromPathAsync(uri.OriginalString); + } + catch (Exception) + { + return null; + } + } + + return null; + } } } \ No newline at end of file diff --git a/Screenbox/Pages/AlbumDetailsPage.xaml b/Screenbox/Pages/AlbumDetailsPage.xaml index 04b88920d..3ef764c56 100644 --- a/Screenbox/Pages/AlbumDetailsPage.xaml +++ b/Screenbox/Pages/AlbumDetailsPage.xaml @@ -113,12 +113,12 @@ SizeChanged="AlbumArt_OnSizeChanged" Translation="0,0,16"> - + + Visibility="{x:Bind ViewModel.Source.AlbumArt, Mode=OneWay, Converter={StaticResource EmptyObjectToVisibilityConverter}, ConverterParameter=true, FallbackValue=Visible}"> @@ -146,7 +146,7 @@ Margin="0,8,0,0" FontWeight="Normal" Style="{StaticResource SubtitleTextBlockStyle}" - Text="{x:Bind ViewModel.Source.ArtistName}" + Text="{x:Bind ViewModel.Source.ArtistName, FallbackValue={x:Null}}" TextWrapping="NoWrap"> diff --git a/Screenbox/Pages/AlbumDetailsPage.xaml.cs b/Screenbox/Pages/AlbumDetailsPage.xaml.cs index 851e7c600..382f826ce 100644 --- a/Screenbox/Pages/AlbumDetailsPage.xaml.cs +++ b/Screenbox/Pages/AlbumDetailsPage.xaml.cs @@ -86,7 +86,7 @@ private async void AlbumDetailsPage_OnLoaded(object sender, RoutedEventArgs e) CreateHeaderAnimation(_props, scrollingProperties.Translation.Y); - if (ViewModel.Source.RelatedSongs.Count == 0) return; + if (ViewModel.Source == null || ViewModel.Source.RelatedSongs.Count == 0) return; MediaViewModel firstSong = ViewModel.Source.RelatedSongs[0]; if (firstSong.Thumbnail != null) { diff --git a/Screenbox/Pages/ArtistDetailsPage.xaml b/Screenbox/Pages/ArtistDetailsPage.xaml index feb946bc6..e59aa7ecf 100644 --- a/Screenbox/Pages/ArtistDetailsPage.xaml +++ b/Screenbox/Pages/ArtistDetailsPage.xaml @@ -215,7 +215,7 @@ diff --git a/Screenbox/Pages/ArtistDetailsPage.xaml.cs b/Screenbox/Pages/ArtistDetailsPage.xaml.cs index 059bf8515..80319ff68 100644 --- a/Screenbox/Pages/ArtistDetailsPage.xaml.cs +++ b/Screenbox/Pages/ArtistDetailsPage.xaml.cs @@ -1,4 +1,6 @@ -using CommunityToolkit.Mvvm.DependencyInjection; +#nullable enable + +using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.WinUI; using CommunityToolkit.WinUI.Animations.Expressions; using Screenbox.Core;