use of com.uwetrottmann.seriesguide.backend.episodes.model.EpisodeList in project SeriesGuide by UweTrottmann.
the class HexagonEpisodeSync method downloadFlagsByTvdbId.
private DownloadFlagsResult downloadFlagsByTvdbId(long showId, int showTvdbId) {
List<Episode> episodes;
boolean hasMoreEpisodes = true;
String cursor = null;
Long lastWatchedMs = null;
while (hasMoreEpisodes) {
// abort if connection is lost
if (!AndroidUtils.isNetworkConnected(context)) {
Timber.e("downloadFlags: no network connection");
return DownloadFlagsResult.FAILED;
}
try {
// get service each time to check if auth was removed
Episodes episodesService = hexagonTools.getEpisodesService();
if (episodesService == null) {
return DownloadFlagsResult.FAILED;
}
// build request
Episodes.Get request = episodesService.get().setShowTvdbId(// use default server limit
showTvdbId);
if (!TextUtils.isEmpty(cursor)) {
request.setCursor(cursor);
}
// execute request
EpisodeList response = request.execute();
if (response == null) {
// If empty should send status 200 and empty list, so no body is a failure.
return DownloadFlagsResult.FAILED;
}
episodes = response.getEpisodes();
// check for more items
if (response.getCursor() != null) {
cursor = response.getCursor();
} else {
hasMoreEpisodes = false;
}
} catch (IOException | IllegalArgumentException e) {
// Note: JSON parser may throw IllegalArgumentException.
Errors.logAndReportHexagon("get episodes of show", e);
return DownloadFlagsResult.FAILED;
}
if (episodes == null || episodes.size() == 0) {
// nothing to do here
break;
}
// build batch of episode flag updates
ArrayList<SgEpisode2UpdateByNumber> batch = new ArrayList<>();
for (Episode episode : episodes) {
Pair<SgEpisode2UpdateByNumber, Long> update = buildSgEpisodeUpdate(episode.getWatchedFlag(), episode.getPlays(), episode.getIsInCollection(), episode.getUpdatedAt(), episode.getEpisodeNumber(), episode.getSeasonNumber(), showId, lastWatchedMs);
if (update != null) {
batch.add(update.first);
lastWatchedMs = update.second;
}
}
// execute database update
SgRoomDatabase.getInstance(context).sgEpisode2Helper().updateWatchedAndCollectedByNumber(batch);
}
return new DownloadFlagsResult(true, false, lastWatchedMs);
}
Aggregations