Search in sources :

Example 6 with BaseShow

use of com.uwetrottmann.trakt5.entities.BaseShow in project SeriesGuide by UweTrottmann.

the class TraktTools method processTraktShows.

private int processTraktShows(@NonNull List<BaseShow> remoteShows, @NonNull HashSet<Integer> localShows, boolean isInitialSync, Flag flag) {
    HashMap<Integer, BaseShow> traktShows = buildTraktShowsMap(remoteShows);
    int uploadedShowsCount = 0;
    final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    for (Integer localShow : localShows) {
        if (traktShows.containsKey(localShow)) {
            // show watched/collected on trakt
            BaseShow traktShow = traktShows.get(localShow);
            int result = processTraktSeasons(isInitialSync, localShow, traktShow, flag);
            if (result < SUCCESS) {
                // processing seasons failed, give up.
                return result;
            }
            if (flag == Flag.WATCHED) {
                updateLastWatchedTime(localShow, traktShow, batch);
            }
        } else {
            // show not watched/collected on trakt
            // check if this is because the show can not be tracked with trakt (yet)
            // some shows only exist on TheTVDB, keep state local and maybe upload in the future
            Integer showTraktId = ShowTools.getShowTraktId(context, localShow);
            if (showTraktId != null) {
                if (isInitialSync) {
                    // upload all watched/collected episodes of the show
                    // do in between processing to stretch uploads over longer time periods
                    uploadEpisodes(localShow, showTraktId, flag);
                    uploadedShowsCount++;
                } else {
                    // set all watched/collected episodes of show not watched/collected
                    batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfShowUri(localShow)).withSelection(flag.clearFlagSelection, null).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
                }
            }
        }
    }
    try {
        DBUtils.applyInSmallBatches(context, batch);
    } catch (OperationApplicationException e) {
        Timber.e(e, "processTraktShows: failed to remove flag for %s.", flag.name);
    }
    if (uploadedShowsCount > 0) {
        Timber.d("processTraktShows: uploaded %s flags for %s complete shows.", flag.name, localShows.size());
    }
    return SUCCESS;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) BaseShow(com.uwetrottmann.trakt5.entities.BaseShow) ArrayList(java.util.ArrayList) OperationApplicationException(android.content.OperationApplicationException)

Aggregations

BaseShow (com.uwetrottmann.trakt5.entities.BaseShow)5 ArrayList (java.util.ArrayList)4 ContentProviderOperation (android.content.ContentProviderOperation)3 OperationApplicationException (android.content.OperationApplicationException)2 SuppressLint (android.annotation.SuppressLint)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 Show (com.battlelancer.seriesguide.dataliberation.model.Show)1 SearchResult (com.battlelancer.seriesguide.items.SearchResult)1 HexagonEpisodeSync (com.battlelancer.seriesguide.sync.HexagonEpisodeSync)1 TvdbException (com.battlelancer.seriesguide.thetvdbapi.TvdbException)1 ShowResult (com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult)1 TraktTools (com.battlelancer.seriesguide.util.TraktTools)1 BaseSeason (com.uwetrottmann.trakt5.entities.BaseSeason)1 Show (com.uwetrottmann.trakt5.entities.Show)1 SyncSeason (com.uwetrottmann.trakt5.entities.SyncSeason)1