Search in sources :

Example 1 with EpisodeCheckin

use of com.uwetrottmann.trakt5.entities.EpisodeCheckin in project SeriesGuide by UweTrottmann.

the class TraktTask method doCheckInAction.

private TraktResponse doCheckInAction() {
    try {
        retrofit2.Response response;
        String message = mArgs.getString(InitBundle.MESSAGE);
        switch(mAction) {
            case CHECKIN_EPISODE:
                {
                    int episodeTvdbId = mArgs.getInt(InitBundle.EPISODE_TVDBID);
                    EpisodeCheckin checkin = new EpisodeCheckin.Builder(new SyncEpisode().id(EpisodeIds.tvdb(episodeTvdbId)), APP_VERSION, null).message(message).build();
                    response = traktCheckin.get().checkin(checkin).execute();
                    break;
                }
            case CHECKIN_MOVIE:
                {
                    int movieTmdbId = mArgs.getInt(InitBundle.MOVIE_TMDB_ID);
                    MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
                    response = traktCheckin.get().checkin(checkin).execute();
                    break;
                }
            default:
                throw new IllegalArgumentException("check-in action unknown.");
        }
        if (response.isSuccessful()) {
            return new TraktResponse(true, mContext.getString(R.string.checkin_success_trakt, mArgs.getString(InitBundle.TITLE)));
        } else {
            // check if the user wants to check-in, but there is already a check-in in progress
            CheckinError checkinError = trakt.get().checkForCheckinError(response);
            if (checkinError != null) {
                DateTime expiresAt = checkinError.expires_at;
                int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.getMillis() - System.currentTimeMillis()) / 1000);
                return new CheckinBlockedResponse(waitTimeMin);
            } else // check if item does not exist on trakt (yet)
            if (response.code() == 404) {
                return new TraktResponse(false, mContext.getString(R.string.trakt_error_not_exists));
            } else if (SgTrakt.isUnauthorized(mContext, response)) {
                return new TraktResponse(false, mContext.getString(R.string.trakt_error_credentials));
            } else {
                SgTrakt.trackFailedRequest(mContext, "check-in", response);
            }
        }
    } catch (IOException e) {
        SgTrakt.trackFailedRequest(mContext, "check-in", e);
    }
    // return generic failure message
    return buildErrorResponse();
}
Also used : CheckinError(com.uwetrottmann.trakt5.entities.CheckinError) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) EpisodeCheckin(com.uwetrottmann.trakt5.entities.EpisodeCheckin) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MovieCheckin(com.uwetrottmann.trakt5.entities.MovieCheckin) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie)

Aggregations

CheckinError (com.uwetrottmann.trakt5.entities.CheckinError)1 EpisodeCheckin (com.uwetrottmann.trakt5.entities.EpisodeCheckin)1 MovieCheckin (com.uwetrottmann.trakt5.entities.MovieCheckin)1 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)1 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)1 IOException (java.io.IOException)1 DateTime (org.joda.time.DateTime)1