TheMovieDb API – Part 8 – Show me more info!

Sample ui view for movie details

Quickly open android studio! Welcome back to this series on implementing TheMovieDb API. As always remember all the code for this series will be in this GitHub repo today we are setting base to show off movie details!

Let’s begin! First since we are doing test driven development we want to keep our implementation separate, in other words, business logic outside of the UI. Create a MovieDetailsContract interface with the following:

public interface MovieDetailsContract {

    interface View {

        void setMoviePoster(String moviePoster);

        void setMovieBackdropImage(String backdropImage);

        void setTitle(String title);

        void setOverview(String overview);

        void setPopularity(Double popularity);

        void setReleaseDate(String releaseDate);
    }

    interface UserActionsListener {
        void showMovieDetails();
    }
}

Nothing too fancy here, under the View interface we have the properties we are going to display. You can add as many as you like, remeber TheMovieDb.org API is huge!

Next  let’s create the movie details presenter that will take care of things, in our case it would implement the showMovieDetails() method within the UserActionsListener interface:

public class MovieDetailsPresenter extends BasePresenter
        implements MovieDetailsContract.UserActionsListener {

    private MovieDetailsContract.View movieDetailsView;
    private Movie movie;

    public MovieDetailsPresenter(Movie movie, MovieDetailsContract.View movieDetailsView) {
        this.movie = movie;
        this.movieDetailsView = movieDetailsView;
    }

    @Override
    public void showMovieDetails() {
        if (IsNotEmpty(movie.backdrop_path)) {
            movieDetailsView.setMovieBackdropImage(Image.BASE_URL + Image.SIZE_W500 + movie.backdrop_path);
        }
        if (IsNotEmpty(movie.poster_path)) {
            movieDetailsView.setMoviePoster(Image.BASE_URL + Image.SIZE_W500 + movie.poster_path);
        }
        movieDetailsView.setTitle(movie.title);
        movieDetailsView.setOverview(movie.overview);
        movieDetailsView.setPopularity((movie.popularity == null) ? 0 : movie.popularity);
        movieDetailsView.setReleaseDate((String) android.text.format.DateFormat.format("yyyy", movie.release_date));
    }
}

Once again we are just simply mapping out properties to their respective view contract. Easy right!

Now testing time, I have created MovieDetailsPresenterTests unit test class under my test package and tested out all my methods in the MovieDetailsContract.View interface:

public class MovieDetailsPresenterTests {

    private MovieDetailsPresenter moviesPresenter;

    @Mock
    MovieDetailsContract.View moviesView;

    @Before
    public void setupNotesPresenter() {
        // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
        // inject the mocks in the test the initMocks method needs to be called.
        MockitoAnnotations.initMocks(this);
        com.jrosa.moviezone.domain.Movie movie = new com.jrosa.moviezone.domain.Movie();
        movie.id = 12345;
        movie.title = "foo";
        movie.overview = "an awesome foo movie";
        movie.popularity = 10.0;
        movie.backdrop_path = "";
        movie.poster_path = "";

        moviesPresenter = new MovieDetailsPresenter(movie, moviesView);
    }


    @Test
    public void movieDetailsView_setMoviePoster() {
        //Given the following movie poster
        moviesView.setMoviePoster("urlfakeposter");
        //image should have being placed
        verify(moviesView).setMoviePoster("urlfakeposter");
    }

    @Test
    public void movieDetailsView_setBackdropImage() {
        //Given the following backdrop image
        moviesView.setMovieBackdropImage("fakebackdroppath");
        //image should have being placed
        verify(moviesView).setMovieBackdropImage("fakebackdroppath");
    }

    @Test
    public void movieDetailsView_setTitle() {
        //Set  title
        moviesView.setTitle("John Doe Adventures");
        //Check that it was successfully set.
        verify(moviesView).setTitle("John Doe Adventures");
    }

    @Test
    public void movieDetailsView_setOverview() {
        //clear movie list.
        moviesView.setOverview("John Doe Adventures is a cool movie");
        //Check that it was successfully set.
        verify(moviesView).setOverview("John Doe Adventures is a cool movie");
    }

    @Test
    public void movieDetailsView_setPopularity() {
        moviesView.setPopularity(9.9);
        //check popularity
        verify(moviesView).setPopularity(9.9);
    }

    @Test
    public void movieDetailsView_setReleaseYear() {
        moviesView.setReleaseYear("2015");
        //check popularity
        verify(moviesView).setReleaseYear("2015");
    }
}

Awesome! For now I’m leaving it there so you can explore the additional properties from a Movie object in TheMovieDb.org API and decide if you want to add more! In the next blog post we will finish up by creating the MovieDetailsFragment, layout and mapped it all out!

Keep practicing,

Joel

Please follow and like us:
Facebook
Twitter
Joel Sosa

Author: Joel Sosa

Android nanodegree holder | Graduate Student @GeorgiaTech CS Interactive Intelligence | @gdgpuertorico Organizer

1 thought on “TheMovieDb API – Part 8 – Show me more info!”

Comments are closed.