Search in sources :

Example 1 with SgEpisode2ForSync

use of com.battlelancer.seriesguide.provider.SgEpisode2ForSync in project SeriesGuide by UweTrottmann.

the class HexagonEpisodeSync method uploadFlags.

/**
 * Uploads all watched, skipped including plays or collected episodes of this show to Hexagon.
 *
 * @return Whether the upload was successful.
 */
boolean uploadFlags(long showId, int showTmdbId) {
    // query for watched, skipped or collected episodes
    List<SgEpisode2ForSync> episodesForSync = SgRoomDatabase.getInstance(context).sgEpisode2Helper().getEpisodesForHexagonSync(showId);
    if (episodesForSync.isEmpty()) {
        Timber.d("uploadFlags: uploading none for show %d", showId);
        return true;
    } else {
        // Issues with some requests failing at Cloud due to
        // EOFException: Unexpected end of ZLIB input stream
        // Using info log to report sizes that are uploaded to determine
        // if MAX_BATCH_SIZE is actually too large.
        // https://github.com/UweTrottmann/SeriesGuide/issues/781
        Timber.i("uploadFlags: uploading %d for show %d", episodesForSync.size(), showId);
    }
    // build list of episodes to upload
    List<SgCloudEpisode> episodes = new ArrayList<>();
    int count = episodesForSync.size();
    for (int i = 0; i < count; i++) {
        SgEpisode2ForSync episodeForSync = episodesForSync.get(i);
        SgCloudEpisode episode = new SgCloudEpisode();
        episode.setSeasonNumber(episodeForSync.getSeason());
        episode.setEpisodeNumber(episodeForSync.getNumber());
        int watchedFlag = episodeForSync.getWatched();
        if (!EpisodeTools.isUnwatched(watchedFlag)) {
            // Skipped or watched.
            episode.setWatchedFlag(watchedFlag);
            episode.setPlays(episodeForSync.getPlays());
        }
        if (episodeForSync.getCollected()) {
            episode.setIsInCollection(true);
        }
        episodes.add(episode);
        // upload a batch
        boolean isLast = i + 1 == count;
        if (episodes.size() == MAX_BATCH_SIZE || isLast) {
            SgCloudEpisodeList episodeList = new SgCloudEpisodeList();
            episodeList.setEpisodes(episodes);
            episodeList.setShowTmdbId(showTmdbId);
            try {
                // get service each time to check if auth was removed
                Episodes episodesService = hexagonTools.getEpisodesService();
                if (episodesService == null) {
                    return false;
                }
                episodesService.saveSgEpisodes(episodeList).execute();
            } catch (IOException e) {
                // abort
                Errors.logAndReportHexagon("save episodes of show", e);
                return false;
            }
            // clear array
            episodes = new ArrayList<>();
        }
    }
    return true;
}
Also used : SgCloudEpisode(com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisode) SgEpisode2ForSync(com.battlelancer.seriesguide.provider.SgEpisode2ForSync) SgCloudEpisodeList(com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisodeList) ArrayList(java.util.ArrayList) Episodes(com.uwetrottmann.seriesguide.backend.episodes.Episodes) IOException(java.io.IOException)

Aggregations

SgEpisode2ForSync (com.battlelancer.seriesguide.provider.SgEpisode2ForSync)1 Episodes (com.uwetrottmann.seriesguide.backend.episodes.Episodes)1 SgCloudEpisode (com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisode)1 SgCloudEpisodeList (com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisodeList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1