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

fix: various crashing exceptions #531

Merged
merged 7 commits into from
Jan 6, 2025
2 changes: 1 addition & 1 deletion Screenbox.Core/Helpers/LastPositionTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 13 additions & 6 deletions Screenbox.Core/ViewModels/AlbumDetailsPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MediaViewModel> SortedItems { get; }

Expand All @@ -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
Expand Down Expand Up @@ -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<MediaViewModel> shuffledList = Source.RelatedSongs.OrderBy(_ => rnd.Next()).ToList();
Messenger.Send(new ClearPlaylistMessage());
Expand Down
17 changes: 11 additions & 6 deletions Screenbox.Core/ViewModels/ArtistDetailsPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGrouping<AlbumViewModel?, MediaViewModel>> _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<MediaViewModel>? _itemList;

public ArtistDetailsPageViewModel()
{
_source = new ArtistViewModel();
_albums = new List<IGrouping<AlbumViewModel?, MediaViewModel>>();
}

Expand All @@ -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<IGrouping<AlbumViewModel?, MediaViewModel>>();
return;
}

Albums = value.RelatedSongs
.OrderBy(m => m.MediaInfo.MusicProperties.TrackNumber)
.ThenBy(m => m.Name, StringComparer.CurrentCulture)
Expand All @@ -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<MediaViewModel> shuffledList = Source.RelatedSongs.OrderBy(_ => rnd.Next()).ToList();
Messenger.Send(new ClearPlaylistMessage());
Expand Down
9 changes: 2 additions & 7 deletions Screenbox.Core/ViewModels/ArtistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MediaViewModel>();
RelatedSongs.CollectionChanged += RelatedSongsOnCollectionChanged;
}

public ArtistViewModel(string artist) : this()
{
Name = artist;
}

public override string ToString()
{
return Name;
Expand Down
5 changes: 5 additions & 0 deletions Screenbox.Core/ViewModels/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ private async Task OpenFolderAsync()
{
return null;
}
catch (Exception e)
{
LogService.Log(e);
return null;
}
}
}
}
5 changes: 3 additions & 2 deletions Screenbox.Core/ViewModels/LivelyWallpaperPlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
41 changes: 20 additions & 21 deletions Screenbox.Core/ViewModels/MediaViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -456,5 +438,22 @@ private void UpdateCaptions()
}
}
}

private static async Task<StorageFile?> 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;
}
}
}
8 changes: 4 additions & 4 deletions Screenbox/Pages/AlbumDetailsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@
SizeChanged="AlbumArt_OnSizeChanged"
Translation="0,0,16">
<Grid.Background>
<ImageBrush ImageSource="{x:Bind ViewModel.Source.AlbumArt, Mode=OneWay}" Stretch="UniformToFill" />
<ImageBrush ImageSource="{x:Bind ViewModel.Source.AlbumArt, Mode=OneWay, FallbackValue={x:Null}}" Stretch="UniformToFill" />
</Grid.Background>
<Border
Background="{ThemeResource SolidBackgroundFillColorSecondaryBrush}"
CornerRadius="{x:Bind CoverArt.CornerRadius, Mode=OneWay}"
Visibility="{x:Bind ViewModel.Source.AlbumArt, Mode=OneWay, Converter={StaticResource EmptyObjectToVisibilityConverter}, ConverterParameter=true}">
Visibility="{x:Bind ViewModel.Source.AlbumArt, Mode=OneWay, Converter={StaticResource EmptyObjectToVisibilityConverter}, ConverterParameter=true, FallbackValue=Visible}">
<FontIcon
x:Name="CoverArtIcon"
FontSize="64"
Expand All @@ -134,7 +134,7 @@
<TextBlock
x:Name="TitleText"
Style="{StaticResource TitleMediumTextBlockStyle}"
Text="{x:Bind ViewModel.Source.Name}"
Text="{x:Bind ViewModel.Source.Name, FallbackValue={x:Null}}"
TextWrapping="NoWrap">
<interactivity:Interaction.Behaviors>
<interactions:OverflowTextToolTipBehavior />
Expand All @@ -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">
<interactivity:Interaction.Behaviors>
<interactions:OverflowTextToolTipBehavior />
Expand Down
2 changes: 1 addition & 1 deletion Screenbox/Pages/AlbumDetailsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion Screenbox/Pages/ArtistDetailsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
<TextBlock
x:Name="TitleText"
Style="{StaticResource TitleMediumTextBlockStyle}"
Text="{x:Bind ViewModel.Source.Name}"
Text="{x:Bind ViewModel.Source.Name, FallbackValue={x:Null}}"
TextWrapping="NoWrap">
<interactivity:Interaction.Behaviors>
<interactions:OverflowTextToolTipBehavior />
Expand Down
4 changes: 3 additions & 1 deletion Screenbox/Pages/ArtistDetailsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down