Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bburky committed Nov 3, 2021
1 parent 2d6308c commit 7722013
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
36 changes: 22 additions & 14 deletions Playlist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,30 @@ private void UpdatePlaylistFile()

public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
// Initialization is done inside OnApplicationStarted, otherwise
// loadPlaylistFile runs too early in Playnite's startup and
// cannot call PlayniteApi.Database.Games.Get()

PlaylistGames = new ObservableCollection<Game>(LoadPlaylistFile());
PlaylistGames.CollectionChanged += (sender, changedArgs) =>
{
UpdatePlaylistFile();
};
PlayniteApi.Database.Games.ItemCollectionChanged += (sender, changedArgs) =>
try
{
foreach (Game game in changedArgs.RemovedItems)
// Initialization is done inside OnApplicationStarted, otherwise
// loadPlaylistFile runs too early in Playnite's startup and
// cannot call PlayniteApi.Database.Games.Get()

PlaylistGames = new ObservableCollection<Game>(LoadPlaylistFile());
PlaylistGames.CollectionChanged += (sender, changedArgs) =>
{
PlaylistGames.Remove(game);
}
};
UpdatePlaylistFile();
};
PlayniteApi.Database.Games.ItemCollectionChanged += (sender, changedArgs) =>
{
foreach (Game game in changedArgs.RemovedItems)
{
PlaylistGames.Remove(game);
}
};
}
catch (Exception e)
{
logger.Error(e, "Error loading PlaylistGames in OnApplicationStarted");
PlayniteApi.Notifications.Add($"{Id}-OnApplicationStarted", $"Playlist extension could not load file: {e.Message}", NotificationType.Error);
}
}
}
}
5 changes: 3 additions & 2 deletions PlaylistViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Playnite.SDK;
using Playnite.SDK.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -49,8 +50,8 @@ public IEnumerable<KeyValuePair<CompletionStatus, RelayCommand<IEnumerable<objec

public PlaylistViewModel(ObservableCollection<Game> playlistGames, IPlayniteAPI playniteApi)
{
PlaylistGames = playlistGames;
this.playniteApi = playniteApi;
PlaylistGames = playlistGames ?? throw new ArgumentNullException(nameof(playlistGames));
this.playniteApi = playniteApi ?? throw new ArgumentNullException(nameof(playniteApi));

NavigateBackCommand = new RelayCommand<object>((a) =>
{
Expand Down

0 comments on commit 7722013

Please sign in to comment.