Android: create, edit and delete channels
A one-page channel editor with what the desktop dialog has but the permissions and the icon: name, password, topic, description, type, sorting, talk power, codec preset or codec and quality, and the limits. Create channel is on the server sheet; create sub-channel, edit and delete on the channel sheet, each only with the permission for it. The codec list, presets and bandwidth move to core for both editors. Core gains channel delete, and the desktop channel menu a Delete Channel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import android.net.Uri
|
||||
import com.ts3client.config.Bookmark
|
||||
import com.ts3client.contacts.ContactType
|
||||
import com.ts3client.net.ChannelNode
|
||||
import com.ts3client.net.ChannelSettings
|
||||
import com.ts3client.net.ConnectionStats
|
||||
import com.ts3client.net.Details
|
||||
import com.ts3client.net.GroupChoice
|
||||
@@ -183,6 +184,36 @@ class ServerController internal constructor(
|
||||
conn.model.getClient(clientId)?.let { conn.setClientVolumeModifier(it, decibels) }
|
||||
}
|
||||
|
||||
// ---- channel administration ----
|
||||
|
||||
val canCreateChannels: Boolean get() = session.canCreateChannels()
|
||||
val canEditChannels: Boolean get() = session.canEditChannels()
|
||||
val canDeleteChannels: Boolean get() = session.canDeleteChannels()
|
||||
|
||||
/** The channels a channel can sort after: its siblings, or [parentId]'s children for a new one. */
|
||||
fun sortChoices(channelId: Int, parentId: Int): List<ChannelNode> =
|
||||
if (channelId > 0) conn.model.siblingsOf(channelId) else conn.model.childrenOf(parentId)
|
||||
|
||||
/** A channel's editable properties as the server has them. */
|
||||
suspend fun channelSettings(channelId: Int): Result<ChannelSettings> = suspendCoroutine { done ->
|
||||
conn.requestChannelSettings(channelId) { settings, error ->
|
||||
done.resume(if (error == null) Result.success(settings) else Result.failure(IllegalStateException(error)))
|
||||
}
|
||||
}
|
||||
|
||||
/** Each of these answers null once done, else why not. */
|
||||
suspend fun editChannel(channelId: Int, changes: Map<String, String>): String? =
|
||||
if (changes.isEmpty()) null
|
||||
else suspendCoroutine { done -> conn.applyChannelEdit(channelId, changes, emptyMap(), emptyList()) { done.resume(it) } }
|
||||
|
||||
suspend fun createChannel(parentId: Int, settings: ChannelSettings): String? = suspendCoroutine { done ->
|
||||
conn.createChannel(parentId, settings.creationParameters(), emptyMap()) { _, error -> done.resume(error) }
|
||||
}
|
||||
|
||||
/** Deletes a channel, with its sub-channels and the clients in it moved out. */
|
||||
suspend fun deleteChannel(channelId: Int): String? =
|
||||
suspendCoroutine { done -> conn.deleteChannel(channelId, true) { done.resume(it) } }
|
||||
|
||||
// ---- moderation ----
|
||||
|
||||
val canKickFromChannel: Boolean get() = session.canKickFromChannel()
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
package com.ts3client.android.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.imePadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.ts3client.android.ServerController
|
||||
import com.ts3client.net.ChannelCodec
|
||||
import com.ts3client.net.ChannelSettings
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/** The channel being edited ([channelId] > 0), or one to create below [parentId] (0: the top level). */
|
||||
data class ChannelEdit(val channelId: Int, val parentId: Int = 0)
|
||||
|
||||
/**
|
||||
* TeamSpeak's channel editor on one page: name, password, topic and description, then the
|
||||
* channel's type, sorting, audio and limits. Only what changed is sent, as the server refuses
|
||||
* an edit carrying one property we may not change.
|
||||
*/
|
||||
@Composable
|
||||
fun ChannelEditScreen(server: ServerController, edit: ChannelEdit, onDone: () -> Unit) {
|
||||
val creating = edit.channelId <= 0
|
||||
val snackbar = remember { SnackbarHostState() }
|
||||
val scope = rememberCoroutineScope()
|
||||
/** The channel as the server described it, which changes are measured against. */
|
||||
var original by remember { mutableStateOf<ChannelSettings?>(null) }
|
||||
var edited by remember { mutableStateOf<ChannelSettings?>(null) }
|
||||
var passwordText by remember { mutableStateOf("") }
|
||||
var saving by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(edit) {
|
||||
if (creating) {
|
||||
// Made from a channel's sheet, a channel is usually a throwaway one, as in TeamSpeak;
|
||||
// the server puts a new channel after its siblings.
|
||||
val fresh = ChannelSettings().apply {
|
||||
type = ChannelSettings.Type.TEMPORARY
|
||||
parentId = edit.parentId
|
||||
order = server.sortChoices(0, edit.parentId).lastOrNull()?.id ?: 0
|
||||
}
|
||||
original = fresh
|
||||
edited = fresh.copy()
|
||||
} else {
|
||||
server.channelSettings(edit.channelId)
|
||||
.onSuccess { original = it; edited = it.copy() }
|
||||
.onFailure { snackbar.showSnackbar("Could not read the channel: ${it.message}"); onDone() }
|
||||
}
|
||||
}
|
||||
|
||||
val change = { f: ChannelSettings.() -> Unit -> edited = edited?.copy()?.apply(f) }
|
||||
val save = save@{
|
||||
val e = edited ?: return@save
|
||||
val o = original ?: return@save
|
||||
if (e.name.isBlank()) {
|
||||
scope.launch { snackbar.showSnackbar("The channel needs a name.") }
|
||||
return@save
|
||||
}
|
||||
saving = true
|
||||
scope.launch {
|
||||
val trimmed = e.copy().apply { name = name.trim() }
|
||||
val error = if (creating) server.createChannel(edit.parentId, trimmed)
|
||||
else server.editChannel(edit.channelId, trimmed.changesFrom(o))
|
||||
saving = false
|
||||
if (error == null) onDone()
|
||||
else snackbar.showSnackbar((if (creating) "Could not create the channel: " else "Could not save the channel: ") + error)
|
||||
}
|
||||
}
|
||||
|
||||
val title = when {
|
||||
!creating -> "Edit channel"
|
||||
edit.parentId > 0 -> "New sub-channel"
|
||||
else -> "New channel"
|
||||
}
|
||||
SubScreen(title, onBack = onDone, snackbar = snackbar, actions = {
|
||||
TextButton(onClick = save, enabled = edited != null && !saving) { Text(if (creating) "Create" else "Save") }
|
||||
}) { padding ->
|
||||
val e = edited
|
||||
if (e == null) {
|
||||
Box(Modifier.padding(padding).fillMaxSize(), contentAlignment = Alignment.Center) { CircularProgressIndicator() }
|
||||
return@SubScreen
|
||||
}
|
||||
Column(Modifier.padding(padding).imePadding().verticalScroll(rememberScrollState()).padding(bottom = 16.dp)) {
|
||||
Column(Modifier.padding(horizontal = 16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
OutlinedTextField(e.name, { change { name = it } }, Modifier.fillMaxWidth(), label = { Text("Name") },
|
||||
singleLine = true)
|
||||
OutlinedTextField(passwordText, {
|
||||
passwordText = it
|
||||
change { password = it }
|
||||
}, Modifier.fillMaxWidth(), label = { Text("Password") }, singleLine = true,
|
||||
placeholder = if (e.hasPassword && e.password == null) ({ Text("Unchanged") }) else null,
|
||||
visualTransformation = PasswordVisualTransformation(),
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password))
|
||||
if (e.hasPassword && e.password == null) {
|
||||
TextButton(onClick = { change { password = "" } }) { Text("Remove password") }
|
||||
}
|
||||
OutlinedTextField(e.topic, { change { topic = it } }, Modifier.fillMaxWidth(), label = { Text("Topic") },
|
||||
singleLine = true)
|
||||
OutlinedTextField(e.description, { change { description = it } }, Modifier.fillMaxWidth(),
|
||||
label = { Text("Description") }, minLines = 3, maxLines = 10)
|
||||
}
|
||||
|
||||
Section("Channel type")
|
||||
Segments(listOf(
|
||||
ChannelSettings.Type.TEMPORARY to "Temporary",
|
||||
ChannelSettings.Type.SEMI_PERMANENT to "Semi-permanent",
|
||||
ChannelSettings.Type.PERMANENT to "Permanent",
|
||||
), e.type) { change { type = it } }
|
||||
// The default channel cannot stop being one; another has to take over.
|
||||
if (original?.defaultChannel != true) {
|
||||
Toggle("Default channel", e.defaultChannel) { change { defaultChannel = it } }
|
||||
}
|
||||
val siblings = remember(edit) {
|
||||
server.sortChoices(edit.channelId, edit.parentId).filter { it.id != edit.channelId }
|
||||
}
|
||||
Picker("Sort after", listOf(0 to "(first)") + siblings.map { it.id to it.name },
|
||||
e.order.takeIf { o -> o == 0 || siblings.any { it.id == o } } ?: 0) { change { order = it } }
|
||||
NumberField("Needed talk power", e.neededTalkPower, 0..9999) { change { neededTalkPower = it } }
|
||||
|
||||
Section("Audio")
|
||||
val preset = ChannelCodec.Preset.of(e.codec, e.codecQuality)
|
||||
var custom by remember { mutableStateOf(preset == null) }
|
||||
val presets: List<Pair<ChannelCodec.Preset?, String>> =
|
||||
ChannelCodec.Preset.entries.map { it to it.label } + (null to "Custom")
|
||||
Segments(presets, if (custom) null else preset) { picked ->
|
||||
custom = picked == null
|
||||
if (picked != null) change { codec = picked.codec; codecQuality = picked.quality }
|
||||
}
|
||||
if (custom) {
|
||||
Picker("Codec", ChannelCodec.NAMES.indices.map { it to ChannelCodec.NAMES[it] },
|
||||
ChannelCodec.clampCodec(e.codec)) { change { codec = it } }
|
||||
Level("Quality", e.codecQuality.toFloat(), 0f..ChannelCodec.MAX_QUALITY.toFloat(),
|
||||
{ it.roundToInt().toString() }, steps = ChannelCodec.MAX_QUALITY - 1) {
|
||||
change { codecQuality = it.roundToInt() }
|
||||
}
|
||||
}
|
||||
Hint("Bandwidth: ${ChannelCodec.bandwidthText(e.codec, e.codecQuality)} per talking client")
|
||||
|
||||
Section("Advanced")
|
||||
Column(Modifier.padding(horizontal = 16.dp)) {
|
||||
OutlinedTextField(e.phoneticName, { change { phoneticName = it } }, Modifier.fillMaxWidth(),
|
||||
label = { Text("Phonetic name") }, singleLine = true)
|
||||
}
|
||||
if (e.type == ChannelSettings.Type.TEMPORARY) {
|
||||
NumberField("Delete delay (seconds)", e.deleteDelay, 0..ChannelSettings.MAX_DELETE_DELAY) {
|
||||
change { deleteDelay = it }
|
||||
}
|
||||
}
|
||||
Toggle("Voice encrypted", e.encrypted) { change { encrypted = it } }
|
||||
Text("Max users", Modifier.padding(start = 16.dp, top = 8.dp), style = MaterialTheme.typography.bodyLarge)
|
||||
Segments(listOf(true to "Unlimited", false to "Limited"), e.maxClientsUnlimited) {
|
||||
change { maxClientsUnlimited = it }
|
||||
}
|
||||
if (!e.maxClientsUnlimited) NumberField("Users", e.maxClients, 0..65535) { change { maxClients = it } }
|
||||
Text("Family max users", Modifier.padding(start = 16.dp, top = 8.dp), style = MaterialTheme.typography.bodyLarge)
|
||||
val family = when {
|
||||
e.familyInherited -> FamilyLimit.INHERITED
|
||||
e.familyUnlimited -> FamilyLimit.UNLIMITED
|
||||
else -> FamilyLimit.LIMITED
|
||||
}
|
||||
Segments(FamilyLimit.entries.map { it to it.label }, family) {
|
||||
change {
|
||||
familyInherited = it == FamilyLimit.INHERITED
|
||||
familyUnlimited = it == FamilyLimit.UNLIMITED
|
||||
}
|
||||
}
|
||||
if (family == FamilyLimit.LIMITED) {
|
||||
NumberField("Users", e.maxFamilyClients, 0..65535) { change { maxFamilyClients = it } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private enum class FamilyLimit(val label: String) { INHERITED("Inherited"), UNLIMITED("Unlimited"), LIMITED("Limited") }
|
||||
|
||||
/** A whole number within [range]; what does not parse leaves the value alone. */
|
||||
@Composable
|
||||
private fun NumberField(label: String, value: Int, range: IntRange, onChange: (Int) -> Unit) {
|
||||
var text by remember(value) { mutableStateOf(value.toString()) }
|
||||
Row(Modifier.padding(horizontal = 16.dp, vertical = 4.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
OutlinedTextField(text, {
|
||||
text = it.filter(Char::isDigit).take(7)
|
||||
text.toIntOrNull()?.let { n -> onChange(n.coerceIn(range)) }
|
||||
}, Modifier.fillMaxWidth(), label = { Text(label) }, singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ArrowDropDown
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Badge
|
||||
import androidx.compose.material3.BadgedBox
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
@@ -48,6 +49,7 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.key
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.Saver
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
@@ -80,6 +82,7 @@ import com.ts3client.config.Settings
|
||||
import com.ts3client.myts.ChatAttachment
|
||||
import com.ts3client.session.ServerLink
|
||||
import com.ts3client.text.TsLink
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private val ChatKeySaver = Saver<ChatKey, String>(
|
||||
save = { key ->
|
||||
@@ -98,6 +101,11 @@ private val ChatKeySaver = Saver<ChatKey, String>(
|
||||
},
|
||||
)
|
||||
|
||||
private val ChannelEditSaver = Saver<ChannelEdit?, IntArray>(
|
||||
save = { edit -> edit?.let { intArrayOf(it.channelId, it.parentId) } },
|
||||
restore = { ChannelEdit(it[0], it[1]) },
|
||||
)
|
||||
|
||||
/** A connection: the channel tree and the chat, with the voice controls underneath. */
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -135,6 +143,8 @@ fun ServerScreen(
|
||||
var serverSheet by remember { mutableStateOf(false) }
|
||||
val bookmarkHere = { channelId: Int -> server.bookmark(channelId)?.let { onEditBookmark(BookmarkEdit(-1, it)) } }
|
||||
var banList by rememberSaveable { mutableStateOf(false) }
|
||||
var channelEdit by rememberSaveable(stateSaver = ChannelEditSaver) { mutableStateOf<ChannelEdit?>(null) }
|
||||
var deletingChannel by remember { mutableStateOf<Int?>(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) }
|
||||
@@ -217,6 +227,10 @@ fun ServerScreen(
|
||||
BanListScreen(server, vm.banReasons, onBack = { banList = false })
|
||||
return
|
||||
}
|
||||
channelEdit?.let {
|
||||
ChannelEditScreen(server, it, onDone = { channelEdit = null })
|
||||
return
|
||||
}
|
||||
connectionInfo?.let { id ->
|
||||
ConnectionInfoScreen(server, id, tree.client(id), onBack = { connectionInfo = null })
|
||||
return
|
||||
@@ -383,6 +397,9 @@ fun ServerScreen(
|
||||
onJoin = { join(channel); channelSheet = null },
|
||||
onSubscribe = { server.setSubscribed(id, it); channelSheet = null },
|
||||
onBookmark = { channelSheet = null; bookmarkHere(id) },
|
||||
onCreateSub = if (server.canCreateChannels) ({ channelSheet = null; channelEdit = ChannelEdit(0, id) }) else null,
|
||||
onEdit = if (server.canEditChannels) ({ channelSheet = null; channelEdit = ChannelEdit(id) }) else null,
|
||||
onDelete = if (server.canDeleteChannels) ({ channelSheet = null; deletingChannel = id }) else null,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -396,11 +413,30 @@ fun ServerScreen(
|
||||
onConfirm = { joinLocked = null; server.joinChannel(id, it) },
|
||||
)
|
||||
}
|
||||
deletingChannel?.let { id ->
|
||||
val channel = tree.channel(id)
|
||||
val scope = rememberCoroutineScope()
|
||||
AlertDialog(
|
||||
onDismissRequest = { deletingChannel = null },
|
||||
title = { Text("Delete ${channel?.name.orEmpty()}?") },
|
||||
text = if (channel != null && (channel.children.isNotEmpty() || channel.clients.isNotEmpty())) {
|
||||
{ Text("Its sub-channels go with it, and whoever is in it is moved out.") }
|
||||
} else null,
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
deletingChannel = null
|
||||
scope.launch { server.deleteChannel(id)?.let { snackbar.showSnackbar("Could not delete the channel: $it") } }
|
||||
}) { Text("Delete") }
|
||||
},
|
||||
dismissButton = { TextButton(onClick = { deletingChannel = null }) { Text("Cancel") } },
|
||||
)
|
||||
}
|
||||
if (serverSheet) {
|
||||
remember(tree, infoUpdates) { server.serverDetails() }?.let {
|
||||
ServerSheet(it, onLink, onDismiss = { serverSheet = false },
|
||||
onBookmark = { serverSheet = false; bookmarkHere(state.channelId) },
|
||||
onBanList = if (server.canViewBanList) ({ serverSheet = false; banList = true }) else null)
|
||||
onBanList = if (server.canViewBanList) ({ serverSheet = false; banList = true }) else null,
|
||||
onCreateChannel = if (server.canCreateChannels) ({ serverSheet = false; channelEdit = ChannelEdit(0, 0) }) else null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +141,10 @@ fun ChannelSheet(
|
||||
onJoin: () -> Unit,
|
||||
onSubscribe: (Boolean) -> Unit,
|
||||
onBookmark: () -> Unit,
|
||||
/** Each null when we may not. */
|
||||
onCreateSub: (() -> Unit)?,
|
||||
onEdit: (() -> Unit)?,
|
||||
onDelete: (() -> Unit)?,
|
||||
) {
|
||||
ModalBottomSheet(onDismissRequest = onDismiss) {
|
||||
Column(Modifier.verticalScroll(rememberScrollState()).navigationBarsPadding().padding(bottom = 16.dp)) {
|
||||
@@ -156,6 +160,9 @@ fun ChannelSheet(
|
||||
Action("SUBSCRIBE_TO_CHANNEL", "Subscribe") { onSubscribe(true) }
|
||||
}
|
||||
Action("BOOKMARK_ADD", "Add to bookmarks", onClick = onBookmark)
|
||||
onCreateSub?.let { Action("CHANNEL_CREATE_SUB", "Create sub-channel", onClick = it) }
|
||||
onEdit?.let { Action("CHANNEL_EDIT", "Edit channel", onClick = it) }
|
||||
onDelete?.let { Action("CHANNEL_DELETE", "Delete channel", onClick = it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,6 +176,7 @@ fun ServerSheet(
|
||||
onDismiss: () -> Unit,
|
||||
onBookmark: () -> Unit,
|
||||
onBanList: (() -> Unit)?,
|
||||
onCreateChannel: (() -> Unit)?,
|
||||
) {
|
||||
ModalBottomSheet(onDismissRequest = onDismiss) {
|
||||
Column(Modifier.verticalScroll(rememberScrollState()).navigationBarsPadding().padding(bottom = 16.dp)) {
|
||||
@@ -179,6 +187,7 @@ fun ServerSheet(
|
||||
}
|
||||
DetailsSection(details, onLink)
|
||||
Action("BOOKMARK_ADD", "Add to bookmarks", onClick = onBookmark)
|
||||
onCreateChannel?.let { Action("CHANNEL_CREATE", "Create channel", onClick = it) }
|
||||
onBanList?.let { Action("BAN_LIST", "Ban list", onClick = it) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user