diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-07-30 01:13:56 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-11-11 13:20:09 -0600 |
commit | c4daf6ba593a57fb25e1f4705b303350bf8a3fa1 (patch) | |
tree | 880a995eca7b8f34e22b594c0fdda75531e13a0e /anastasis/src/main/java/net/taler/anastasis/ui/forms/EditPolicyForm.kt | |
parent | a31ca9fa74e07bc06af156c83592471ac3964121 (diff) | |
download | taler-android-c4daf6ba593a57fb25e1f4705b303350bf8a3fa1.tar.gz taler-android-c4daf6ba593a57fb25e1f4705b303350bf8a3fa1.tar.bz2 taler-android-c4daf6ba593a57fb25e1f4705b303350bf8a3fa1.zip |
Policy editing + too many things to list
Signed-off-by: Iván Ávalos <avalos@disroot.org>
Diffstat (limited to 'anastasis/src/main/java/net/taler/anastasis/ui/forms/EditPolicyForm.kt')
-rw-r--r-- | anastasis/src/main/java/net/taler/anastasis/ui/forms/EditPolicyForm.kt | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/anastasis/src/main/java/net/taler/anastasis/ui/forms/EditPolicyForm.kt b/anastasis/src/main/java/net/taler/anastasis/ui/forms/EditPolicyForm.kt new file mode 100644 index 0000000..4cc3661 --- /dev/null +++ b/anastasis/src/main/java/net/taler/anastasis/ui/forms/EditPolicyForm.kt @@ -0,0 +1,117 @@ +/* + * This file is part of GNU Taler + * (C) 2023 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.anastasis.ui.forms + +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.material3.Checkbox +import androidx.compose.material3.Icon +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import net.taler.anastasis.models.AuthMethod +import net.taler.anastasis.models.AuthenticationProviderStatus +import net.taler.anastasis.models.Policy +import net.taler.anastasis.ui.reusable.components.DropdownTextField +import net.taler.anastasis.ui.theme.LocalSpacing + +@Composable +fun EditPolicyForm( + modifier: Modifier = Modifier, + policy: Policy?, + methods: List<AuthMethod>, + providers: Map<String, AuthenticationProviderStatus.Ok>, + onPolicyEdited: (policy: Policy) -> Unit, +) { + val localPolicy = policy ?: Policy(methods = listOf()) + val localMethods = localPolicy.methods.associateBy { it.authenticationMethod } + val submitLocalMethods = { it: Map<Int, Policy.PolicyMethod> -> + onPolicyEdited( + localPolicy.copy( + methods = it.flatMap { entry -> + listOf(entry.value) + } + ) + ) + } + + LazyColumn( + modifier = modifier, + ) { + items(count = methods.size) { index -> + val method = methods[index] + // Get only the providers that support this method type + val methodProviders = providers.filterValues { provider -> + method.type in provider.methods.map { it.type } + }.keys.toList() + val selectedProvider = localMethods[index]?.provider + val checked = selectedProvider != null + Row( + Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + ) { + Checkbox( + enabled = checked, + checked = checked, + onCheckedChange = { + if (it) selectedProvider?.let { prov -> + submitLocalMethods( + localMethods.toMutableMap().apply { + this[index] = Policy.PolicyMethod( + authenticationMethod = index, + provider = prov, + ) + } + ) + } else { + submitLocalMethods( + localMethods.toMutableMap().apply { + remove(index) + } + ) + } + }, + ) + DropdownTextField( + modifier = Modifier.padding(bottom = LocalSpacing.current.small), + label = method.instructions, + leadingIcon = { + Icon( + method.type.icon, + contentDescription = stringResource(method.type.nameRes), + ) + }, + selectedIndex = selectedProvider?.let{ methodProviders.indexOf(it) }, + options = methodProviders, + onOptionSelected = { + submitLocalMethods( + localMethods.toMutableMap().apply { + this[index] = Policy.PolicyMethod( + authenticationMethod = index, + provider = methodProviders[it], + ) + } + ) + }, + ) + } + } + } +}
\ No newline at end of file |