KotlinPublicationScriptsBui.../core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/LicensesView.kt

75 lines
2.4 KiB
Kotlin
Raw Normal View History

2022-11-15 12:53:59 +00:00
package dev.inmo.kmppscriptbuilder.core.ui
2022-11-15 13:55:54 +00:00
import androidx.compose.runtime.Composable
2022-11-15 18:56:24 +00:00
import androidx.compose.runtime.derivedStateOf
2022-11-15 13:55:54 +00:00
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
2022-11-15 18:56:24 +00:00
import androidx.compose.runtime.remember
2022-11-15 13:55:54 +00:00
import androidx.compose.runtime.setValue
2022-11-15 12:53:59 +00:00
import dev.inmo.kmppscriptbuilder.core.models.License
import dev.inmo.kmppscriptbuilder.core.models.getLicenses
2022-11-15 18:56:24 +00:00
import dev.inmo.kmppscriptbuilder.core.ui.utils.CommonTextField
2022-11-15 13:55:54 +00:00
import dev.inmo.kmppscriptbuilder.core.ui.utils.Drawer
2022-11-15 12:53:59 +00:00
import io.ktor.client.HttpClient
2022-11-15 13:55:54 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
2022-11-15 12:53:59 +00:00
2022-11-15 13:55:54 +00:00
internal class LicenseState(
2022-11-15 12:53:59 +00:00
id: String = "",
title: String = "",
url: String? = null
) {
var id: String by mutableStateOf(id)
var title: String by mutableStateOf(title)
var url: String? by mutableStateOf(url)
fun toLicense() = License(id, title, url)
}
2022-11-15 13:55:54 +00:00
internal fun License.toLicenseState() = LicenseState(id, title, url)
expect object LicensesDrawer : Drawer<LicensesView>
2022-11-15 12:53:59 +00:00
class LicensesView: VerticalView("Licenses") {
2022-11-15 13:55:54 +00:00
internal var licensesListState = mutableStateListOf<LicenseState>()
2022-11-15 12:53:59 +00:00
var licenses: List<License>
get() = licensesListState.map { it.toLicense() }
set(value) {
licensesListState.clear()
licensesListState.addAll(value.map { it.toLicenseState() })
}
2022-11-15 13:55:54 +00:00
internal val availableLicensesState = mutableStateListOf<License>()
internal var licenseSearchFilter by mutableStateOf("")
2022-11-15 18:56:24 +00:00
internal val searchFieldFocused = mutableStateOf(false)
internal val licensesOffersToShow = derivedStateOf {
val query = licenseSearchFilter.lowercase()
availableLicensesState.filter {
it.title.lowercase().contains(query)
}
}
2022-11-15 12:53:59 +00:00
init {
CoroutineScope(Dispatchers.Default).launch {
val client = HttpClient()
availableLicensesState.addAll(client.getLicenses().values)
client.close()
}
}
2022-11-15 13:55:54 +00:00
override val content: @Composable () -> Unit = {
2022-11-15 18:56:24 +00:00
CommonTextField(
licenseSearchFilter,
"Search filter",
onFocusChanged = {
searchFieldFocused.value = it
}
) { filterText ->
licenseSearchFilter = filterText
}
2022-11-15 13:55:54 +00:00
with(LicensesDrawer) { draw() }
2022-11-15 12:53:59 +00:00
}
}