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:
parent
fc863e132d
commit
aa37826041
3 changed files with 46 additions and 19 deletions
|
|
@ -28,6 +28,7 @@ class BookshelfApiClientFactory
|
|||
@Synchronized
|
||||
fun provideClient(url: String): BookshelfApiService? {
|
||||
val fixedUrl = url.fixUriScheme()
|
||||
Log.d("BookshelfClientFactory", "provideClient(url='$url', fixed='$fixedUrl')")
|
||||
if (fixedUrl == cachedUrl && cachedClient != null) {
|
||||
return cachedClient
|
||||
}
|
||||
|
|
@ -48,8 +49,9 @@ class BookshelfApiClientFactory
|
|||
.build()
|
||||
.create(BookshelfApiService::class.java)
|
||||
}.onFailure {
|
||||
Log.e("BookshelfApiClientFactory", "Failed to create BookshelfApiService for $fixedUrl", it)
|
||||
Log.e("BookshelfClientFactory", "Failed to create BookshelfApiService for '$fixedUrl'", it)
|
||||
}.getOrNull().also {
|
||||
Log.d("BookshelfClientFactory", "Created client: ${it != null}")
|
||||
cachedUrl = fixedUrl
|
||||
cachedClient = it
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.dueattendant149.bookreader.data.remote.bookshelfapi
|
||||
|
||||
import android.util.Log
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import org.dueattendant149.bookreader.data.remote.bookshelfapi.model.DownloadRequest
|
||||
|
|
@ -30,7 +31,19 @@ class BookshelfApiRepository
|
|||
private val clientFactory: BookshelfApiClientFactory,
|
||||
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 {
|
||||
val client = client() ?: return false
|
||||
|
|
|
|||
|
|
@ -24,34 +24,46 @@ class ServerSettings
|
|||
) {
|
||||
private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
|
||||
init {
|
||||
// Pre-populate defaults for homelab if not set
|
||||
if (!prefs.contains(KEY_BOOKSHELF_URL)) {
|
||||
prefs.edit { putString(KEY_BOOKSHELF_URL, DEFAULT_BOOKSHELF_URL) }
|
||||
}
|
||||
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? {
|
||||
val stored = prefs.getString(KEY_BOOKSHELF_URL, null)
|
||||
return when {
|
||||
stored.isNullOrBlank() -> DEFAULT_BOOKSHELF_URL
|
||||
else -> stored
|
||||
}
|
||||
}
|
||||
|
||||
fun getBookshelfUrl(): String? = prefs.getString(KEY_BOOKSHELF_URL, DEFAULT_BOOKSHELF_URL)
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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() }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue