From 788fe49aa40015d5556525310974c9a896088bae Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 16 Nov 2022 16:45:48 +0600 Subject: [PATCH] almost completed adding of new creds type UI --- .../core/models/MavenConfig.kt | 5 ++ .../core/ui/RepositoriesView.kt | 57 ++++++++++++++++++- .../core/ui/RepositoryCredentialTypeDrawer.kt | 32 +++++++++++ .../core/ui/RepositoryCredentialTypeDrawer.kt | 40 +++++++++++++ .../core/ui/utils/ActualUIElements.kt | 3 +- 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 core/src/jsMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt create mode 100644 core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt diff --git a/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/models/MavenConfig.kt b/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/models/MavenConfig.kt index 1991cd8..fe274fc 100644 --- a/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/models/MavenConfig.kt +++ b/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/models/MavenConfig.kt @@ -53,6 +53,11 @@ data class MavenPublishingRepository( val usernameProperty: String, val passwordProperty: String ): CredentialsType { + constructor(baseParameter: String) : this( + "${baseParameter.uppercase()}_USER", + "${baseParameter.uppercase()}_PASSWORD", + ) + override fun buildCheckPart(): String = "(project.hasProperty('${usernameProperty}') || System.getenv('${usernameProperty}') != null) && (project.hasProperty('${passwordProperty}') || System.getenv('${passwordProperty}') != null)" override fun buildCredsPart(): String { return """ diff --git a/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoriesView.kt b/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoriesView.kt index f65447d..e53db8b 100644 --- a/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoriesView.kt +++ b/core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoriesView.kt @@ -3,24 +3,32 @@ package dev.inmo.kmppscriptbuilder.core.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import dev.inmo.kmppscriptbuilder.core.models.MavenPublishingRepository +import dev.inmo.kmppscriptbuilder.core.ui.utils.ButtonsPanel import dev.inmo.kmppscriptbuilder.core.ui.utils.CommonText import dev.inmo.kmppscriptbuilder.core.ui.utils.CommonTextField import dev.inmo.kmppscriptbuilder.core.ui.utils.DefaultSmallVerticalMargin +import dev.inmo.kmppscriptbuilder.core.ui.utils.Drawer class RepositoryState( name: String = "", - url: String = "" + url: String = "", + credsType: MavenPublishingRepository.CredentialsType = MavenPublishingRepository.CredentialsType.UsernameAndPassword(name) ) { var name: String by mutableStateOf(name) var url: String by mutableStateOf(url) + var credsType by mutableStateOf(credsType) fun toRepository() = MavenPublishingRepository(name, url) } private fun MavenPublishingRepository.toRepositoryState() = RepositoryState(name, url) +expect class RepositoryCredentialTypeDrawer : Drawer +expect fun RepositoryCredentialTypeDrawerWithState(repositoryState: RepositoryState): RepositoryCredentialTypeDrawer + class RepositoriesView : ListView("Repositories info") { var repositories: List get() = itemsList.map { it.toRepository() } @@ -38,6 +46,10 @@ class RepositoriesView : ListView("Repositories info") { @Composable override fun buildView(item: RepositoryState) { + val credsTypesDrawer = remember { + RepositoryCredentialTypeDrawerWithState(item) + } + CommonText("Repository name") CommonTextField( item.name, @@ -49,6 +61,49 @@ class RepositoriesView : ListView("Repositories info") { item.url, "For example: https://repo.maven.apache.org/maven2/" ) { item.url = it } + + ButtonsPanel( + "Credentials type", + MavenPublishingRepository.CredentialsType.Nothing.takeIf { item.credsType != it } ?: item.credsType, + MavenPublishingRepository.CredentialsType.UsernameAndPassword(item.name).takeIf { item.credsType !is MavenPublishingRepository.CredentialsType.UsernameAndPassword } ?: item.credsType, + MavenPublishingRepository.CredentialsType.HttpHeaderCredentials("Authorization", "${item.name.uppercase()}_TOKEN").takeIf { item.credsType !is MavenPublishingRepository.CredentialsType.HttpHeaderCredentials } ?: item.credsType, + ) { + with(credsTypesDrawer) { + with(item.credsType) { + draw() + } + } + } + + when (val credsType = item.credsType) { + is MavenPublishingRepository.CredentialsType.HttpHeaderCredentials -> { + CommonText("Header name") + CommonTextField(credsType.headerName) { + item.credsType = credsType.copy(headerName = it) + } + DefaultSmallVerticalMargin() + + CommonText("Property name") + CommonTextField(credsType.headerValueProperty) { + item.credsType = credsType.copy(headerValueProperty = it) + } + } + MavenPublishingRepository.CredentialsType.Nothing -> { + CommonText("No parameters for absence of credentials") + } + is MavenPublishingRepository.CredentialsType.UsernameAndPassword -> { + CommonText("Username property name") + CommonTextField(credsType.usernameProperty) { + item.credsType = credsType.copy(usernameProperty = it) + } + DefaultSmallVerticalMargin() + + CommonText("Password property name") + CommonTextField(credsType.passwordProperty) { + item.credsType = credsType.copy(passwordProperty = it) + } + } + } } } diff --git a/core/src/jsMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt b/core/src/jsMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt new file mode 100644 index 0000000..50f34d9 --- /dev/null +++ b/core/src/jsMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt @@ -0,0 +1,32 @@ +package dev.inmo.kmppscriptbuilder.core.ui + +import androidx.compose.runtime.Composable +import dev.inmo.jsuikit.elements.DefaultButton +import dev.inmo.jsuikit.modifiers.UIKitButton +import dev.inmo.jsuikit.modifiers.UIKitMargin +import dev.inmo.jsuikit.modifiers.UIKitUtility +import dev.inmo.kmppscriptbuilder.core.models.MavenPublishingRepository +import dev.inmo.kmppscriptbuilder.core.ui.utils.Drawer +import dev.inmo.kmppscriptbuilder.core.ui.utils.NoTransform + +actual class RepositoryCredentialTypeDrawer( + private val state: RepositoryState +) : Drawer { + @Composable + override fun MavenPublishingRepository.CredentialsType.draw() { + val name = when (this@draw) { + is MavenPublishingRepository.CredentialsType.HttpHeaderCredentials -> "Headers" + MavenPublishingRepository.CredentialsType.Nothing -> "No" + is MavenPublishingRepository.CredentialsType.UsernameAndPassword -> "Username and password" + } + if (state.credsType == this) { + DefaultButton(name, UIKitButton.Type.Primary, UIKitButton.Size.Small, UIKitMargin.Small.Horizontal, UIKitUtility.NoTransform, UIKitUtility.Border.Rounded) + } else { + DefaultButton(name, UIKitButton.Type.Default, UIKitButton.Size.Small, UIKitMargin.Small.Horizontal, UIKitUtility.NoTransform, UIKitUtility.Border.Rounded) { + state.credsType = this + } + } + } +} + +actual fun RepositoryCredentialTypeDrawerWithState(repositoryState: RepositoryState): RepositoryCredentialTypeDrawer = RepositoryCredentialTypeDrawer(repositoryState) diff --git a/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt b/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt new file mode 100644 index 0000000..c8d4186 --- /dev/null +++ b/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/RepositoryCredentialTypeDrawer.kt @@ -0,0 +1,40 @@ +package dev.inmo.kmppscriptbuilder.core.ui + +import androidx.compose.foundation.layout.padding +import androidx.compose.material.Button +import androidx.compose.material.OutlinedButton +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import dev.inmo.kmppscriptbuilder.core.models.MavenPublishingRepository +import dev.inmo.kmppscriptbuilder.core.ui.utils.Drawer + +actual class RepositoryCredentialTypeDrawer( + private val state: RepositoryState +) : Drawer { + @Composable + override fun MavenPublishingRepository.CredentialsType.draw() { + val name = when (this@draw) { + is MavenPublishingRepository.CredentialsType.HttpHeaderCredentials -> "Headers" + MavenPublishingRepository.CredentialsType.Nothing -> "No" + is MavenPublishingRepository.CredentialsType.UsernameAndPassword -> "Username and password" + } + if (state.credsType == this) { + Button({}, Modifier.padding(8.dp, 0.dp)) { + Text(name) + } + } else { + OutlinedButton( + { + state.credsType = this + }, + Modifier.padding(8.dp, 0.dp) + ) { + Text(name) + } + } + } +} + +actual fun RepositoryCredentialTypeDrawerWithState(repositoryState: RepositoryState): RepositoryCredentialTypeDrawer = RepositoryCredentialTypeDrawer(repositoryState) diff --git a/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/utils/ActualUIElements.kt b/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/utils/ActualUIElements.kt index daf68fc..7e5d66d 100644 --- a/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/utils/ActualUIElements.kt +++ b/core/src/jvmMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/utils/ActualUIElements.kt @@ -87,7 +87,8 @@ actual fun TitleText(text: String) { actual fun ButtonsPanel( title: String, data: Iterable, - itemDrawer: @Composable (T) -> Unit) { + itemDrawer: @Composable (T) -> Unit +) { Row { Text(title, Modifier.padding(8.dp)) data.forEach { itemDrawer(it) }