Search in sources :

Example 1 with MovieDetails

use of com.battlelancer.seriesguide.items.MovieDetails in project SeriesGuide by UweTrottmann.

the class MovieTools method addMovie.

private boolean addMovie(int movieTmdbId, Lists listToAddTo) {
    // get movie info
    MovieDetails details = getMovieDetails(movieTmdbId);
    if (details.tmdbMovie() == null) {
        // abort if minimal data failed to load
        return false;
    }
    // build values
    ContentValues values = buildBasicMovieContentValuesWithId(details);
    // set flags
    values.put(SeriesGuideContract.Movies.IN_COLLECTION, DBUtils.convertBooleanToInt(listToAddTo == Lists.COLLECTION));
    values.put(SeriesGuideContract.Movies.IN_WATCHLIST, DBUtils.convertBooleanToInt(listToAddTo == Lists.WATCHLIST));
    // add to database
    context.getContentResolver().insert(SeriesGuideContract.Movies.CONTENT_URI, values);
    // ensure ratings and watched flags are downloaded on next sync
    TraktSettings.resetMoviesLastActivity(context);
    return true;
}
Also used : ContentValues(android.content.ContentValues) MovieDetails(com.battlelancer.seriesguide.items.MovieDetails)

Example 2 with MovieDetails

use of com.battlelancer.seriesguide.items.MovieDetails in project SeriesGuide by UweTrottmann.

the class MovieTools method buildMoviesContentValues.

private static ContentValues[] buildMoviesContentValues(List<MovieDetails> movies) {
    ContentValues[] valuesArray = new ContentValues[movies.size()];
    int index = 0;
    for (MovieDetails movie : movies) {
        valuesArray[index] = buildMovieContentValues(movie);
        index++;
    }
    return valuesArray;
}
Also used : ContentValues(android.content.ContentValues) SuppressLint(android.annotation.SuppressLint) MovieDetails(com.battlelancer.seriesguide.items.MovieDetails)

Example 3 with MovieDetails

use of com.battlelancer.seriesguide.items.MovieDetails in project SeriesGuide by UweTrottmann.

the class MovieLoader method loadInBackground.

@Override
public MovieDetails loadInBackground() {
    // try loading from trakt and tmdb, this might return a cached response
    MovieDetails details = app.getMovieTools().getMovieDetails(mTmdbId);
    // update local database
    updateLocalMovie(getContext(), details, mTmdbId);
    // fill in details from local database
    Cursor movieQuery = getContext().getContentResolver().query(Movies.buildMovieUri(mTmdbId), MovieQuery.PROJECTION, null, null, null);
    if (movieQuery == null || !movieQuery.moveToFirst() || movieQuery.getCount() < 1) {
        if (movieQuery != null) {
            movieQuery.close();
        }
        // ensure list flags and watched flag are false on failure
        // (assumption: movie not in db, it has the truth, so can't be in any lists or watched)
        details.inCollection = false;
        details.inWatchlist = false;
        details.isWatched = false;
        return details;
    }
    // set local state for watched, collected and watchlist status
    // assumption: local db has the truth for these
    details.inCollection = DBUtils.restoreBooleanFromInt(movieQuery.getInt(MovieQuery.IN_COLLECTION));
    details.inWatchlist = DBUtils.restoreBooleanFromInt(movieQuery.getInt(MovieQuery.IN_WATCHLIST));
    details.isWatched = DBUtils.restoreBooleanFromInt(movieQuery.getInt(MovieQuery.WATCHED));
    // also use local state of user rating
    details.userRating = movieQuery.getInt(MovieQuery.RATING_USER);
    // only overwrite other info if remote data failed to load
    if (details.traktRatings() == null) {
        details.traktRatings(new Ratings());
        details.traktRatings().rating = (double) movieQuery.getInt(MovieQuery.RATING_TRAKT);
        details.traktRatings().votes = movieQuery.getInt(MovieQuery.RATING_VOTES_TRAKT);
    }
    if (details.tmdbMovie() == null) {
        details.tmdbMovie(new Movie());
        details.tmdbMovie().imdb_id = movieQuery.getString(MovieQuery.IMDB_ID);
        details.tmdbMovie().title = movieQuery.getString(MovieQuery.TITLE);
        details.tmdbMovie().overview = movieQuery.getString(MovieQuery.OVERVIEW);
        details.tmdbMovie().poster_path = movieQuery.getString(MovieQuery.POSTER);
        details.tmdbMovie().runtime = movieQuery.getInt(MovieQuery.RUNTIME_MIN);
        details.tmdbMovie().vote_average = movieQuery.getDouble(MovieQuery.RATING_TMDB);
        details.tmdbMovie().vote_count = movieQuery.getInt(MovieQuery.RATING_VOTES_TMDB);
        // if stored release date is Long.MAX, movie has no release date
        long releaseDateMs = movieQuery.getLong(MovieQuery.RELEASED_UTC_MS);
        details.tmdbMovie().release_date = MovieTools.movieReleaseDateFrom(releaseDateMs);
    }
    // clean up
    movieQuery.close();
    return details;
}
Also used : Movie(com.uwetrottmann.tmdb2.entities.Movie) Cursor(android.database.Cursor) MovieDetails(com.battlelancer.seriesguide.items.MovieDetails) Ratings(com.uwetrottmann.trakt5.entities.Ratings)

