🚧 Adds dropdown list
This commit is contained in:
parent
c124754c96
commit
b4f1943467
@ -14,8 +14,10 @@ import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
@ -26,6 +28,7 @@ import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
@ -37,6 +40,8 @@ import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.core.content.edit
|
||||
import dev.svitan.antifed.ui.components.DropdownList
|
||||
import dev.svitan.antifed.ui.theme.AntiFedTheme
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
@ -52,6 +57,8 @@ class AuthActivity : ComponentActivity() {
|
||||
AntiFedTheme {
|
||||
var serverUrl by remember { mutableStateOf("") }
|
||||
var token by remember { mutableStateOf("") }
|
||||
var authIndex by remember { mutableIntStateOf(-1) }
|
||||
var authId by remember { mutableStateOf("") }
|
||||
|
||||
var needToCheckUrl by remember { mutableStateOf(false) }
|
||||
var checkingUrl by remember { mutableStateOf(false) }
|
||||
@ -71,6 +78,11 @@ class AuthActivity : ComponentActivity() {
|
||||
if (loadedServerUrl.isNotBlank()) {
|
||||
serverUrl = loadedServerUrl
|
||||
}
|
||||
val loadedToken = prefs.getString(getString(R.string.token_key), "") ?: ""
|
||||
if (loadedToken.isNotBlank()) {
|
||||
token = loadedToken
|
||||
}
|
||||
authId = prefs.getString(getString(R.string.auth_key), "") ?: ""
|
||||
isServerUrlOk = prefs.getBoolean(getString(R.string.server_url_okay_key), false)
|
||||
|
||||
fun serverUrlMatches(): Boolean {
|
||||
@ -85,9 +97,8 @@ class AuthActivity : ComponentActivity() {
|
||||
if (!needToCheckUrl) return@LaunchedEffect
|
||||
else {
|
||||
isServerUrlOk = false
|
||||
with(prefs.edit()) {
|
||||
prefs.edit {
|
||||
putBoolean(getString(R.string.server_url_okay_key), false)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,9 +112,8 @@ class AuthActivity : ComponentActivity() {
|
||||
checkingUrl = false
|
||||
needToCheckUrl = false
|
||||
isServerUrlOk = response.status == HttpStatusCode.OK
|
||||
with(prefs.edit()) {
|
||||
prefs.edit {
|
||||
putBoolean(getString(R.string.server_url_okay_key), isServerUrlOk)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,6 +136,10 @@ class AuthActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
auths = response.body<List<AuthDTO>>()
|
||||
if (authId.isNotBlank()) {
|
||||
authIndex = auths.indexOfFirst { it.id == authId }
|
||||
}
|
||||
|
||||
checkingToken = false
|
||||
needToCheckToken = false
|
||||
isTokenOk = true
|
||||
@ -167,9 +181,8 @@ class AuthActivity : ComponentActivity() {
|
||||
onValueChange = {
|
||||
needToCheckUrl = true
|
||||
serverUrl = it
|
||||
with(prefs.edit()) {
|
||||
prefs.edit {
|
||||
putString(getString(R.string.server_url_key), it)
|
||||
apply()
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.server_url)) },
|
||||
@ -191,16 +204,45 @@ class AuthActivity : ComponentActivity() {
|
||||
onValueChange = {
|
||||
needToCheckToken = true
|
||||
token = it
|
||||
prefs.edit {
|
||||
putString(getString(R.string.token_key), it)
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.token)) },
|
||||
singleLine = true,
|
||||
isError = token.isBlank(),
|
||||
isError = isTokenOk,
|
||||
visualTransformation = PasswordVisualTransformation(),
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
|
||||
trailingIcon = {
|
||||
if (checkingToken) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.width(36.dp),
|
||||
color = MaterialTheme.colorScheme.secondary,
|
||||
trackColor = MaterialTheme.colorScheme.surfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Text("auths: $auths")
|
||||
if (auths.isNotEmpty())
|
||||
DropdownList(
|
||||
itemList = auths.map { it -> it.name },
|
||||
selectedIndex = 0,
|
||||
modifier = Modifier,
|
||||
onItemClick = {
|
||||
authIndex = it
|
||||
prefs.edit {
|
||||
putInt(getString(R.string.auth_key), it)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
FloatingActionButton(
|
||||
onClick = { println("creating new auth!!") }
|
||||
) {
|
||||
Icon(Icons.Default.Add, stringResource(R.string.create_auth))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,92 @@
|
||||
package dev.svitan.antifed.ui.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.VerticalDivider
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Popup
|
||||
import androidx.compose.ui.window.PopupProperties
|
||||
|
||||
@Composable
|
||||
fun DropdownList(
|
||||
itemList: List<String>,
|
||||
selectedIndex: Int,
|
||||
modifier: Modifier,
|
||||
onItemClick: (Int) -> Unit
|
||||
) {
|
||||
var showDropdown by rememberSaveable { mutableStateOf(true) }
|
||||
val scrollState = rememberScrollState()
|
||||
|
||||
Column(
|
||||
modifier = Modifier,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Box(
|
||||
modifier = modifier
|
||||
.background(Color.Red)
|
||||
.clickable { showDropdown = !showDropdown },
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(text = itemList[selectedIndex], modifier = Modifier.padding(3.dp))
|
||||
}
|
||||
|
||||
Box {
|
||||
if (showDropdown) {
|
||||
Popup(
|
||||
alignment = Alignment.TopCenter,
|
||||
properties = PopupProperties(
|
||||
excludeFromSystemGesture = true,
|
||||
),
|
||||
onDismissRequest = { showDropdown = false }
|
||||
) {
|
||||
Column(
|
||||
modifier = modifier
|
||||
.heightIn(max = 90.dp)
|
||||
.verticalScroll(state = scrollState)
|
||||
.border(width = 1.dp, color = Color.Gray),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
itemList.onEachIndexed { index, item ->
|
||||
if (index != 0) {
|
||||
VerticalDivider(thickness = 1.dp, color = Color.LightGray)
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(Color.Green)
|
||||
.clickable {
|
||||
onItemClick(index)
|
||||
showDropdown = !showDropdown
|
||||
},
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(text = item)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -6,4 +6,5 @@
|
||||
<string name="auth_icon_desc">Ísť do nastavení autentikácie</string>
|
||||
<string name="go_back">Ísť späť</string>
|
||||
<string name="token">Token</string>
|
||||
<string name="create_auth">Vytvoriť novú auth</string>
|
||||
</resources>
|
@ -9,6 +9,9 @@
|
||||
<string name="settings_prefs_key" translatable="false">dev.svitan.antifed.settings</string>
|
||||
<string name="server_url_key" translatable="false">server_url</string>
|
||||
<string name="server_url_okay_key" translatable="false">server_url_okay</string>
|
||||
<string name="token_key" translatable="false">token</string>
|
||||
<string name="auth_key" translatable="false">auth_id</string>
|
||||
<string name="create_auth">Create new auth</string>
|
||||
<!-- Strings used for fragments for navigation -->
|
||||
|
||||
</resources>
|
Loading…
x
Reference in New Issue
Block a user