Skip to content

Commit

Permalink
TmdbSettings: wrap setting image URL with checks
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Feb 22, 2024
1 parent 4af621a commit 8ddf0bc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ object TmdbSettings {
week, updating weekly should be fine. */
private const val UPDATE_INTERVAL_MS = 7 * DateUtils.DAY_IN_MILLIS

const val KEY_TMDB_BASE_URL = "com.battlelancer.seriesguide.tmdb.baseurl"
private const val KEY_TMDB_BASE_URL = "com.battlelancer.seriesguide.tmdb.baseurl"
const val POSTER_SIZE_SPEC_W154 = "w154"
const val POSTER_SIZE_SPEC_W342 = "w342"
private const val STILL_SIZE_SPEC_W300 = "w300"
const val IMAGE_SIZE_SPEC_ORIGINAL = "original"
private const val DEFAULT_BASE_URL = "https://image.tmdb.org/t/p/"
private const val IMAGE_SIZE_SPEC_ORIGINAL = "original"
const val DEFAULT_BASE_URL = "https://image.tmdb.org/t/p/"

fun isConfigurationUpToDate(context: Context): Boolean {
val lastUpdatedMs =
Expand All @@ -37,6 +37,16 @@ object TmdbSettings {
}
}

/**
* Saves the base URL, unless it's empty or blank.
*/
fun setImageBaseUrl(context: Context, url: String) {
if (url.isEmpty() || url.isBlank()) return
PreferenceManager.getDefaultSharedPreferences(context).edit {
putString(KEY_TMDB_BASE_URL, url)
}
}

@JvmStatic
fun getImageBaseUrl(context: Context): String {
return PreferenceManager.getDefaultSharedPreferences(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SgSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, tru
// Get latest TMDb configuration.
val tmdbSync = TmdbSync(context, tmdbConfigService.get(), movieTools.get())
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
tmdbSync.updateConfigurationAndWatchProviders(progress, prefs)
tmdbSync.updateConfigurationAndWatchProviders(progress)

// Update show data.
// If failed for at least one show, do not proceed with other sync steps to avoid
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/com/battlelancer/seriesguide/sync/TmdbSync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package com.battlelancer.seriesguide.sync

import android.content.Context
import android.content.SharedPreferences
import android.text.format.DateUtils
import androidx.core.content.edit
import com.battlelancer.seriesguide.movies.MoviesSettings
import com.battlelancer.seriesguide.movies.tools.MovieTools
import com.battlelancer.seriesguide.provider.SgRoomDatabase
Expand All @@ -25,15 +23,15 @@ class TmdbSync internal constructor(
private val movieTools: MovieTools
) {

fun updateConfigurationAndWatchProviders(progress: SyncProgress, prefs: SharedPreferences) {
fun updateConfigurationAndWatchProviders(progress: SyncProgress) {
if (TmdbSettings.isConfigurationUpToDate(context)) {
return
}
// No need to abort on failure, can use default or last fetched config.
var hadError = false

// Image URL configuration
if (!updateConfiguration(prefs)) {
if (!updateConfiguration()) {
hadError = true
}

Expand All @@ -59,15 +57,14 @@ class TmdbSync internal constructor(
/**
* Downloads and stores the latest image url configuration from themoviedb.org.
*/
private fun updateConfiguration(prefs: SharedPreferences): Boolean {
private fun updateConfiguration(): Boolean {
try {
val response = configurationService.configuration().execute()
if (response.isSuccessful) {
val config = response.body()
if (!config?.images?.secure_base_url.isNullOrEmpty()) {
prefs.edit {
putString(TmdbSettings.KEY_TMDB_BASE_URL, config!!.images!!.secure_base_url)
}
val baseUrl = config?.images?.secure_base_url
if (baseUrl != null) {
TmdbSettings.setImageBaseUrl(context, baseUrl)
return true
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ class TmdbSettingsTest {
assertThat(TmdbSettings.isConfigurationUpToDate(context)).isTrue()
}

@Test
fun getImageBaseUrl() {
assertThat(TmdbSettings.getImageBaseUrl(context)).isEqualTo(TmdbSettings.DEFAULT_BASE_URL)

TmdbSettings.setImageBaseUrl(context, "")
assertThat(TmdbSettings.getImageBaseUrl(context)).isEqualTo(TmdbSettings.DEFAULT_BASE_URL)

TmdbSettings.setImageBaseUrl(context, " ")
assertThat(TmdbSettings.getImageBaseUrl(context)).isEqualTo(TmdbSettings.DEFAULT_BASE_URL)

val testValue = "placeholder"
TmdbSettings.setImageBaseUrl(context, testValue)
assertThat(TmdbSettings.getImageBaseUrl(context)).isEqualTo(testValue)
}

}

0 comments on commit 8ddf0bc

Please sign in to comment.