This commit is contained in:
Damontecres 2026-04-30 12:40:10 -04:00
parent f364ef9708
commit f02f66c506
No known key found for this signature in database
2 changed files with 63 additions and 15 deletions

View file

@ -74,6 +74,7 @@ class HomeSettingsService
constructor( constructor(
@param:ApplicationContext private val context: Context, @param:ApplicationContext private val context: Context,
private val api: ApiClient, private val api: ApiClient,
private val serverPluginApi: ServerPluginApi,
private val serverRepository: ServerRepository, private val serverRepository: ServerRepository,
private val userPreferencesService: UserPreferencesService, private val userPreferencesService: UserPreferencesService,
private val navDrawerService: NavDrawerService, private val navDrawerService: NavDrawerService,
@ -194,6 +195,14 @@ class HomeSettingsService
return HomePageSettings(rows, version) 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] * Loads [HomePageSettings] into [currentSettings]
* *
@ -205,21 +214,12 @@ class HomeSettingsService
Timber.v("Getting setting for %s", userId) Timber.v("Getting setting for %s", userId)
// User local then server/remote otherwise create a default // User local then server/remote otherwise create a default
val settings = val settings =
try { tryLoad {
val local = loadFromLocal(userId) serverPluginApi.fetchHomePageSettings()
Timber.v("Found local? %s", local != null) } ?: tryLoad {
local loadFromLocal(userId)
} catch (ex: Exception) { } ?: tryLoad {
Timber.w(ex, "Error loading local settings") loadFromServer(userId)
// 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
} }
val resolvedSettings = val resolvedSettings =
if (settings != null) { if (settings != null) {

View file

@ -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<HomePageSettings>(res.body.byteStream())
} else if (res.code == 404) {
null
} else {
throw ApiClientException(res.body.string())
}
}
}
}