use of com.uwetrottmann.trakt5.entities.BaseSeason in project SeriesGuide by UweTrottmann.
the class TraktTools method processTraktSeasons.
/**
* Sync the watched/collected episodes of the given trakt show with the local episodes. The
* given show has to be watched/collected on trakt.
*
* @param isInitialSync If {@code true}, will upload watched/collected episodes that are not
* watched/collected on trakt. If {@code false}, will set them not watched/collected (if not
* skipped) to mirror the trakt episode.
*/
public int processTraktSeasons(boolean isInitialSync, int localShow, @NonNull BaseShow traktShow, @NonNull Flag flag) {
HashMap<Integer, BaseSeason> traktSeasons = buildTraktSeasonsMap(traktShow.seasons);
Cursor localSeasonsQuery = context.getContentResolver().query(SeriesGuideContract.Seasons.buildSeasonsOfShowUri(localShow), new String[] { SeriesGuideContract.Seasons._ID, SeriesGuideContract.Seasons.COMBINED }, null, null, null);
if (localSeasonsQuery == null) {
return FAILED;
}
final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
List<SyncSeason> syncSeasons = new ArrayList<>();
while (localSeasonsQuery.moveToNext()) {
String seasonId = localSeasonsQuery.getString(0);
int seasonNumber = localSeasonsQuery.getInt(1);
if (traktSeasons.containsKey(seasonNumber)) {
// season watched/collected on trakt
if (!processTraktEpisodes(isInitialSync, seasonId, traktSeasons.get(seasonNumber), syncSeasons, flag)) {
return FAILED;
}
} else {
// season not watched/collected on trakt
if (isInitialSync) {
// schedule all watched/collected episodes of this season for upload
SyncSeason syncSeason = buildSyncSeason(seasonId, seasonNumber, flag);
if (syncSeason != null) {
syncSeasons.add(syncSeason);
}
} else {
// set all watched/collected episodes of season not watched/collected
batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId)).withSelection(flag.clearFlagSelection, null).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
}
}
}
localSeasonsQuery.close();
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "Setting seasons unwatched failed.");
}
if (syncSeasons.size() > 0) {
// upload watched/collected episodes for this show
Integer showTraktId = ShowTools.getShowTraktId(context, localShow);
if (showTraktId == null) {
// show should have a trakt id, give up
return FAILED;
}
return uploadEpisodes(showTraktId, syncSeasons, flag);
} else {
return SUCCESS;
}
}
use of com.uwetrottmann.trakt5.entities.BaseSeason 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;
}
Aggregations