Android: whisper to channels, clients and whisper lists
The channel and client sheets whisper to them or add them to a whisper list. The top bar's whisper menu picks a list, replies to the last whisper, stops whispering and edits the lists; a banner over the voice bar says who the microphone whispers to. Core keeps the whisper on the session, finding its channels by path and its clients by unique id again as the server changes, and the saved lists in whisperlists.properties. ts3j now sends whisper packets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,11 +3,14 @@ package com.ts3client.android
|
||||
import android.net.Uri
|
||||
import com.ts3client.config.Bookmark
|
||||
import com.ts3client.contacts.ContactType
|
||||
import com.ts3client.config.WhisperList
|
||||
import com.ts3client.net.ChannelNode
|
||||
import com.ts3client.net.ClientEntry
|
||||
import com.ts3client.net.ChannelSettings
|
||||
import com.ts3client.net.ConnectionStats
|
||||
import com.ts3client.net.Details
|
||||
import com.ts3client.net.GroupChoice
|
||||
import com.ts3client.net.Whisper
|
||||
import com.ts3client.net.ban.BanEntry
|
||||
import com.ts3client.net.ban.BanRule
|
||||
import com.ts3client.net.filetransfer.RemoteFile
|
||||
@@ -185,6 +188,30 @@ class ServerController internal constructor(
|
||||
conn.model.getClient(clientId)?.let { conn.setClientVolumeModifier(it, decibels) }
|
||||
}
|
||||
|
||||
// ---- whisper ----
|
||||
|
||||
fun setWhisper(whisper: Whisper?) = session.setWhisper(whisper)
|
||||
|
||||
fun whisperToChannel(channelId: Int) = conn.model.getChannel(channelId)?.let { setWhisper(Whisper.toChannel(it)) }
|
||||
|
||||
fun whisperToClient(clientId: Int) = conn.model.getClient(clientId)?.let { setWhisper(Whisper.toClient(it)) }
|
||||
|
||||
/** The client who whispered to us last, while on the server. */
|
||||
fun lastWhisperer(): ClientEntry? = session.lastWhisperer()
|
||||
|
||||
/** A channel as a whisper list holds it: by path, so the list works on any server with that channel. */
|
||||
fun channelRef(channelId: Int): String? = conn.model.getChannel(channelId)?.let { conn.model.channelPath(it.id) }
|
||||
|
||||
fun clientRef(clientId: Int): WhisperList.Client? =
|
||||
conn.model.getClient(clientId)?.let { WhisperList.Client(it.uniqueId, it.nickname) }
|
||||
|
||||
/** The clients on the server, for adding to a whisper list; ourselves left out. */
|
||||
fun clientRefs(): List<WhisperList.Client> = if (!session.isConnected) emptyList()
|
||||
else conn.model.allClients().filter { it.id != conn.selfClientId && !it.uniqueId.isNullOrEmpty() }
|
||||
.sortedBy { it.nickname.lowercase() }.map { WhisperList.Client(it.uniqueId, it.nickname) }
|
||||
|
||||
fun channelPaths(): List<String> = session.channelPaths()
|
||||
|
||||
// ---- channel administration ----
|
||||
|
||||
val canCreateChannels: Boolean get() = session.canCreateChannels()
|
||||
@@ -439,6 +466,8 @@ class ServerController internal constructor(
|
||||
inputMode = app.settings.inputMode,
|
||||
hostButton = if (session.isConnected) conn.model.hostButton() else null,
|
||||
self = session.selfState(),
|
||||
whisper = session.whisper()?.label,
|
||||
whisperNobody = session.isWhisperingToNobody,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ import com.ts3client.config.Bookmark
|
||||
import com.ts3client.config.Bookmarks
|
||||
import com.ts3client.config.IdentityStore
|
||||
import com.ts3client.config.Settings
|
||||
import com.ts3client.config.WhisperList
|
||||
import com.ts3client.config.WhisperLists
|
||||
import com.ts3client.contacts.Contact
|
||||
import com.ts3client.contacts.ContactStore
|
||||
import com.ts3client.myts.ChatAttachment
|
||||
@@ -64,6 +66,10 @@ data class SessionState(
|
||||
val hostButton: HostButton? = null,
|
||||
/** What the tree shows for the local client, and the connection notification too. */
|
||||
val self: SelfState = SelfState.DISCONNECTED,
|
||||
/** Who the microphone whispers to instead of the channel; null when it talks in the channel. */
|
||||
val whisper: String? = null,
|
||||
/** Whether none of the whisper's channels and clients are on the server, so nobody hears it. */
|
||||
val whisperNobody: Boolean = false,
|
||||
)
|
||||
|
||||
/** A file to share in chat, open for reading. */
|
||||
@@ -361,6 +367,37 @@ class SessionController(private val context: Context) {
|
||||
shortcuts.pin(bookmark)
|
||||
}
|
||||
|
||||
// ---- whisper lists ----
|
||||
|
||||
private val whisperListStore = WhisperLists.load()
|
||||
private val _whisperLists = MutableStateFlow(whisperListCopies())
|
||||
val whisperLists: StateFlow<List<WhisperList>> = _whisperLists
|
||||
|
||||
private fun whisperListCopies() = whisperListStore.all().map { it.copy() }
|
||||
|
||||
/** Puts [list] in place of the one at [index], or adds it when [index] is -1. */
|
||||
fun saveWhisperList(index: Int, list: WhisperList) {
|
||||
val all = whisperListStore.all()
|
||||
if (index in all.indices) all[index] = list.copy() else all.add(list.copy())
|
||||
whisperListStore.save()
|
||||
_whisperLists.value = whisperListCopies()
|
||||
}
|
||||
|
||||
/** Adds a channel or a client to the list at [index], or to a new list named [newName] when [index] is -1. */
|
||||
fun addToWhisperList(index: Int, newName: String, add: (WhisperList) -> Unit) {
|
||||
val list = whisperListStore.all().getOrNull(index)?.copy() ?: WhisperList(newName)
|
||||
add(list)
|
||||
saveWhisperList(index, list)
|
||||
}
|
||||
|
||||
fun removeWhisperList(index: Int) {
|
||||
val all = whisperListStore.all()
|
||||
if (index !in all.indices) return
|
||||
all.removeAt(index)
|
||||
whisperListStore.save()
|
||||
_whisperLists.value = whisperListCopies()
|
||||
}
|
||||
|
||||
// ---- contacts ----
|
||||
|
||||
private fun contactCopies(): List<Contact> = contactStore.all().map { it.copy() }
|
||||
|
||||
@@ -79,6 +79,7 @@ import com.ts3client.android.SessionState
|
||||
import com.ts3client.android.TreeFind
|
||||
import com.ts3client.android.findIn
|
||||
import com.ts3client.config.Settings
|
||||
import com.ts3client.config.WhisperList
|
||||
import com.ts3client.myts.ChatAttachment
|
||||
import com.ts3client.session.ServerLink
|
||||
import com.ts3client.text.TsLink
|
||||
@@ -156,6 +157,9 @@ fun ServerScreen(
|
||||
/** A locked channel whose files are wanted, while its password is asked for. */
|
||||
var filesLocked by remember { mutableStateOf<Int?>(null) }
|
||||
var deletingChannel by remember { mutableStateOf<Int?>(null) }
|
||||
var whisperLists by rememberSaveable { mutableStateOf(false) }
|
||||
/** What is being added to a whisper list, while the list is picked: its name and how to add it. */
|
||||
var toWhisperList by remember { mutableStateOf<Pair<String, (WhisperList) -> Unit>?>(null) }
|
||||
var connectionInfo by rememberSaveable { mutableStateOf<Int?>(null) }
|
||||
/** The client to move, while a channel for them is being picked. */
|
||||
var moving by remember { mutableStateOf<Int?>(null) }
|
||||
@@ -246,6 +250,10 @@ fun ServerScreen(
|
||||
FilesScreen(server, it.channelId, it.name, it.password, onBack = { files = null })
|
||||
return
|
||||
}
|
||||
if (whisperLists) {
|
||||
WhisperListsScreen(vm, server, onBack = { whisperLists = false })
|
||||
return
|
||||
}
|
||||
connectionInfo?.let { id ->
|
||||
ConnectionInfoScreen(server, id, tree.client(id), onBack = { connectionInfo = null })
|
||||
return
|
||||
@@ -276,6 +284,7 @@ fun ServerScreen(
|
||||
expandedHeight = 48.dp,
|
||||
actions = {
|
||||
state.hostButton?.let { HostButtonAction(it) { url -> runCatching { uriHandler.openUri(url) } } }
|
||||
if (state.connected) WhisperMenu(vm, server, state) { whisperLists = true }
|
||||
IconButton(onClick = find::show) { PackIcon("FIND", "Find") }
|
||||
IconButton(onClick = onSettings) { PackIcon("SETTINGS", "Settings") }
|
||||
},
|
||||
@@ -283,9 +292,12 @@ fun ServerScreen(
|
||||
}
|
||||
},
|
||||
bottomBar = {
|
||||
val ptt = rememberPttTarget(vm)
|
||||
VoiceBar(state, ptt.owner != null, ptt.talking, vm, server, onEnableMicrophone, showChat, chats.anyUnread) {
|
||||
showChat = !showChat
|
||||
Column {
|
||||
val ptt = rememberPttTarget(vm)
|
||||
WhisperBanner(state) { server.setWhisper(null) }
|
||||
VoiceBar(state, ptt.owner != null, ptt.talking, vm, server, onEnableMicrophone, showChat, chats.anyUnread) {
|
||||
showChat = !showChat
|
||||
}
|
||||
}
|
||||
},
|
||||
snackbarHost = { SnackbarHost(snackbar) },
|
||||
@@ -386,6 +398,11 @@ fun ServerScreen(
|
||||
onAddContact = { server.addContact(id, it) },
|
||||
onEditContact = { clientSheet = null; onEditContact(client.uniqueId) },
|
||||
onConnectionInfo = { clientSheet = null; connectionInfo = id },
|
||||
onWhisper = { clientSheet = null; server.whisperToClient(id) },
|
||||
onAddToWhisperList = {
|
||||
clientSheet = null
|
||||
server.clientRef(id)?.let { ref -> toWhisperList = client.name to { it.clients.add(ref) } }
|
||||
},
|
||||
onMove = if (server.canMoveClients) ({ clientSheet = null; moving = id }) else null,
|
||||
onMoveToOwnChannel = if (server.canMoveClients && tree.channel(state.channelId)?.clients?.none { it.id == id } == true) {
|
||||
{ server.moveClient(id, state.channelId); clientSheet = null }
|
||||
@@ -412,6 +429,11 @@ fun ServerScreen(
|
||||
onJoin = { join(channel); channelSheet = null },
|
||||
onSubscribe = { server.setSubscribed(id, it); channelSheet = null },
|
||||
onBookmark = { channelSheet = null; bookmarkHere(id) },
|
||||
onWhisper = { channelSheet = null; server.whisperToChannel(id) },
|
||||
onAddToWhisperList = {
|
||||
channelSheet = null
|
||||
server.channelRef(id)?.let { ref -> toWhisperList = channel.name to { it.channels.add(ref) } }
|
||||
},
|
||||
onFiles = {
|
||||
channelSheet = null
|
||||
if (channel.locked) filesLocked = id else files = FilesOf(id, channel.name, "")
|
||||
@@ -460,6 +482,12 @@ fun ServerScreen(
|
||||
dismissButton = { TextButton(onClick = { deletingChannel = null }) { Text("Cancel") } },
|
||||
)
|
||||
}
|
||||
toWhisperList?.let { (name, add) ->
|
||||
AddToWhisperListDialog(vm, name, onDismiss = { toWhisperList = null }) { index, newName ->
|
||||
toWhisperList = null
|
||||
vm.addToWhisperList(index, newName, add)
|
||||
}
|
||||
}
|
||||
if (serverSheet) {
|
||||
remember(tree, infoUpdates) { server.serverDetails() }?.let {
|
||||
ServerSheet(it, onLink, onDismiss = { serverSheet = false },
|
||||
|
||||
@@ -54,6 +54,8 @@ fun ClientSheet(
|
||||
onAddContact: (ContactType) -> Unit,
|
||||
onEditContact: () -> Unit,
|
||||
onConnectionInfo: () -> Unit,
|
||||
onWhisper: () -> Unit,
|
||||
onAddToWhisperList: () -> Unit,
|
||||
/** Null when the client cannot be moved. */
|
||||
onMove: (() -> Unit)?,
|
||||
/** Null when the client cannot be moved, or is in our channel already. */
|
||||
@@ -95,6 +97,8 @@ fun ClientSheet(
|
||||
} else {
|
||||
Action("PLAYER_CHAT", "Send message", onClick = onMessage)
|
||||
Action("POKE", "Poke") { poking = true }
|
||||
Action("PLAYER_WHISPER", "Whisper", onClick = onWhisper)
|
||||
Action("ON_WHISPERLIST", "Add to whisper list…", onClick = onAddToWhisperList)
|
||||
onMove?.let { Action("CHANNEL_SWITCH", "Move to channel…", onClick = it) }
|
||||
onMoveToOwnChannel?.let { Action("MOVE_CLIENT_TO_OWN_CHANNEL", "Move to own channel", onClick = it) }
|
||||
moderation()
|
||||
@@ -129,7 +133,7 @@ fun ClientSheet(
|
||||
}
|
||||
}
|
||||
|
||||
/** What can be done with a channel: join it, whether to hear who is in it, its files, and editing it. */
|
||||
/** What can be done with a channel: join it, whether to hear who is in it, whisper to it, its files, and editing it. */
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ChannelSheet(
|
||||
@@ -141,6 +145,8 @@ fun ChannelSheet(
|
||||
onJoin: () -> Unit,
|
||||
onSubscribe: (Boolean) -> Unit,
|
||||
onBookmark: () -> Unit,
|
||||
onWhisper: () -> Unit,
|
||||
onAddToWhisperList: () -> Unit,
|
||||
onFiles: () -> Unit,
|
||||
/** Each null when we may not. */
|
||||
onCreateSub: (() -> Unit)?,
|
||||
@@ -160,6 +166,8 @@ fun ChannelSheet(
|
||||
} else {
|
||||
Action("SUBSCRIBE_TO_CHANNEL", "Subscribe") { onSubscribe(true) }
|
||||
}
|
||||
Action("WHISPER", "Whisper", onClick = onWhisper)
|
||||
Action("ON_WHISPERLIST", "Add to whisper list…", onClick = onAddToWhisperList)
|
||||
Action("FILETRANSFER", "Browse files", onClick = onFiles)
|
||||
Action("BOOKMARK_ADD", "Add to bookmarks", onClick = onBookmark)
|
||||
onCreateSub?.let { Action("CHANNEL_CREATE_SUB", "Create sub-channel", onClick = it) }
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
package com.ts3client.android.ui
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.MoreVert
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.ListItem
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
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.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.ts3client.android.ServerController
|
||||
import com.ts3client.android.SessionController
|
||||
import com.ts3client.android.SessionState
|
||||
import com.ts3client.config.WhisperList
|
||||
import com.ts3client.net.Whisper
|
||||
|
||||
/** Says the microphone whispers instead of talking in the channel, until stopped. */
|
||||
@Composable
|
||||
fun WhisperBanner(state: SessionState, onStop: () -> Unit) {
|
||||
val target = state.whisper ?: return
|
||||
Surface(color = MaterialTheme.colorScheme.tertiaryContainer) {
|
||||
Row(Modifier.fillMaxWidth().padding(start = 16.dp, end = 8.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
PackIcon("WHISPER", null)
|
||||
Column(Modifier.weight(1f).padding(horizontal = 12.dp)) {
|
||||
Text("Whispering to $target", maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
style = MaterialTheme.typography.bodyMedium)
|
||||
if (state.whisperNobody) {
|
||||
Text("Nobody there", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error)
|
||||
}
|
||||
}
|
||||
TextButton(onClick = onStop) { Text("Stop") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The top bar's whisper button: a list to whisper to, a reply to the last whisper, the lists' editor. */
|
||||
@Composable
|
||||
fun WhisperMenu(vm: SessionController, server: ServerController, state: SessionState, onManage: () -> Unit) {
|
||||
val lists by vm.whisperLists.collectAsStateWithLifecycle()
|
||||
var open by remember { mutableStateOf(false) }
|
||||
Box {
|
||||
IconButton(onClick = { open = true }) { PackIcon("WHISPER", "Whisper") }
|
||||
DropdownMenu(open, { open = false }) {
|
||||
val replyTo = remember(open) { server.lastWhisperer() }
|
||||
replyTo?.let { client ->
|
||||
DropdownMenuItem({ Text("Reply to ${client.nickname}") }, leadingIcon = { PackIcon("PLAYER_WHISPER", null) },
|
||||
onClick = { open = false; server.whisperToClient(client.id) })
|
||||
}
|
||||
for (list in lists) {
|
||||
DropdownMenuItem({ Text(list.name.ifBlank { "Unnamed" }) },
|
||||
leadingIcon = { PackIcon("ON_WHISPERLIST", null) },
|
||||
enabled = !list.isEmpty,
|
||||
onClick = { open = false; server.setWhisper(Whisper.of(list)) })
|
||||
}
|
||||
if (state.whisper != null) {
|
||||
DropdownMenuItem({ Text("Stop whispering") }, leadingIcon = { PackIcon("WHISPER", null) },
|
||||
onClick = { open = false; server.setWhisper(null) })
|
||||
}
|
||||
if (replyTo != null || lists.isNotEmpty() || state.whisper != null) HorizontalDivider()
|
||||
DropdownMenuItem({ Text("Whisper lists…") }, leadingIcon = { PackIcon("WHISPERLISTS", null) },
|
||||
onClick = { open = false; onManage() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Picks the whisper list to add something to, or names a new one. */
|
||||
@Composable
|
||||
fun AddToWhisperListDialog(vm: SessionController, what: String, onDismiss: () -> Unit, onPick: (index: Int, newName: String) -> Unit) {
|
||||
val lists by vm.whisperLists.collectAsStateWithLifecycle()
|
||||
var naming by remember { mutableStateOf(false) }
|
||||
if (naming) {
|
||||
TextPrompt("New whisper list", "Name", "Add", onDismiss = onDismiss, onConfirm = {
|
||||
if (it.isNotBlank()) onPick(-1, it.trim()) else onDismiss()
|
||||
})
|
||||
return
|
||||
}
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text("Add $what to") },
|
||||
text = {
|
||||
Column(Modifier.verticalScroll(rememberScrollState())) {
|
||||
lists.forEachIndexed { index, list ->
|
||||
ListItem(headlineContent = { Text(list.name.ifBlank { "Unnamed" }) },
|
||||
modifier = Modifier.clickable { onPick(index, "") })
|
||||
}
|
||||
ListItem(headlineContent = { Text("New list…") }, modifier = Modifier.clickable { naming = true })
|
||||
}
|
||||
},
|
||||
confirmButton = {},
|
||||
dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } },
|
||||
)
|
||||
}
|
||||
|
||||
/** The saved whisper lists, each opening onto its channels and clients. */
|
||||
@Composable
|
||||
fun WhisperListsScreen(vm: SessionController, server: ServerController, onBack: () -> Unit) {
|
||||
val lists by vm.whisperLists.collectAsStateWithLifecycle()
|
||||
/** The list being edited: its index, -1 for a new one; null on the overview. */
|
||||
var editing by rememberSaveable { mutableStateOf<Int?>(null) }
|
||||
editing?.let { index ->
|
||||
WhisperListScreen(vm, server, index, lists.getOrNull(index) ?: WhisperList(), onBack = { editing = null })
|
||||
return
|
||||
}
|
||||
SubScreen("Whisper lists", onBack = onBack, actions = {
|
||||
TextButton(onClick = { editing = -1 }) { Text("New") }
|
||||
}) { padding ->
|
||||
if (lists.isEmpty()) {
|
||||
Box(Modifier.padding(padding).fillMaxWidth().padding(24.dp)) {
|
||||
Text("No whisper lists", color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
return@SubScreen
|
||||
}
|
||||
LazyColumn(Modifier.padding(padding)) {
|
||||
itemsIndexed(lists) { index, list ->
|
||||
var menu by remember { mutableStateOf(false) }
|
||||
ListItem(
|
||||
headlineContent = { Text(list.name.ifBlank { "Unnamed" }) },
|
||||
supportingContent = { Text(summary(list)) },
|
||||
leadingContent = { PackIcon("ON_WHISPERLIST", null) },
|
||||
trailingContent = {
|
||||
Box {
|
||||
IconButton(onClick = { menu = true }) { Icon(Icons.Default.MoreVert, "List options") }
|
||||
DropdownMenu(menu, { menu = false }) {
|
||||
DropdownMenuItem({ Text("Whisper") }, enabled = !list.isEmpty, onClick = {
|
||||
menu = false
|
||||
server.setWhisper(Whisper.of(list))
|
||||
onBack()
|
||||
})
|
||||
DropdownMenuItem({ Text("Remove") }, onClick = { menu = false; vm.removeWhisperList(index) })
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier.clickable { editing = index },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun summary(list: WhisperList): String {
|
||||
val parts = listOfNotNull(
|
||||
list.channels.size.takeIf { it > 0 }?.let { if (it == 1) "1 channel" else "$it channels" },
|
||||
list.clients.size.takeIf { it > 0 }?.let { if (it == 1) "1 client" else "$it clients" },
|
||||
)
|
||||
return parts.joinToString(", ").ifEmpty { "Empty" }
|
||||
}
|
||||
|
||||
/** One whisper list: its name, channels and clients, saved on Save. */
|
||||
@Composable
|
||||
private fun WhisperListScreen(vm: SessionController, server: ServerController, index: Int, saved: WhisperList, onBack: () -> Unit) {
|
||||
var name by rememberSaveable { mutableStateOf(saved.name) }
|
||||
var list by remember { mutableStateOf(saved.copy()) }
|
||||
val change = { f: WhisperList.() -> Unit -> list = list.copy().apply(f) }
|
||||
var addingChannel by remember { mutableStateOf(false) }
|
||||
var addingClient by remember { mutableStateOf(false) }
|
||||
|
||||
SubScreen(if (index < 0) "New whisper list" else "Edit whisper list", onBack = onBack, actions = {
|
||||
TextButton(onClick = {
|
||||
vm.saveWhisperList(index, list.copy().apply { this.name = name.trim() })
|
||||
onBack()
|
||||
}, enabled = name.isNotBlank()) { Text("Save") }
|
||||
}) { padding ->
|
||||
LazyColumn(Modifier.padding(padding)) {
|
||||
item {
|
||||
OutlinedTextField(name, { name = it }, Modifier.fillMaxWidth().padding(16.dp), label = { Text("Name") },
|
||||
singleLine = true)
|
||||
}
|
||||
item { Section("Channels") }
|
||||
items(list.channels) { channel ->
|
||||
RemovableRow("CHANNEL_GREEN", channel) { change { channels.remove(channel) } }
|
||||
}
|
||||
item { LinkRow("Add channel…", null, "CHANNEL_CREATE") { addingChannel = true } }
|
||||
item { Section("Clients") }
|
||||
items(list.clients) { client ->
|
||||
RemovableRow("PLAYER_WHISPER", client.nickname.ifBlank { client.uniqueId }) { change { clients.remove(client) } }
|
||||
}
|
||||
item { LinkRow("Add client…", null, "ADD_FRIEND") { addingClient = true } }
|
||||
}
|
||||
}
|
||||
|
||||
if (addingChannel) {
|
||||
val paths = remember { server.channelPaths().filter { it !in list.channels } }
|
||||
ChoiceDialog("Add channel", paths, { it }, onDismiss = { addingChannel = false }) {
|
||||
addingChannel = false
|
||||
change { channels.add(it) }
|
||||
}
|
||||
}
|
||||
if (addingClient) {
|
||||
val online = remember { server.clientRefs().filter { c -> list.clients.none { it.uniqueId == c.uniqueId } } }
|
||||
ChoiceDialog("Add client", online, { it.nickname }, onDismiss = { addingClient = false }) {
|
||||
addingClient = false
|
||||
change { clients.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RemovableRow(iconKey: String, label: String, onRemove: () -> Unit) {
|
||||
ListItem(
|
||||
headlineContent = { Text(label, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
leadingContent = { PackIcon(iconKey, null) },
|
||||
trailingContent = { IconButton(onClick = onRemove) { Icon(Icons.Default.Close, "Remove") } },
|
||||
)
|
||||
}
|
||||
|
||||
/** Picks one of [choices], of the server on screen; says so when it has none. */
|
||||
@Composable
|
||||
private fun <T> ChoiceDialog(title: String, choices: List<T>, label: (T) -> String, onDismiss: () -> Unit, onPick: (T) -> Unit) {
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text(title) },
|
||||
text = {
|
||||
if (choices.isEmpty()) Text("Nothing to add on this server")
|
||||
else LazyColumn(Modifier.heightIn(max = 400.dp)) {
|
||||
items(choices) { choice ->
|
||||
ListItem(headlineContent = { Text(label(choice), maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
modifier = Modifier.clickable { onPick(choice) })
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {},
|
||||
dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } },
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user