refactor: add option for custom `ByteString` key generation

pull/1274/head
andrekir 2024-09-27 19:48:29 -03:00
rodzic 3c0fd03555
commit 3c22272d23
5 zmienionych plików z 39 dodań i 33 usunięć

Wyświetl plik

@ -35,6 +35,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.twotone.Check
import androidx.compose.material.icons.twotone.Close
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.icons.twotone.ContentCopy
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
@ -50,7 +51,6 @@ import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalClipboardManager
@ -410,10 +410,10 @@ fun ChannelScreen(
}
}) {
Icon(
painter = when {
isError -> rememberVectorPainter(Icons.TwoTone.Close)
!isUrlEqual -> rememberVectorPainter(Icons.TwoTone.Check)
else -> painterResource(R.drawable.ic_twotone_content_copy_24)
imageVector = when {
isError -> Icons.TwoTone.Close
!isUrlEqual -> Icons.TwoTone.Check
else -> Icons.TwoTone.ContentCopy
},
contentDescription = when {
isError -> "Error"

Wyświetl plik

@ -41,6 +41,7 @@ fun EditBase64Preference(
keyboardActions: KeyboardActions,
onValueChange: (ByteString) -> Unit,
modifier: Modifier = Modifier,
onGenerateKey: (() -> Unit)? = null,
) {
fun ByteString.encodeToString() = Base64.encodeToString(this.toByteArray(), Base64.NO_WRAP)
fun String.toByteString() = Base64.decode(this, Base64.NO_WRAP).toByteString()
@ -56,6 +57,12 @@ fun EditBase64Preference(
}
}
val (icon, description) = when {
isError -> Icons.TwoTone.Close to stringResource(R.string.error)
onGenerateKey != null && !isFocused -> Icons.TwoTone.Refresh to stringResource(R.string.reset)
else -> null to null
}
OutlinedTextField(
value = valueState,
onValueChange = {
@ -75,14 +82,18 @@ fun EditBase64Preference(
trailingIcon = {
IconButton(
onClick = {
val psk = if (isError) value else Channel.getRandomKey()
valueState = psk.encodeToString()
onValueChange(psk)
}
if (isError) {
valueState = value.encodeToString()
onValueChange(value)
} else if (onGenerateKey != null && !isFocused) {
onGenerateKey()
}
},
enabled = enabled,
) {
Icon(
imageVector = if (isError) Icons.TwoTone.Close else Icons.TwoTone.Refresh,
contentDescription = stringResource(if (isError) R.string.error else R.string.reset),
imageVector = icon ?: return@IconButton,
contentDescription = description,
tint = if (isError) MaterialTheme.colors.error
else LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
)
@ -100,6 +111,7 @@ private fun EditBase64PreferencePreview() {
enabled = true,
keyboardActions = KeyboardActions {},
onValueChange = {},
onGenerateKey = {},
modifier = Modifier.padding(16.dp)
)
}

Wyświetl plik

@ -67,7 +67,7 @@ fun EditChannelDialog(
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = {
channelInput = channelInput.copy {
name = it
name = it.trim()
if (psk == Channel.default.settings.psk) psk = Channel.getRandomKey()
}
},
@ -85,6 +85,9 @@ fun EditChannelDialog(
channelInput = channelInput.copy { psk = it }
}
},
onGenerateKey = {
channelInput = channelInput.copy { psk = Channel.getRandomKey() }
},
)
SwitchPreference(
@ -137,7 +140,7 @@ fun EditChannelDialog(
.fillMaxWidth()
.weight(1f),
onClick = {
onAddClick(channelInput.copy { name = channelInput.name.trim() })
onAddClick(channelInput)
},
enabled = true,
) { Text(stringResource(R.string.save)) }

Wyświetl plik

@ -42,7 +42,9 @@ fun SecurityConfigItemList(
enabled = enabled,
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChange = {
securityInput = securityInput.copy { publicKey = it }
if (it.size() == 32) {
securityInput = securityInput.copy { publicKey = it }
}
},
)
}
@ -54,7 +56,9 @@ fun SecurityConfigItemList(
enabled = enabled,
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChange = {
securityInput = securityInput.copy { privateKey = it }
if (it.size() == 32) {
securityInput = securityInput.copy { privateKey = it }
}
},
)
}
@ -66,9 +70,11 @@ fun SecurityConfigItemList(
enabled = enabled,
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChange = {
securityInput = securityInput.copy {
adminKey.clear()
adminKey.add(it)
if (it.size() == 32) {
securityInput = securityInput.copy {
adminKey.clear()
adminKey.add(it)
}
}
},
)

Wyświetl plik

@ -1,15 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillAlpha="0.3"
android:fillColor="@android:color/white"
android:pathData="M8,7h11v14H8z"
android:strokeAlpha="0.3" />
<path
android:fillColor="@android:color/white"
android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z" />
</vector>