Example 4 with MovieDetails

use of com.battlelancer.seriesguide.items.MovieDetails in project SeriesGuide by UweTrottmann.

the class MovieTools method getMovieDetails.

/**
     * Download movie data from trakt and TMDb.
     */
private MovieDetails getMovieDetails(String languageCode, int movieTmdbId) {
    MovieDetails details = new MovieDetails();
    // load ratings from trakt
    Integer movieTraktId = lookupTraktId(movieTmdbId);
    if (movieTraktId != null) {
        details.traktRatings(loadRatingsFromTrakt(movieTraktId));
    }
    // load summary from tmdb
    details.tmdbMovie(loadSummaryFromTmdb(languageCode, movieTmdbId));
    return details;
}
Also used : MovieDetails(com.battlelancer.seriesguide.items.MovieDetails)

Example 5 with MovieDetails

use of com.battlelancer.seriesguide.items.MovieDetails in project SeriesGuide by UweTrottmann.

the class MovieTools method addMovies.

/**
     * Adds new movies to the database.
     *
     * @param newCollectionMovies Movie TMDB ids to add to the collection.
     * @param newWatchlistMovies Movie TMDB ids to add to the watchlist.
     */
public UpdateResult addMovies(@NonNull Set<Integer> newCollectionMovies, @NonNull Set<Integer> newWatchlistMovies) {
    Timber.d("addMovies: %s to collection, %s to watchlist", newCollectionMovies.size(), newWatchlistMovies.size());
    // build a single list of tmdb ids
    Set<Integer> newMovies = new HashSet<>();
    for (Integer tmdbId : newCollectionMovies) {
        newMovies.add(tmdbId);
    }
    for (Integer tmdbId : newWatchlistMovies) {
        newMovies.add(tmdbId);
    }
    String languageCode = DisplaySettings.getMoviesLanguage(context);
    List<MovieDetails> movies = new LinkedList<>();
    // loop through ids
    for (Iterator<Integer> iterator = newMovies.iterator(); iterator.hasNext(); ) {
        int tmdbId = iterator.next();
        if (!AndroidUtils.isNetworkConnected(context)) {
            Timber.e("addMovies: no network connection");
            return UpdateResult.INCOMPLETE;
        }
        // download movie data
        MovieDetails movieDetails = getMovieDetails(languageCode, tmdbId);
        if (movieDetails.tmdbMovie() == null) {
            // skip if minimal values failed to load
            Timber.d("addMovies: downloaded movie %s incomplete, skipping", tmdbId);
            continue;
        }
        // set flags
        movieDetails.inCollection = newCollectionMovies.contains(tmdbId);
        movieDetails.inWatchlist = newWatchlistMovies.contains(tmdbId);
        movies.add(movieDetails);
        // add to database in batches of at most 10
        if (movies.size() == 10 || !iterator.hasNext()) {
            // insert into database
            context.getContentResolver().bulkInsert(SeriesGuideContract.Movies.CONTENT_URI, buildMoviesContentValues(movies));
            // start new batch
            movies.clear();
        }
    }
    return UpdateResult.SUCCESS;
}
Also used : LinkedList(java.util.LinkedList) MovieDetails(com.battlelancer.seriesguide.items.MovieDetails) SuppressLint(android.annotation.SuppressLint) HashSet(java.util.HashSet)

Aggregations

MovieDetails (com.battlelancer.seriesguide.items.MovieDetails)5 SuppressLint (android.annotation.SuppressLint)2 ContentValues (android.content.ContentValues)2 Cursor (android.database.Cursor)1 Movie (com.uwetrottmann.tmdb2.entities.Movie)1 Ratings (com.uwetrottmann.trakt5.entities.Ratings)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1