/// <summary> /// Gets the latest Watchlist from Trakt. /// </summary> public async void GetWatchlist() { var traktWatchlist = (await Client.Sync.GetWatchlistMoviesAsync()).ToList(); foreach (var item in traktWatchlist) { // TODO: Should we skip (or remove) items that are already collected? WatchlistMovie movie = localWatchlist.AsEnumerable().FirstOrDefault(x => MoviesMatch(item.Movie, x)); if (movie != null) { // Update local data from watchlist movie.TraktId = item.Movie.Ids.Trakt ?? 0; movie.Title = item.Movie.Title; movie.Year = item.Movie.Year ?? 0; continue; } movie = new WatchlistMovie(item.Movie.Title, item.Movie.Year ?? 0, item.Movie.Ids.Trakt ?? 0); localWatchlist.Add(movie); } foreach (var item in localWatchlist) { // Mark as removed so we can clean them up later if (!traktWatchlist.Any(x => MoviesMatch(x.Movie, item))) { item.Removed = true; // TODO: Make it an option to cancel scheduled recordings if an item is removed? } } // Find anything marked as removed that either: // a. hasn't been scheduled yet, or // b. is finished recording. // Those are safe to clean up. var purgeList = localWatchlist.Where(x => x.Removed && (!x.Scheduled || x.Completed)).ToList(); foreach (var item in purgeList) { localWatchlist.Remove(item); } await schedulerDb.SaveChangesAsync(); await localWatchlist.LoadAsync(); OnPropertyChanged("Watchlist"); }
private static bool MoviesMatch(TraktMovie traktMovie, WatchlistMovie watchlistMovie) { // Try matching by ids first if (watchlistMovie.TraktId == traktMovie.Ids.Trakt) { return true; } // Fall back to title and year match (but only if we don't know the Trakt id) if (watchlistMovie.TraktId == 0 && watchlistMovie.Title == traktMovie.Title && watchlistMovie.Year == (traktMovie.Year ?? 0)) { return true; } return false; }