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;
}
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);
}
Aggregations