Search in sources :

Example 6 with SyncSeason

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

the class TraktTools method processTraktEpisodes.

private boolean processTraktEpisodes(boolean isInitialSync, String seasonId, BaseSeason traktSeason, List<SyncSeason> syncSeasons, Flag flag) {
    HashSet<Integer> traktEpisodes = buildTraktEpisodesMap(traktSeason.episodes);
    Cursor localEpisodesQuery = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId), new String[] { SeriesGuideContract.Episodes._ID, SeriesGuideContract.Episodes.NUMBER, flag.databaseColumn }, null, null, null);
    if (localEpisodesQuery == null) {
        return false;
    }
    ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    List<SyncEpisode> syncEpisodes = new ArrayList<>();
    int episodesAddFlagCount = 0;
    int episodesRemoveFlagCount = 0;
    while (localEpisodesQuery.moveToNext()) {
        int episodeId = localEpisodesQuery.getInt(0);
        int episodeNumber = localEpisodesQuery.getInt(1);
        int flagValue = localEpisodesQuery.getInt(2);
        boolean isFlagged = flag == Flag.WATCHED ? EpisodeTools.isWatched(flagValue) : EpisodeTools.isCollected(flagValue);
        if (traktEpisodes.contains(episodeNumber)) {
            // episode watched/collected on trakt
            if (!isFlagged) {
                // set as watched/collected
                batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodeUri(episodeId)).withValue(flag.databaseColumn, flag.flaggedValue).build());
                episodesAddFlagCount++;
            }
        } else {
            // episode not watched/collected on trakt
            if (isFlagged) {
                if (isInitialSync) {
                    // upload to trakt
                    syncEpisodes.add(new SyncEpisode().number(episodeNumber));
                } else {
                    // set as not watched/collected if it is currently watched/collected
                    boolean isSkipped = flag == Flag.WATCHED && EpisodeTools.isSkipped(flagValue);
                    if (!isSkipped) {
                        batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodeUri(episodeId)).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
                        episodesRemoveFlagCount++;
                    }
                }
            }
        }
    }
    int localEpisodeCount = localEpisodesQuery.getCount();
    boolean addFlagToWholeSeason = episodesAddFlagCount == localEpisodeCount;
    boolean removeFlagFromWholeSeason = episodesRemoveFlagCount == localEpisodeCount;
    localEpisodesQuery.close();
    // if setting the whole season as (not) watched/collected, replace with single db op
    if (addFlagToWholeSeason || removeFlagFromWholeSeason) {
        batch.clear();
        batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId)).withValue(flag.databaseColumn, addFlagToWholeSeason ? flag.flaggedValue : flag.notFlaggedValue).build());
    }
    try {
        DBUtils.applyInSmallBatches(context, batch);
    } catch (OperationApplicationException e) {
        Timber.e(e, "Episodes watched/collected values database update failed.");
    }
    if (syncEpisodes.size() > 0) {
        syncSeasons.add(new SyncSeason().number(traktSeason.number).episodes(syncEpisodes));
    }
    return true;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) OperationApplicationException(android.content.OperationApplicationException) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason)

Example 7 with SyncSeason

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

the class TraktTools method buildSyncSeason.

/**
     * Returns a list of watched/collected episodes of a season. Packaged ready for upload to
     * trakt.
     */
private SyncSeason buildSyncSeason(String seasonTvdbId, int seasonNumber, Flag flag) {
    // query for watched/collected episodes of the given season
    Cursor flaggedEpisodesQuery = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonTvdbId), new String[] { SeriesGuideContract.Episodes.NUMBER }, flag.flagSelection, null, SeriesGuideContract.Episodes.SORT_NUMBER_ASC);
    if (flaggedEpisodesQuery == null) {
        // query failed
        return null;
    }
    List<SyncEpisode> syncEpisodes = new ArrayList<>();
    while (flaggedEpisodesQuery.moveToNext()) {
        int episodeNumber = flaggedEpisodesQuery.getInt(0);
        syncEpisodes.add(new SyncEpisode().number(episodeNumber));
    }
    flaggedEpisodesQuery.close();
    if (syncEpisodes.size() == 0) {
        // no episodes watched/collected
        return null;
    }
    return new SyncSeason().number(seasonNumber).episodes(syncEpisodes);
}
Also used : SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason)

Aggregations

SyncSeason (com.uwetrottmann.trakt5.entities.SyncSeason)6 Cursor (android.database.Cursor)5 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)4 ArrayList (java.util.ArrayList)3 ContentProviderOperation (android.content.ContentProviderOperation)2 OperationApplicationException (android.content.OperationApplicationException)2 SyncItems (com.uwetrottmann.trakt5.entities.SyncItems)2 SyncShow (com.uwetrottmann.trakt5.entities.SyncShow)2 Nullable (android.support.annotation.Nullable)1 BaseSeason (com.uwetrottmann.trakt5.entities.BaseSeason)1 SyncResponse (com.uwetrottmann.trakt5.entities.SyncResponse)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1