Merge pull request #520 from greenart7c3/select_all

Fixes for relay selection dialog
This commit is contained in:
Vitor Pamplona 2023-07-24 08:20:19 -04:00 committed by GitHub
commit eedce96453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 38 deletions

View File

@ -24,9 +24,11 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties import androidx.compose.ui.window.DialogProperties
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.RelayInformation import com.vitorpamplona.amethyst.model.RelayInformation
import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
@ -37,7 +39,6 @@ data class RelayList(
val isSelected: Boolean val isSelected: Boolean
) )
@OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun RelaySelectionDialog( fun RelaySelectionDialog(
list: List<Relay>, list: List<Relay>,
@ -79,6 +80,10 @@ fun RelaySelectionDialog(
) )
} }
var selected by remember {
mutableStateOf(true)
}
Dialog( Dialog(
onDismissRequest = { onClose() }, onDismissRequest = { onClose() },
properties = DialogProperties( properties = DialogProperties(
@ -126,6 +131,17 @@ fun RelaySelectionDialog(
) )
} }
RelaySwitch(
text = stringResource(R.string.select_deselect_all),
checked = selected,
onClick = {
selected = !selected
relays = relays.mapIndexed { _, item ->
item.copy(isSelected = selected)
}
}
)
LazyColumn( LazyColumn(
contentPadding = PaddingValues( contentPadding = PaddingValues(
top = 10.dp, top = 10.dp,
@ -136,48 +152,55 @@ fun RelaySelectionDialog(
relays, relays,
key = { _, item -> item.relay.url } key = { _, item -> item.relay.url }
) { index, item -> ) { index, item ->
Row( RelaySwitch(
horizontalArrangement = Arrangement.SpaceBetween, text = item.relay.url
verticalAlignment = Alignment.CenterVertically, .removePrefix("ws://")
modifier = Modifier .removePrefix("wss://")
.fillMaxWidth() .removeSuffix("/"),
.combinedClickable( checked = item.isSelected,
onClick = { onClick = {
relays = relays.mapIndexed { j, item -> relays = relays.mapIndexed { j, item ->
if (index == j) { if (index == j) {
item.copy(isSelected = !item.isSelected) item.copy(isSelected = !item.isSelected)
} else { } else {
item item
}
}
},
onLongClick = {
loadRelayInfo(item.relay.url, context, scope) {
relayInfo = it
}
}
)
) {
Text(
item.relay.url
.removePrefix("ws://")
.removePrefix("wss://")
.removeSuffix("/")
)
Switch(
checked = item.isSelected,
onCheckedChange = {
relays = relays.mapIndexed { j, item ->
if (index == j) {
item.copy(isSelected = !item.isSelected)
} else { item }
} }
} }
) },
} onLongPress = {
loadRelayInfo(item.relay.url, context, scope) {
relayInfo = it
}
}
)
} }
} }
} }
} }
} }
} }
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RelaySwitch(text: String, checked: Boolean, onClick: () -> Unit, onLongPress: () -> Unit = { }) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.combinedClickable(
onClick = onClick,
onLongClick = onLongPress
)
) {
Text(
modifier = Modifier.weight(1f),
text = text
)
Switch(
checked = checked,
onCheckedChange = {
onClick()
}
)
}
}

View File

@ -506,4 +506,5 @@
<string name="nip05_verified">Nostr address was verified</string> <string name="nip05_verified">Nostr address was verified</string>
<string name="nip05_failed">Nostr address failed verification</string> <string name="nip05_failed">Nostr address failed verification</string>
<string name="nip05_checking">Checking Nostr address</string> <string name="nip05_checking">Checking Nostr address</string>
<string name="select_deselect_all">Select/Deselect all</string>
</resources> </resources>