From 2c5f44498b128f85488631fe7a02460122f7e039 Mon Sep 17 00:00:00 2001 From: J2ghz Date: Mon, 21 Nov 2016 09:57:43 +0100 Subject: [PATCH] Replace progress in SyncLibraryTask --- Trakt/Helpers/ISplittableProgress.cs | 4 + Trakt/Helpers/SplittableProgress.cs | 7 +- Trakt/ScheduledTasks/SyncLibraryTask.cs | 150 ++++++++++++------------ 3 files changed, 82 insertions(+), 79 deletions(-) diff --git a/Trakt/Helpers/ISplittableProgress.cs b/Trakt/Helpers/ISplittableProgress.cs index bdf9daa..de90354 100644 --- a/Trakt/Helpers/ISplittableProgress.cs +++ b/Trakt/Helpers/ISplittableProgress.cs @@ -2,6 +2,10 @@ { using System; + /// + /// Similar to , but it contains a split method and Report is relative, not absolute. + /// + /// The type of progress update value public interface ISplittableProgress : IProgress { ISplittableProgress Split(int parts); diff --git a/Trakt/Helpers/SplittableProgress.cs b/Trakt/Helpers/SplittableProgress.cs index 64756ed..b150934 100644 --- a/Trakt/Helpers/SplittableProgress.cs +++ b/Trakt/Helpers/SplittableProgress.cs @@ -2,7 +2,10 @@ { using System; - // Can't be generic, because it's impossible to do arithmetics on generics + /// + /// Similar to , but report is relative, not absolute. + /// + /// Can't be generic, because it's impossible to do arithmetics on generics public class SplittableProgress : Progress, ISplittableProgress { public SplittableProgress(Action handler) @@ -10,7 +13,7 @@ { } - public double Progress { get; private set; } + private double Progress { get; set; } ISplittableProgress ISplittableProgress.Split(int parts) { diff --git a/Trakt/ScheduledTasks/SyncLibraryTask.cs b/Trakt/ScheduledTasks/SyncLibraryTask.cs index 58a0e27..d3060d1 100644 --- a/Trakt/ScheduledTasks/SyncLibraryTask.cs +++ b/Trakt/ScheduledTasks/SyncLibraryTask.cs @@ -1,6 +1,4 @@ -using MediaBrowser.Model.Tasks; - -namespace Trakt.ScheduledTasks +namespace Trakt.ScheduledTasks { using System; using System.Collections.Generic; @@ -15,14 +13,15 @@ namespace Trakt.ScheduledTasks using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Entities; + using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; + using MediaBrowser.Model.Tasks; using Trakt.Api; using Trakt.Api.DataContracts.Sync; using Trakt.Helpers; using Trakt.Model; - using MediaBrowser.Model.IO; /// /// Task that will Sync each users local library with their respective trakt.tv profiles. This task will only include @@ -32,13 +31,26 @@ namespace Trakt.ScheduledTasks { //private readonly IHttpClient _httpClient; private readonly IJsonSerializer _jsonSerializer; + private readonly IUserManager _userManager; + private readonly ILogger _logger; + private readonly TraktApi _traktApi; + private readonly IUserDataManager _userDataManager; + private readonly ILibraryManager _libraryManager; - public SyncLibraryTask(ILogManager logger, IJsonSerializer jsonSerializer, IUserManager userManager, IUserDataManager userDataManager, IHttpClient httpClient, IServerApplicationHost appHost, IFileSystem fileSystem, ILibraryManager libraryManager) + public SyncLibraryTask( + ILogManager logger, + IJsonSerializer jsonSerializer, + IUserManager userManager, + IUserDataManager userDataManager, + IHttpClient httpClient, + IServerApplicationHost appHost, + IFileSystem fileSystem, + ILibraryManager libraryManager) { _jsonSerializer = jsonSerializer; _userManager = userManager; @@ -55,7 +67,10 @@ namespace Trakt.ScheduledTasks public string Key { - get { return "TraktSyncLibraryTask"; } + get + { + return "TraktSyncLibraryTask"; + } } /// @@ -72,10 +87,6 @@ namespace Trakt.ScheduledTasks return; } - // purely for progress reporting - var progPercent = 0.0; - var percentPerUser = 100 / users.Count; - foreach (var user in users) { var traktUser = UserHelper.GetTraktUser(user); @@ -87,10 +98,9 @@ namespace Trakt.ScheduledTasks continue; } - await SyncUserLibrary(user, traktUser, progPercent, percentPerUser, progress, cancellationToken) + await + SyncUserLibrary(user, traktUser, progress.Split(users.Count), cancellationToken) .ConfigureAwait(false); - - progPercent += percentPerUser; } } @@ -101,18 +111,18 @@ namespace Trakt.ScheduledTasks private async Task SyncUserLibrary( User user, TraktUser traktUser, - double progPercent, - double percentPerUser, - IProgress progress, + ISplittableProgress progress, CancellationToken cancellationToken) { // purely for progress reporting - var mediaItemsCount = _libraryManager.GetItemList(new InternalItemsQuery - { - IncludeItemTypes = new[] { typeof(Movie).Name, typeof(Episode).Name }, - ExcludeLocationTypes = new[] { LocationType.Virtual } - }) - .Count(i => _traktApi.CanSync(i, traktUser)); + var mediaItemsCount = + _libraryManager.GetItemList( + new InternalItemsQuery + { + IncludeItemTypes = new[] { typeof(Movie).Name, typeof(Episode).Name }, + ExcludeLocationTypes = new[] { LocationType.Virtual } + }) + .Count(i => _traktApi.CanSync(i, traktUser)); if (mediaItemsCount == 0) { @@ -122,10 +132,8 @@ namespace Trakt.ScheduledTasks _logger.Info(mediaItemsCount + " Items found for '" + user.Name + "'."); - var percentPerItem = (float)percentPerUser / mediaItemsCount / 2.0; - - await SyncMovies(user, traktUser, progress, progPercent, percentPerItem, cancellationToken); - await SyncShows(user, traktUser, progress, progPercent, percentPerItem, cancellationToken); + await SyncMovies(user, traktUser, progress.Split(2), cancellationToken); + await SyncShows(user, traktUser, progress.Split(2), cancellationToken); } /// @@ -134,9 +142,7 @@ namespace Trakt.ScheduledTasks private async Task SyncMovies( User user, TraktUser traktUser, - IProgress progress, - double progPercent, - double percentPerItem, + ISplittableProgress progress, CancellationToken cancellationToken) { /* @@ -159,17 +165,19 @@ namespace Trakt.ScheduledTasks var playedMovies = new List(); var unplayedMovies = new List(); + var decisionProgress = progress.Split(4).Split(libraryMovies.Count); foreach (var child in libraryMovies) { cancellationToken.ThrowIfCancellationRequested(); var libraryMovie = child as Movie; var userData = _userDataManager.GetUserData(user.Id, child); - + // if movie is not collected, or (export media info setting is enabled and every collected matching movie has different metadata), collect it var collectedMathingMovies = SyncFromTraktTask.FindMatches(libraryMovie, traktCollectedMovies).ToList(); if (!collectedMathingMovies.Any() || (traktUser.ExportMediaInfo - && collectedMathingMovies.All(collectedMovie => collectedMovie.MetadataIsDifferent(libraryMovie)))) + && collectedMathingMovies.All( + collectedMovie => collectedMovie.MetadataIsDifferent(libraryMovie)))) { collectedMovies.Add(libraryMovie); } @@ -188,7 +196,8 @@ namespace Trakt.ScheduledTasks else if (!traktUser.SkipUnwatchedImportFromTrakt) { userData.Played = false; - await _userDataManager.SaveUserData( + await + _userDataManager.SaveUserData( user.Id, libraryMovie, userData, @@ -206,28 +215,24 @@ namespace Trakt.ScheduledTasks } } - // purely for progress reporting - progPercent += percentPerItem; - progress.Report(progPercent); + decisionProgress.Report(100); } // send movies to mark collected - await SendMovieCollectionUpdates(true, traktUser, collectedMovies, progress, progPercent, percentPerItem, cancellationToken); + await SendMovieCollectionUpdates(true, traktUser, collectedMovies, progress.Split(4), cancellationToken); // send movies to mark watched - await SendMoviePlaystateUpdates(true, traktUser, playedMovies, progress, progPercent, percentPerItem, cancellationToken); + await SendMoviePlaystateUpdates(true, traktUser, playedMovies, progress.Split(4), cancellationToken); // send movies to mark unwatched - await SendMoviePlaystateUpdates(false, traktUser, unplayedMovies, progress, progPercent, percentPerItem, cancellationToken); + await SendMoviePlaystateUpdates(false, traktUser, unplayedMovies, progress.Split(4), cancellationToken); } private async Task SendMovieCollectionUpdates( bool collected, TraktUser traktUser, List movies, - IProgress progress, - double progPercent, - double percentPerItem, + ISplittableProgress progress, CancellationToken cancellationToken) { _logger.Info("Movies to " + (collected ? "add to" : "remove from") + " Collection: " + movies.Count); @@ -237,8 +242,11 @@ namespace Trakt.ScheduledTasks { var dataContracts = await - _traktApi.SendLibraryUpdateAsync(movies, traktUser, cancellationToken, collected ? EventType.Add : EventType.Remove) - .ConfigureAwait(false); + _traktApi.SendLibraryUpdateAsync( + movies, + traktUser, + cancellationToken, + collected ? EventType.Add : EventType.Remove).ConfigureAwait(false); if (dataContracts != null) { foreach (var traktSyncResponse in dataContracts) @@ -256,9 +264,7 @@ namespace Trakt.ScheduledTasks _logger.ErrorException("Exception handled sending movies to trakt.tv", e); } - // purely for progress reporting - progPercent += percentPerItem * movies.Count; - progress.Report(progPercent); + progress.Report(100); } } @@ -266,9 +272,7 @@ namespace Trakt.ScheduledTasks bool seen, TraktUser traktUser, List playedMovies, - IProgress progress, - double progPercent, - double percentPerItem, + ISplittableProgress progress, CancellationToken cancellationToken) { _logger.Info("Movies to set " + (seen ? string.Empty : "un") + "watched: " + playedMovies.Count); @@ -291,9 +295,7 @@ namespace Trakt.ScheduledTasks _logger.ErrorException("Error updating movie play states", e); } - // purely for progress reporting - progPercent += percentPerItem * playedMovies.Count; - progress.Report(progPercent); + progress.Report(100); } } @@ -303,9 +305,7 @@ namespace Trakt.ScheduledTasks private async Task SyncShows( User user, TraktUser traktUser, - IProgress progress, - double progPercent, - double percentPerItem, + ISplittableProgress progress, CancellationToken cancellationToken) { var traktWatchedShows = await _traktApi.SendGetWatchedShowsRequest(traktUser).ConfigureAwait(false); @@ -325,6 +325,7 @@ namespace Trakt.ScheduledTasks var playedEpisodes = new List(); var unplayedEpisodes = new List(); + var decisionProgress = progress.Split(4).Split(episodeItems.Count); foreach (var child in episodeItems) { cancellationToken.ThrowIfCancellationRequested(); @@ -352,7 +353,8 @@ namespace Trakt.ScheduledTasks else if (!traktUser.SkipUnwatchedImportFromTrakt) { userData.Played = false; - await _userDataManager.SaveUserData( + await + _userDataManager.SaveUserData( user.Id, episode, userData, @@ -375,25 +377,21 @@ namespace Trakt.ScheduledTasks collectedEpisodes.Add(episode); } - // purely for progress reporting - progPercent += percentPerItem; - progress.Report(progPercent); + decisionProgress.Report(100); } - await SendEpisodeCollectionUpdates(true, traktUser, collectedEpisodes, progress, progPercent, percentPerItem, cancellationToken); + await SendEpisodeCollectionUpdates(true, traktUser, collectedEpisodes, progress.Split(4), cancellationToken); - await SendEpisodePlaystateUpdates(true, traktUser, playedEpisodes, progress, progPercent, percentPerItem, cancellationToken); + await SendEpisodePlaystateUpdates(true, traktUser, playedEpisodes, progress.Split(4), cancellationToken); - await SendEpisodePlaystateUpdates(false, traktUser, unplayedEpisodes, progress, progPercent, percentPerItem, cancellationToken); + await SendEpisodePlaystateUpdates(false, traktUser, unplayedEpisodes, progress.Split(4), cancellationToken); } private async Task SendEpisodePlaystateUpdates( bool seen, TraktUser traktUser, List playedEpisodes, - IProgress progress, - double progPercent, - double percentPerItem, + ISplittableProgress progress, CancellationToken cancellationToken) { _logger.Info("Episodes to set " + (seen ? string.Empty : "un") + "watched: " + playedEpisodes.Count); @@ -414,9 +412,7 @@ namespace Trakt.ScheduledTasks _logger.ErrorException("Error updating episode play states", e); } - // purely for progress reporting - progPercent += percentPerItem * playedEpisodes.Count; - progress.Report(progPercent); + progress.Report(100); } } @@ -424,9 +420,7 @@ namespace Trakt.ScheduledTasks bool collected, TraktUser traktUser, List collectedEpisodes, - IProgress progress, - double progPercent, - double percentPerItem, + ISplittableProgress progress, CancellationToken cancellationToken) { _logger.Info("Episodes to add to Collection: " + collectedEpisodes.Count); @@ -436,8 +430,11 @@ namespace Trakt.ScheduledTasks { var dataContracts = await - _traktApi.SendLibraryUpdateAsync(collectedEpisodes, traktUser, cancellationToken, collected ? EventType.Add : EventType.Remove) - .ConfigureAwait(false); + _traktApi.SendLibraryUpdateAsync( + collectedEpisodes, + traktUser, + cancellationToken, + collected ? EventType.Add : EventType.Remove).ConfigureAwait(false); if (dataContracts != null) { foreach (var traktSyncResponse in dataContracts) @@ -455,9 +452,7 @@ namespace Trakt.ScheduledTasks _logger.ErrorException("Exception handled sending episodes to trakt.tv", e); } - // purely for progress reporting - progPercent += percentPerItem * collectedEpisodes.Count; - progress.Report(progPercent); + progress.Report(100); } } @@ -465,7 +460,8 @@ namespace Trakt.ScheduledTasks public string Category => "Trakt"; - public string Description => "Adds any media that is in each users trakt monitored locations to their trakt.tv profile"; + public string Description + => "Adds any media that is in each users trakt monitored locations to their trakt.tv profile"; private void LogTraktResponseDataContract(TraktSyncResponse dataContract) {