Skip to content

Recipes

In this section I’ll show you some examples and customizations which can be done with this library.

Caching

Since the API data isn’t updating that often it isn’t necessary to fetch the same resource over and over. You can set the cache time interval and the condition for being connected to the network:

// Setting condition or passing null to disable caching (default: null)
TMDb.onlineCondition { 
    return InternetUtility.isConnectedToNetwork()
}

// Duration to use cached data if connected to network (default: 2 hours)
TMDb.onlineCache(/* duration */ 2, /* unit */ TimeUnit.HOURS)

// Duration to use cached data if disconnected from network (default: 2 days)
TMDb.offlineCache(/* duration */ 1, /* unit */ TimeUnit.DAYS)

This setting modifies the cache control header of the requests made by OkHttp3Client / Retrofit2. You may also set the OkHttp3‘s Cache to the client:

fun setupTMDb() {
    // TODO
}

Modify requests before calling

By default, requests support caching, but you can add your own modifications by using TMDbInterceptor if you’d like:

fun setupTMDb() {
    val customInterceptor = TMDbInterceptor("YOUR_KEY", "") { requestBuilder ->
        requestBuilder.addHeader("headerName", "value") // or a custom offline handling
    }

    TMDb.tmdbInterceptor = customInterceptor
    TMDb.init("YOUR_KEY")
}