TheMovieDb API – Part 3 – Helper, helpers

TheMovieDb API android app screenshots

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 going to set up what I like to call “tool chain” classes, just popped out of my mind, this classes contain methods that will make checking things like network status a breeze. Don’t worry today post is shorter o you can recap on the previous parts. Let’s begin!

First let’s make a class called App that extends (inherits) from Application and place it under the AndroidManifest.xml, within it we are going to place helper methods such as isNetworkAvailable() and  weAreInATableSiizeDevice() this last one will help later on when building layouts for multiple form factors.

public class App extends Application {

    /**
     * Detects where or not there is a network available.
     *
     * @return networkAvailability
     */
    protected static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    /**
     * Returns true if the dimensions under the configuration are considered of a  tablet sized device.
     *
     * @param context
     * @return true if in a tablet sized device.
     */
    protected static boolean weAreInATabletSizeDevice(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

And don’t forget to place under your manifest.

<application
    android:name="App"
...

Your MainActivity launch mode should be singleTop

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop">
...

Great! Now let’s quickly move on how we are going to architect our application. Model View Presenter or MVP for shorts has gotten quite the popularity among android developers and this because it helps separate out source code in a way that is more testable and mockable. This of course comes with the cost of more classes, interfaces and ultimately code but hey,

If you can test it you can make it! – Joel R Sosa

So we are going to use MVP thouth the application. Let’s make a BasePresenter abstract class, any object oriented lovers?, within it we will have a very useful IsNotEmpty method to evaluate values later on. Remember APIs responses and their objects may or not have the data complete to fill out properties it is important to validate and make sure we get the behavior we expect.

public abstract class BasePresenter {

    /**
     * Returns true if @param val is null or empty. ""
     *
     * @param val the value to evaluate
     * @return a boolean struct of true if value is not empty.
     */
    protected boolean IsNotEmpty(String val) {
        return !TextUtils.isEmpty(val);
    }
}

Awesome! Now you are all set up. Make sure to recap and that you have everything we have cover up so far all the way from dependencies within build.gradle to unit tests we placed so far.

See you next time,

Joel

Please follow and like us:
Facebook
Twitter
Joel Sosa

Author: Joel Sosa

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