diff --git a/app/src/main/java/com/battlelancer/seriesguide/settings/TmdbSettings.kt b/app/src/main/java/com/battlelancer/seriesguide/settings/TmdbSettings.kt index 8b95e7e63e..8af3c9741b 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/settings/TmdbSettings.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/settings/TmdbSettings.kt @@ -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 = @@ -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) diff --git a/app/src/main/java/com/battlelancer/seriesguide/sync/SgSyncAdapter.kt b/app/src/main/java/com/battlelancer/seriesguide/sync/SgSyncAdapter.kt index 6bc68ee3d6..930277add8 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/sync/SgSyncAdapter.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/sync/SgSyncAdapter.kt @@ -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 diff --git a/app/src/main/java/com/battlelancer/seriesguide/sync/TmdbSync.kt b/app/src/main/java/com/battlelancer/seriesguide/sync/TmdbSync.kt index be8d9afc71..ec04c2c9ff 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/sync/TmdbSync.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/sync/TmdbSync.kt @@ -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 @@ -25,7 +23,7 @@ class TmdbSync internal constructor( private val movieTools: MovieTools ) { - fun updateConfigurationAndWatchProviders(progress: SyncProgress, prefs: SharedPreferences) { + fun updateConfigurationAndWatchProviders(progress: SyncProgress) { if (TmdbSettings.isConfigurationUpToDate(context)) { return } @@ -33,7 +31,7 @@ class TmdbSync internal constructor( var hadError = false // Image URL configuration - if (!updateConfiguration(prefs)) { + if (!updateConfiguration()) { hadError = true } @@ -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 { diff --git a/app/src/test/java/com/battlelancer/seriesguide/settings/TmdbSettingsTest.kt b/app/src/test/java/com/battlelancer/seriesguide/settings/TmdbSettingsTest.kt index 57d370c231..95cc14b034 100644 --- a/app/src/test/java/com/battlelancer/seriesguide/settings/TmdbSettingsTest.kt +++ b/app/src/test/java/com/battlelancer/seriesguide/settings/TmdbSettingsTest.kt @@ -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) + } + } \ No newline at end of file