fix(settings): always return defaults for blank URLs + add debug logging

- ServerSettings.getBookshelfUrl/getAbsUrl/getAbsToken: if stored value
  is null or blank, return hardcoded homelab default (books.dueattendant149.org).
  This fixes 'not configured' when user saved empty strings before defaults existed.
- setBookshelfUrl/setAbsUrl: if blank, store default instead of empty string.
- BookshelfApiRepository.client(): added Log.d for URL resolution.
- BookshelfApiClientFactory.provideClient(): added Log.d for URL and result.
- ':app:assembleOssDebug' passes, APK deployed.
This commit is contained in:
Atte149 2026-06-27 21:22:52 +03:00
parent fc863e132d
commit aa37826041
3 changed files with 46 additions and 19 deletions

View file

@ -28,6 +28,7 @@ class BookshelfApiClientFactory
@Synchronized @Synchronized
fun provideClient(url: String): BookshelfApiService? { fun provideClient(url: String): BookshelfApiService? {
val fixedUrl = url.fixUriScheme() val fixedUrl = url.fixUriScheme()
Log.d("BookshelfClientFactory", "provideClient(url='$url', fixed='$fixedUrl')")
if (fixedUrl == cachedUrl && cachedClient != null) { if (fixedUrl == cachedUrl && cachedClient != null) {
return cachedClient return cachedClient
} }
@ -48,8 +49,9 @@ class BookshelfApiClientFactory
.build() .build()
.create(BookshelfApiService::class.java) .create(BookshelfApiService::class.java)
}.onFailure { }.onFailure {
Log.e("BookshelfApiClientFactory", "Failed to create BookshelfApiService for $fixedUrl", it) Log.e("BookshelfClientFactory", "Failed to create BookshelfApiService for '$fixedUrl'", it)
}.getOrNull().also { }.getOrNull().also {
Log.d("BookshelfClientFactory", "Created client: ${it != null}")
cachedUrl = fixedUrl cachedUrl = fixedUrl
cachedClient = it cachedClient = it
} }

View file

@ -1,5 +1,6 @@
package org.dueattendant149.bookreader.data.remote.bookshelfapi package org.dueattendant149.bookreader.data.remote.bookshelfapi
import android.util.Log
import okhttp3.MultipartBody import okhttp3.MultipartBody
import okhttp3.RequestBody import okhttp3.RequestBody
import org.dueattendant149.bookreader.data.remote.bookshelfapi.model.DownloadRequest import org.dueattendant149.bookreader.data.remote.bookshelfapi.model.DownloadRequest
@ -30,7 +31,19 @@ class BookshelfApiRepository
private val clientFactory: BookshelfApiClientFactory, private val clientFactory: BookshelfApiClientFactory,
private val serverSettings: ServerSettings, private val serverSettings: ServerSettings,
) { ) {
private suspend fun client() = serverSettings.getBookshelfUrl()?.let { clientFactory.provideClient(it) } private suspend fun client(): BookshelfApiService? {
val url = serverSettings.getBookshelfUrl()
Log.d("BookshelfApiRepo", "getBookshelfUrl() = '$url'")
if (url.isNullOrBlank()) {
Log.e("BookshelfApiRepo", "URL is null or blank — server not configured")
return null
}
val client = clientFactory.provideClient(url)
if (client == null) {
Log.e("BookshelfApiRepo", "clientFactory.provideClient('$url') returned null")
}
return client
}
suspend fun isServerAvailable(): Boolean { suspend fun isServerAvailable(): Boolean {
val client = client() ?: return false val client = client() ?: return false

View file

@ -24,34 +24,46 @@ class ServerSettings
) { ) {
private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
init { fun getBookshelfUrl(): String? {
// Pre-populate defaults for homelab if not set val stored = prefs.getString(KEY_BOOKSHELF_URL, null)
if (!prefs.contains(KEY_BOOKSHELF_URL)) { return when {
prefs.edit { putString(KEY_BOOKSHELF_URL, DEFAULT_BOOKSHELF_URL) } stored.isNullOrBlank() -> DEFAULT_BOOKSHELF_URL
} else -> stored
if (!prefs.contains(KEY_ABS_URL)) {
prefs.edit { putString(KEY_ABS_URL, DEFAULT_ABS_URL) }
}
if (!prefs.contains(KEY_ABS_TOKEN)) {
prefs.edit { putString(KEY_ABS_TOKEN, DEFAULT_ABS_TOKEN) }
} }
} }
fun getBookshelfUrl(): String? = prefs.getString(KEY_BOOKSHELF_URL, DEFAULT_BOOKSHELF_URL)
fun setBookshelfUrl(url: String) { fun setBookshelfUrl(url: String) {
prefs.edit { putString(KEY_BOOKSHELF_URL, url.ifBlank { DEFAULT_BOOKSHELF_URL }) } val value = url.trim().ifBlank { DEFAULT_BOOKSHELF_URL }
prefs.edit { putString(KEY_BOOKSHELF_URL, value) }
} }
fun getAbsUrl(): String? = prefs.getString(KEY_ABS_URL, DEFAULT_ABS_URL) fun getAbsUrl(): String? {
val stored = prefs.getString(KEY_ABS_URL, null)
return when {
stored.isNullOrBlank() -> DEFAULT_ABS_URL
else -> stored
}
}
fun setAbsUrl(url: String) { fun setAbsUrl(url: String) {
prefs.edit { putString(KEY_ABS_URL, url.ifBlank { DEFAULT_ABS_URL }) } val value = url.trim().ifBlank { DEFAULT_ABS_URL }
prefs.edit { putString(KEY_ABS_URL, value) }
} }
fun getAbsToken(): String? = prefs.getString(KEY_ABS_TOKEN, DEFAULT_ABS_TOKEN) fun getAbsToken(): String? {
val stored = prefs.getString(KEY_ABS_TOKEN, null)
return when {
stored.isNullOrBlank() -> DEFAULT_ABS_TOKEN
else -> stored
}
}
fun setAbsToken(token: String) { fun setAbsToken(token: String) {
prefs.edit { putString(KEY_ABS_TOKEN, token) } val value = token.trim().ifBlank { DEFAULT_ABS_TOKEN }
prefs.edit { putString(KEY_ABS_TOKEN, value) }
}
fun clearAll() {
prefs.edit { clear() }
} }
} }