Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow entry of custom address for KServer connectors #73

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ class KServerStateVH(parent: ViewGroup) :
item: Item,
payloads: List<Any>
) -> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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..." }
Expand Down
34 changes: 29 additions & 5 deletions app/src/main/res/layout/sync_add_new_kserver_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,46 @@
tools:ignore="HardcodedText" />

<com.google.android.material.radiobutton.MaterialRadioButton
android:id="@+id/server_kserver_dev_item"
android:id="@+id/server_kserver_local_item"
style="@style/Widget.Material3.CompoundButton.RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dev.kserver.octi.darken.eu (Testing)"
android:text="localhost (Local)"
tools:ignore="HardcodedText" />

<com.google.android.material.radiobutton.MaterialRadioButton
android:id="@+id/server_kserver_local_item"
android:id="@+id/server_kserver_custom_item"
style="@style/Widget.Material3.CompoundButton.RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="localhost (Local)"
android:text="@string/sync_custom_server_label"
tools:ignore="HardcodedText" />
</RadioGroup>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/custom_server_input_layout"
style="@style/Widget.Material3.TextInputEditText.FilledBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/create_new_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/server_group"
tools:visibility="visible">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/custom_server_input"
style="@style/Widget.Material3.TextInputEditText.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="protocol://server:port"
android:imeOptions="actionGo"
android:inputType="text"
tools:ignore="HardcodedText" />
</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.button.MaterialButton
android:id="@+id/create_new_account"
style="@style/Widget.Material3.Button"
Expand All @@ -82,7 +106,7 @@
android:text="@string/general_create_account_action"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/server_group" />
app:layout_constraintTop_toBottomOf="@id/custom_server_input_layout" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/create_new_account_hint"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
<string name="sync_setting_interval_desc">Background synchronization interval (in minutes).</string>
<string name="sync_setting_category_background_label">Background synchronization</string>

<string name="sync_custom_server_label">Custom server</string>

<string name="settings_category_ui_label">User interface</string>

<string name="permission_required_label">Permission required</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading