Search in sources :

Example 1 with SgShow2Helper

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

the class HexagonSync method syncEpisodes.

private boolean syncEpisodes(@NonNull Map<Integer, Long> tmdbIdsToShowIds) {
    // get shows that need episode merging
    SgShow2Helper helper = SgRoomDatabase.getInstance(context).sgShow2Helper();
    List<SgShow2Ids> showsToMerge = helper.getHexagonMergeNotCompleted();
    // try merging episodes for them
    boolean mergeSuccessful = true;
    HexagonEpisodeSync episodeSync = new HexagonEpisodeSync(context, hexagonTools);
    for (SgShow2Ids show : showsToMerge) {
        // abort if connection is lost
        if (!AndroidUtils.isNetworkConnected(context)) {
            return false;
        }
        // TMDB ID is required, legacy shows with TVDB only data will no longer be synced.
        Integer showTmdbId = show.getTmdbId();
        if (showTmdbId == null || showTmdbId == 0)
            continue;
        boolean success = episodeSync.downloadFlags(show.getId(), showTmdbId, show.getTvdbId());
        if (!success) {
            // try again next time
            mergeSuccessful = false;
            continue;
        }
        success = episodeSync.uploadFlags(show.getId(), showTmdbId);
        if (success) {
            // set merge as completed
            helper.setHexagonMergeCompleted(show.getId());
        } else {
            mergeSuccessful = false;
        }
    }
    // download changed episodes and update properties on existing episodes
    boolean changedDownloadSuccessful = episodeSync.downloadChangedFlags(tmdbIdsToShowIds);
    return mergeSuccessful && changedDownloadSuccessful;
}
Also used : SgShow2Helper(com.battlelancer.seriesguide.provider.SgShow2Helper) SgShow2Ids(com.battlelancer.seriesguide.provider.SgShow2Ids)

Example 2 with SgShow2Helper

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

the class ShowSync method sync.

/**
 * Update shows based on the sync type.
 */
@SuppressLint("TimberExceptionLogging")
@Nullable
public SgSyncAdapter.UpdateResult sync(Context context, ShowTools showTools, long currentTime, SyncProgress progress) {
    hasUpdatedShows = false;
    List<Long> showsToUpdate = getShowsToUpdate(context, currentTime);
    if (showsToUpdate == null) {
        return null;
    }
    Timber.d("Updating %d show(s)...", showsToUpdate.size());
    // from here on we need more sophisticated abort handling, so keep track of errors
    SgSyncAdapter.UpdateResult resultCode = SgSyncAdapter.UpdateResult.SUCCESS;
    // loop through shows and download latest data from TVDb
    int consecutiveTimeouts = 0;
    for (Long showId : showsToUpdate) {
        // stop sync if connectivity is lost
        if (!AndroidUtils.isNetworkConnected(context)) {
            resultCode = SgSyncAdapter.UpdateResult.INCOMPLETE;
            break;
        }
        ShowResult result = showTools.updateShow(showId);
        if (result == ShowResult.SUCCESS) {
            hasUpdatedShows = true;
        } else {
            // failed, continue with other shows
            resultCode = SgSyncAdapter.UpdateResult.INCOMPLETE;
            SgShow2Helper helper = SgRoomDatabase.getInstance(context).sgShow2Helper();
            String showTitle = helper.getShowTitle(showId);
            Integer showTmdbId = helper.getShowTmdbId(showId);
            String message = String.format("Failed to update show ('%s', TMDB id %s).", showTitle, showTmdbId);
            if (result == ShowResult.DOES_NOT_EXIST) {
                message += " It no longer exists.";
            }
            progress.setImportantErrorIfNone(message);
            Timber.e(message);
            // Stop updating after multiple consecutive timeouts (around 3 * 15/20 seconds)
            if (result == ShowResult.TIMEOUT_ERROR) {
                consecutiveTimeouts++;
            } else if (consecutiveTimeouts > 0) {
                consecutiveTimeouts--;
            }
            if (consecutiveTimeouts == 3) {
                Timber.e("Connection unstable, give up.");
                return resultCode;
            }
        }
    }
    return resultCode;
}
Also used : SgShow2Helper(com.battlelancer.seriesguide.provider.SgShow2Helper) ShowResult(com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint) Nullable(androidx.annotation.Nullable)

Aggregations

SgShow2Helper (com.battlelancer.seriesguide.provider.SgShow2Helper)2 SuppressLint (android.annotation.SuppressLint)1 Nullable (androidx.annotation.Nullable)1 SgShow2Ids (com.battlelancer.seriesguide.provider.SgShow2Ids)1 ShowResult (com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult)1