diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt b/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt index 784440aa..3af9ddf7 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt @@ -74,6 +74,7 @@ class HomeSettingsService constructor( @param:ApplicationContext private val context: Context, private val api: ApiClient, + private val serverPluginApi: ServerPluginApi, private val serverRepository: ServerRepository, private val userPreferencesService: UserPreferencesService, private val navDrawerService: NavDrawerService, @@ -194,6 +195,14 @@ class HomeSettingsService return HomePageSettings(rows, version) } + private suspend fun tryLoad(block: suspend () -> HomePageSettings?): HomePageSettings? = + try { + block.invoke() + } catch (ex: Exception) { + Timber.w(ex, "Error loading local settings") + null + } + /** * Loads [HomePageSettings] into [currentSettings] * @@ -205,21 +214,12 @@ class HomeSettingsService Timber.v("Getting setting for %s", userId) // User local then server/remote otherwise create a default val settings = - try { - val local = loadFromLocal(userId) - Timber.v("Found local? %s", local != null) - local - } catch (ex: Exception) { - Timber.w(ex, "Error loading local settings") - // TODO show toast? - null - } ?: try { - val remote = loadFromServer(userId) - Timber.v("Found remote? %s", remote != null) - remote - } catch (ex: Exception) { - Timber.w(ex, "Error loading remote settings") - null + tryLoad { + serverPluginApi.fetchHomePageSettings() + } ?: tryLoad { + loadFromLocal(userId) + } ?: tryLoad { + loadFromServer(userId) } val resolvedSettings = if (settings != null) { diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/ServerPluginApi.kt b/app/src/main/java/com/github/damontecres/wholphin/services/ServerPluginApi.kt new file mode 100644 index 00000000..9fe1def9 --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/services/ServerPluginApi.kt @@ -0,0 +1,48 @@ +package com.github.damontecres.wholphin.services + +import com.github.damontecres.wholphin.data.model.HomePageSettings +import com.github.damontecres.wholphin.services.hilt.AuthOkHttpClient +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.decodeFromStream +import okhttp3.OkHttpClient +import okhttp3.Request +import org.jellyfin.sdk.api.client.ApiClient +import org.jellyfin.sdk.api.client.exception.ApiClientException +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class ServerPluginApi + @Inject + constructor( + @param:AuthOkHttpClient private val okHttpClient: OkHttpClient, + private val api: ApiClient, + ) { + private fun createUrl(path: String): String? = + api.baseUrl?.let { if (it.endsWith("/")) "${it}wholphin/$path" else "$it/wholphin/$path" } + + companion object { + private const val HOME_CONFIG_PATH = "home" + } + + @OptIn(ExperimentalSerializationApi::class) + suspend fun fetchHomePageSettings(): HomePageSettings? { + val url = createUrl(HOME_CONFIG_PATH) ?: return null + val request = + Request + .Builder() + .url(url) + .get() + .build() + return okHttpClient.newCall(request).execute().use { res -> + if (res.isSuccessful) { + Json.decodeFromStream(res.body.byteStream()) + } else if (res.code == 404) { + null + } else { + throw ApiClientException(res.body.string()) + } + } + } + }