Seperate the port and the server url in the preferences

This commit is contained in:
Syer10
2021-06-01 16:13:03 -04:00
parent 751c1ea083
commit 9d3907398f
10 changed files with 133 additions and 9 deletions

View File

@@ -16,6 +16,14 @@ class ServerPreferences(private val preferenceStore: PreferenceStore) {
}
fun server(): Preference<String> {
return preferenceStore.getString("server_url", "http://localhost:4567")
return preferenceStore.getString("server_url", "http://localhost")
}
fun port(): Preference<Int> {
return preferenceStore.getInt("server_port", 4567)
}
fun serverUrl(): Preference<String> {
return ServerUrlPreference("", server(), port())
}
}

View File

@@ -0,0 +1,58 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.data.server
import ca.gosyer.common.prefs.Preference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.stateIn
class ServerUrlPreference(
private val key: String,
private val server: Preference<String>,
private val port: Preference<Int>
) : Preference<String> {
override fun key(): String {
return key
}
override fun get(): String {
return server.get() + ":" + port.get()
}
override fun set(value: String) {
val (server, port) = value.split(':')
this.server.set(server)
this.port.set(port.toInt())
}
override fun isSet(): Boolean {
return server.isSet() || port.isSet()
}
override fun delete() {
server.delete()
port.delete()
}
override fun defaultValue(): String {
return server.defaultValue() + ":" + port.defaultValue()
}
override fun changes(): Flow<String> {
return merge(server.changes(), port.changes())
.map { get() }
}
override fun stateIn(scope: CoroutineScope): StateFlow<String> {
return changes().stateIn(scope, SharingStarted.Eagerly, get())
}
}

View File

@@ -22,7 +22,7 @@ open class BaseInteractionHandler(
protected val client: Http,
serverPreferences: ServerPreferences
) {
private val _serverUrl = serverPreferences.server()
private val _serverUrl = serverPreferences.serverUrl()
val serverUrl get() = _serverUrl.get()
protected inline fun <T> repeat(block: () -> T): T {

View File

@@ -0,0 +1,53 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.ui.base.prefs
import ca.gosyer.common.prefs.Preference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
fun Preference<Int>.asStringStateIn(scope: CoroutineScope): PreferenceMutableStateFlow<String> {
return PreferenceMutableStateFlow(IntStringPreference(this), scope)
}
class IntStringPreference(private val int: Preference<Int>) : Preference<String> {
override fun key(): String {
return int.key()
}
override fun get(): String {
return int.get().toString()
}
override fun set(value: String) {
value.toIntOrNull()?.let { int.set(it) }
}
override fun isSet(): Boolean {
return int.isSet()
}
override fun delete() {
int.delete()
}
override fun defaultValue(): String {
return int.defaultValue().toString()
}
override fun changes(): Flow<String> {
return int.changes().map { it.toString() }
}
override fun stateIn(scope: CoroutineScope): StateFlow<String> {
return changes().stateIn(scope, SharingStarted.Eagerly, get())
}
}

View File

@@ -23,7 +23,7 @@ class ExtensionsMenuViewModel @Inject constructor(
serverPreferences: ServerPreferences,
private val extensionPreferences: ExtensionPreferences
) : ViewModel() {
val serverUrl = serverPreferences.server().stateIn(scope)
val serverUrl = serverPreferences.serverUrl().stateIn(scope)
private lateinit var extensionList: List<Extension>

View File

@@ -36,7 +36,7 @@ class LibraryScreenViewModel @Inject constructor(
libraryPreferences: LibraryPreferences,
serverPreferences: ServerPreferences,
) : ViewModel() {
val serverUrl = serverPreferences.server().stateIn(scope)
val serverUrl = serverPreferences.serverUrl().stateIn(scope)
private val library = Library(MutableStateFlow(emptyList()), mutableMapOf())
val categories = library.categories.asStateFlow()

View File

@@ -35,7 +35,7 @@ class MangaMenuViewModel @Inject constructor(
private val libraryHandler: LibraryInteractionHandler,
serverPreferences: ServerPreferences
) : ViewModel() {
val serverUrl = serverPreferences.server().stateIn(scope)
val serverUrl = serverPreferences.serverUrl().stateIn(scope)
private val _manga = MutableStateFlow<Manga?>(null)
val manga = _manga.asStateFlow()

View File

@@ -15,6 +15,7 @@ import ca.gosyer.ui.base.components.Toolbar
import ca.gosyer.ui.base.prefs.EditTextPreference
import ca.gosyer.ui.base.prefs.SwitchPreference
import ca.gosyer.ui.base.prefs.asStateIn
import ca.gosyer.ui.base.prefs.asStringStateIn
import ca.gosyer.ui.base.vm.ViewModel
import ca.gosyer.ui.base.vm.viewModel
import ca.gosyer.ui.main.Route
@@ -25,7 +26,8 @@ class SettingsServerViewModel @Inject constructor(
private val serverPreferences: ServerPreferences
) : ViewModel() {
val host = serverPreferences.host().asStateIn(scope)
val serverUrl = serverPreferences.server().asStateIn(scope)
val server = serverPreferences.server().asStateIn(scope)
val port = serverPreferences.port().asStringStateIn(scope)
}
@Composable
@@ -36,7 +38,10 @@ fun SettingsServerScreen(navController: BackStack<Route>) {
SwitchPreference(preference = vm.host, title = "Host server inside TachideskJUI")
LazyColumn {
item {
EditTextPreference(vm.serverUrl, "Server Url", subtitle = vm.serverUrl.collectAsState().value)
EditTextPreference(vm.server, "Server Url", subtitle = vm.server.collectAsState().value)
}
item {
EditTextPreference(vm.port, "Server Url", subtitle = vm.port.collectAsState().value)
}
}
}

View File

@@ -28,7 +28,7 @@ class SourcesMenuViewModel @Inject constructor(
serverPreferences: ServerPreferences,
catalogPreferences: CatalogPreferences
) : ViewModel() {
val serverUrl = serverPreferences.server().stateIn(scope)
val serverUrl = serverPreferences.serverUrl().stateIn(scope)
private val languages = catalogPreferences.languages().stateIn(scope)

View File

@@ -32,7 +32,7 @@ class SourceScreenViewModel @Inject constructor(
private lateinit var source: Source
private var bundle: Bundle? = null
val serverUrl = serverPreferences.server().stateIn(scope)
val serverUrl = serverPreferences.serverUrl().stateIn(scope)
private val _mangas = MutableStateFlow(emptyList<Manga>())
val mangas = _mangas.asStateFlow()