Add image server option to reader config sheet
parent
1f03e0a84b
commit
dfb50fbddc
@ -0,0 +1,85 @@
|
|||||||
|
package org.koitharu.kotatsu.reader.ui.config
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||||
|
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.mapToArray
|
||||||
|
import org.koitharu.kotatsu.parsers.config.ConfigKey
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
|
import org.koitharu.kotatsu.parsers.util.SuspendLazy
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
|
||||||
|
class ImageServerDelegate(
|
||||||
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||||
|
private val mangaSource: MangaSource?,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private val repositoryLazy = SuspendLazy {
|
||||||
|
mangaRepositoryFactory.create(checkNotNull(mangaSource)) as RemoteMangaRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun isAvailable() = withContext(Dispatchers.Default) {
|
||||||
|
repositoryLazy.tryGet().map { repository ->
|
||||||
|
repository.getConfigKeys().any { it is ConfigKey.PreferredImageServer }
|
||||||
|
}.getOrDefault(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getValue(): String? = withContext(Dispatchers.Default) {
|
||||||
|
repositoryLazy.tryGet().map { repository ->
|
||||||
|
val key = repository.getConfigKeys().firstNotNullOfOrNull { it as? ConfigKey.PreferredImageServer }
|
||||||
|
if (key != null) {
|
||||||
|
key.presetValues[repository.getConfig()[key]]
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}.getOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun showDialog(context: Context): Boolean {
|
||||||
|
val repository = withContext(Dispatchers.Default) {
|
||||||
|
repositoryLazy.tryGet().getOrNull()
|
||||||
|
} ?: return false
|
||||||
|
val key = repository.getConfigKeys().firstNotNullOfOrNull {
|
||||||
|
it as? ConfigKey.PreferredImageServer
|
||||||
|
} ?: return false
|
||||||
|
val entries = key.presetValues.values.mapToArray {
|
||||||
|
it ?: context.getString(R.string.automatic)
|
||||||
|
}
|
||||||
|
val entryValues = key.presetValues.keys.toTypedArray()
|
||||||
|
val config = repository.getConfig()
|
||||||
|
val initialValue = config[key]
|
||||||
|
var currentValue = initialValue
|
||||||
|
val changed = suspendCancellableCoroutine { cont ->
|
||||||
|
val dialog = MaterialAlertDialogBuilder(context)
|
||||||
|
.setTitle(R.string.image_server)
|
||||||
|
.setCancelable(true)
|
||||||
|
.setSingleChoiceItems(entries, entryValues.indexOf(initialValue)) { _, i ->
|
||||||
|
currentValue = entryValues[i]
|
||||||
|
}.setNegativeButton(android.R.string.cancel) { dialog, _ ->
|
||||||
|
dialog.cancel()
|
||||||
|
}.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||||
|
if (currentValue != initialValue) {
|
||||||
|
config[key] = currentValue
|
||||||
|
cont.resume(true)
|
||||||
|
} else {
|
||||||
|
cont.resume(false)
|
||||||
|
}
|
||||||
|
}.setOnCancelListener {
|
||||||
|
cont.resume(false)
|
||||||
|
}.create()
|
||||||
|
dialog.show()
|
||||||
|
cont.invokeOnCancellation {
|
||||||
|
dialog.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
repository.invalidateCache()
|
||||||
|
}
|
||||||
|
return changed
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:tint="?attr/colorControlNormal"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="#000"
|
||||||
|
android:pathData="M21,17H7V3H21M21,1H7A2,2 0 0,0 5,3V17A2,2 0 0,0 7,19H21A2,2 0 0,0 23,17V3A2,2 0 0,0 21,1M3,5H1V21A2,2 0 0,0 3,23H19V21H3M15.96,10.29L13.21,13.83L11.25,11.47L8.5,15H19.5L15.96,10.29Z" />
|
||||||
|
</vector>
|
||||||
Loading…
Reference in New Issue