Search in sources :

Example 1 with SgShow2CloudUpdate

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

the class HexagonShowSync method uploadAll.

/**
 * Uploads all local shows to Hexagon.
 */
public boolean uploadAll() {
    Timber.d("uploadAll: uploading all shows");
    List<SgShow2CloudUpdate> forCloudUpdate = SgRoomDatabase.getInstance(context).sgShow2Helper().getForCloudUpdate();
    List<SgCloudShow> shows = new LinkedList<>();
    for (SgShow2CloudUpdate localShow : forCloudUpdate) {
        if (localShow.getTmdbId() == null)
            continue;
        SgCloudShow show = new SgCloudShow();
        show.setTmdbId(localShow.getTmdbId());
        show.setIsFavorite(localShow.getFavorite());
        show.setNotify(localShow.getNotify());
        show.setIsHidden(localShow.getHidden());
        show.setLanguage(localShow.getLanguage());
        shows.add(show);
    }
    if (shows.size() == 0) {
        Timber.d("uploadAll: no shows to upload");
        // nothing to upload
        return true;
    }
    return upload(shows);
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) SgShow2CloudUpdate(com.battlelancer.seriesguide.provider.SgShow2CloudUpdate) LinkedList(java.util.LinkedList)

Example 2 with SgShow2CloudUpdate

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

the class HexagonShowSync method appendShowUpdates.

private void appendShowUpdates(List<SgShow2CloudUpdate> updates, Set<Long> toUpdate, Map<Integer, SearchResult> toAdd, List<SgCloudShow> shows, Map<Integer, Long> tmdbIdsToShowIds, boolean mergeValues) {
    for (SgCloudShow show : shows) {
        // schedule to add shows not in local database
        Integer showTmdbId = show.getTmdbId();
        // Invalid data.
        if (showTmdbId == null)
            continue;
        Long showIdOrNull = tmdbIdsToShowIds.get(showTmdbId);
        if (showIdOrNull == null) {
            // ...but do NOT add shows marked as removed
            if (show.getIsRemoved() != null && show.getIsRemoved()) {
                continue;
            }
            if (!toAdd.containsKey(showTmdbId)) {
                SearchResult item = new SearchResult();
                item.setTmdbId(showTmdbId);
                item.setLanguage(show.getLanguage());
                item.setTitle("");
                toAdd.put(showTmdbId, item);
            }
        } else if (!toUpdate.contains(showIdOrNull)) {
            // Create update if there isn't already one.
            SgShow2CloudUpdate update = SgRoomDatabase.getInstance(context).sgShow2Helper().getForCloudUpdate(showIdOrNull);
            if (update != null) {
                boolean hasUpdates = false;
                if (show.getIsFavorite() != null) {
                    // when merging, favorite shows, but never unfavorite them
                    if (!mergeValues || show.getIsFavorite()) {
                        update.setFavorite(show.getIsFavorite());
                        hasUpdates = true;
                    }
                }
                if (show.getNotify() != null) {
                    // when merging, enable notifications, but never disable them
                    if (!mergeValues || show.getNotify()) {
                        update.setNotify(show.getNotify());
                        hasUpdates = true;
                    }
                }
                if (show.getIsHidden() != null) {
                    // when merging, un-hide shows, but never hide them
                    if (!mergeValues || !show.getIsHidden()) {
                        update.setHidden(show.getIsHidden());
                        hasUpdates = true;
                    }
                }
                if (!TextUtils.isEmpty(show.getLanguage())) {
                    // always overwrite with hexagon language value
                    update.setLanguage(show.getLanguage());
                    hasUpdates = true;
                }
                if (hasUpdates) {
                    updates.add(update);
                    toUpdate.add(showIdOrNull);
                }
            }
        }
    }
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) SearchResult(com.battlelancer.seriesguide.ui.search.SearchResult) SgShow2CloudUpdate(com.battlelancer.seriesguide.provider.SgShow2CloudUpdate)

Example 3 with SgShow2CloudUpdate

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

the class HexagonShowSync method download.

/**
 * Downloads shows from Hexagon and updates existing shows with new property values. Any
 * shows not yet in the local database, determined by the given TMDB ID map, will be added
 * to the given map.
 *
 * When merging shows (e.g. just signed in) also downloads legacy cloud shows.
 */
public boolean download(Map<Integer, Long> tmdbIdsToShowIds, HashMap<Integer, SearchResult> toAdd, boolean hasMergedShows) {
    List<SgShow2CloudUpdate> updates = new ArrayList<>();
    Set<Long> toUpdate = new HashSet<>();
    long currentTime = System.currentTimeMillis();
    DateTime lastSyncTime = new DateTime(HexagonSettings.getLastShowsSyncTime(context));
    if (hasMergedShows) {
        Timber.d("download: changed shows since %s", lastSyncTime);
    } else {
        Timber.d("download: all shows");
    }
    boolean success = downloadShows(updates, toUpdate, toAdd, tmdbIdsToShowIds, hasMergedShows, lastSyncTime);
    if (!success)
        return false;
    // to encourage users to update the app.
    if (!hasMergedShows) {
        boolean successLegacy = downloadLegacyShows(updates, toUpdate, toAdd, tmdbIdsToShowIds);
        if (!successLegacy)
            return false;
    }
    // Apply all updates
    SgRoomDatabase.getInstance(context).sgShow2Helper().updateForCloudUpdate(updates);
    if (hasMergedShows) {
        // set new last sync time
        PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(HexagonSettings.KEY_LAST_SYNC_SHOWS, currentTime).apply();
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) SgShow2CloudUpdate(com.battlelancer.seriesguide.provider.SgShow2CloudUpdate) DateTime(com.google.api.client.util.DateTime) HashSet(java.util.HashSet)

Aggregations

SgShow2CloudUpdate (com.battlelancer.seriesguide.provider.SgShow2CloudUpdate)3 SgCloudShow (com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow)2 SearchResult (com.battlelancer.seriesguide.ui.search.SearchResult)1 DateTime (com.google.api.client.util.DateTime)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1