diff --git a/app/src/main/java/eu/darken/octi/syncs/kserver/ui/KServerStateVH.kt b/app/src/main/java/eu/darken/octi/syncs/kserver/ui/KServerStateVH.kt index 0ea987f1..682ac49e 100644 --- a/app/src/main/java/eu/darken/octi/syncs/kserver/ui/KServerStateVH.kt +++ b/app/src/main/java/eu/darken/octi/syncs/kserver/ui/KServerStateVH.kt @@ -21,8 +21,15 @@ class KServerStateVH(parent: ViewGroup) : item: Item, payloads: List ) -> Unit = { item, _ -> - title.text = "${getString(R.string.sync_kserver_type_label)} (${item.credentials.serverAdress.domain})" + title.text = when { + item.credentials.serverAdress.domain.endsWith(".darken.eu") -> { + "${getString(R.string.sync_kserver_type_label)} (${item.credentials.serverAdress})" + } + else -> { + "${getString(R.string.sync_kserver_type_label)} (${item.credentials.serverAdress.address})" + } + } accountText.text = item.credentials.accountId.id lastSyncText.apply { diff --git a/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerFragment.kt b/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerFragment.kt index f08062a2..aae51319 100644 --- a/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerFragment.kt +++ b/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerFragment.kt @@ -44,8 +44,8 @@ class AddKServerFragment : Fragment3(R.layout.sync_add_new_kserver_fragment) { when (checkedId) { R.id.server_kserver_prod_item -> vm.selectType(KServer.Official.PROD) R.id.server_kserver_beta_item -> vm.selectType(KServer.Official.BETA) - R.id.server_kserver_dev_item -> vm.selectType(KServer.Official.DEV) R.id.server_kserver_local_item -> vm.selectType(KServer.Official.LOCAL) + R.id.server_kserver_custom_item -> vm.selectType(null) } } ui.serverKserverProdItem.apply { @@ -55,10 +55,6 @@ class AddKServerFragment : Fragment3(R.layout.sync_add_new_kserver_fragment) { text = "${KServer.Official.BETA.address.domain} (Beta)" isGone = true } - ui.serverKserverDevItem.apply { - text = "${KServer.Official.DEV.address.domain} (dev)" - isGone = true - } ui.serverKserverLocalItem.apply { text = "${KServer.Official.LOCAL.address.domain} (local)" isGone = !BuildConfigWrap.DEBUG @@ -68,15 +64,27 @@ class AddKServerFragment : Fragment3(R.layout.sync_add_new_kserver_fragment) { when (state.serverType) { KServer.Official.PROD -> serverGroup.check(R.id.server_kserver_prod_item) KServer.Official.BETA -> serverGroup.check(R.id.server_kserver_beta_item) - KServer.Official.DEV -> serverGroup.check(R.id.server_kserver_dev_item) KServer.Official.LOCAL -> serverGroup.check(R.id.server_kserver_local_item) + null -> serverGroup.check(R.id.server_kserver_custom_item) } + customServerInputLayout.isGone = state.serverType != null + createNewAccount.isEnabled = !state.isBusy linkExistingAccount.isEnabled = !state.isBusy serverGroup.isEnabled = !state.isBusy } - ui.createNewAccount.setOnClickListener { vm.createAccount() } + ui.apply { + createNewAccount.setOnClickListener { + vm.createAccount( + if (serverKserverCustomItem.isChecked) { + customServerInput.text.toString() + } else { + null + } + ) + } + } ui.linkExistingAccount.setOnClickListener { vm.linkAccount() } super.onViewCreated(view, savedInstanceState) diff --git a/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerVM.kt b/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerVM.kt index 75c23472..67d4833e 100644 --- a/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerVM.kt +++ b/app/src/main/java/eu/darken/octi/syncs/kserver/ui/add/AddKServerVM.kt @@ -7,7 +7,6 @@ import eu.darken.octi.common.debug.logging.Logging.Priority.INFO import eu.darken.octi.common.debug.logging.log import eu.darken.octi.common.debug.logging.logTag import eu.darken.octi.common.uix.ViewModel3 -import eu.darken.octi.sync.core.SyncSettings import eu.darken.octi.syncs.kserver.core.KServer import eu.darken.octi.syncs.kserver.core.KServerAccountRepo import eu.darken.octi.syncs.kserver.core.KServerEndpoint @@ -22,29 +21,40 @@ class AddKServerVM @Inject constructor( private val dispatcherProvider: DispatcherProvider, private val kServerAccountRepo: KServerAccountRepo, private val kServerEndpointFactory: KServerEndpoint.Factory, - private val syncSettings: SyncSettings, ) : ViewModel3(dispatcherProvider = dispatcherProvider) { data class State( - val serverType: KServer.Official = KServer.Official.PROD, + val serverType: KServer.Official? = KServer.Official.PROD, val isBusy: Boolean = false ) private val _state = MutableStateFlow(State()) val state = _state.asLiveData2() - fun selectType(type: KServer.Official) { + fun selectType(type: KServer.Official?) { log(TAG) { "selectType(type=$type)" } _state.value = _state.value.copy(serverType = type) } - fun createAccount() = launch { - log(TAG) { "createAccount()" } + private fun parseCustomServer(raw: String): KServer.Address { + val regex = Regex("([a-zA-Z]+)://([a-zA-Z0-9.-]+):(\\d+)") + val matchResult = regex.find(raw) ?: throw IllegalArgumentException("Invalid address input") + + val (protocol, address, port) = matchResult.destructured + return KServer.Address( + domain = address, + protocol = protocol, + port = port.toInt() + ) + } + + fun createAccount(customServer: String?) = launch { + log(TAG) { "createAccount($customServer)" } _state.value = _state.value.copy(isBusy = true) try { - val type = _state.value.serverType.address - log(TAG) { "createAccount(): $type" } - val endpoint = kServerEndpointFactory.create(type) + val address = _state.value.serverType?.address ?: parseCustomServer(customServer!!) + log(TAG) { "createAccount(): $address" } + val endpoint = kServerEndpointFactory.create(address) withContext(NonCancellable) { log(TAG) { "Creating account..." } diff --git a/app/src/main/res/layout/sync_add_new_kserver_fragment.xml b/app/src/main/res/layout/sync_add_new_kserver_fragment.xml index 1fd83fb2..a76f76b2 100644 --- a/app/src/main/res/layout/sync_add_new_kserver_fragment.xml +++ b/app/src/main/res/layout/sync_add_new_kserver_fragment.xml @@ -56,22 +56,46 @@ tools:ignore="HardcodedText" /> + + + + + + + app:layout_constraintTop_toBottomOf="@id/custom_server_input_layout" /> Background synchronization interval (in minutes). Background synchronization + Custom server + User interface Permission required diff --git a/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServer.kt b/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServer.kt index d09e09f4..67c2a9de 100644 --- a/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServer.kt +++ b/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServer.kt @@ -15,7 +15,6 @@ interface KServer { enum class Official(val address: Address) { @Json(name = "PROD") PROD(Address("prod.kserver.octi.darken.eu")), @Json(name = "BETA") BETA(Address("beta.kserver.octi.darken.eu")), - @Json(name = "DEV") DEV(Address("dev.kserver.octi.darken.eu")), @Json(name = "LOCAL") LOCAL(Address("blasphemy", protocol = "http", port = 8080)), } @@ -26,8 +25,8 @@ interface KServer { @Json(name = "protocol") val protocol: String = "https", @Json(name = "port") val port: Int = 443, ) : Parcelable { - val httpUrl: String - get() = "$protocol://$domain:$port/v1/" + val address: String + get() = "$protocol://$domain:$port" } @JsonClass(generateAdapter = true) diff --git a/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServerEndpoint.kt b/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServerEndpoint.kt index 631f0ebc..932b78bc 100644 --- a/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServerEndpoint.kt +++ b/syncs-kserver/src/main/java/eu/darken/octi/syncs/kserver/core/KServerEndpoint.kt @@ -40,7 +40,7 @@ class KServerEndpoint @AssistedInject constructor( private val api: KServerApi by lazy { Retrofit.Builder().apply { - baseUrl(serverAdress.httpUrl) + baseUrl("${serverAdress.address}/v1/") client(httpClient) addConverterFactory(MoshiConverterFactory.create(baseMoshi).asLenient()) }.build().create(KServerApi::class.java)