Settings values are Setting<T>: one key each, change listeners

Each setting is declared once with its key, type and range, and loaded
and saved from that list. Output device, volume and receive buffer, the
status bar and the master volume slider take effect through listeners
instead of calls wherever they change; Android does the same for output
settings and country flags. The Playback page previews voice volume with
the master volume applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 22:53:37 +00:00
co-authored by Claude Opus 5.5
parent bbe741d5cf
commit 203082d040
55 changed files with 581 additions and 615 deletions
@@ -43,7 +43,7 @@ class IconTheme(private val context: Context, private val settings: Settings) {
}
fun select(pack: IconPack) {
settings.iconPack = pack.id()
settings.iconPack.set(pack.id())
settings.save()
reload()
}
@@ -100,7 +100,7 @@ class IconTheme(private val context: Context, private val settings: Settings) {
private fun reload() {
val packs = IconPacks.findAll(folder, null)
val active = IconPacks.select(packs, settings.iconPack) ?: material
val active = IconPacks.select(packs, settings.iconPack.get()) ?: material
lookupOrder = IconPacks.lookupOrder(active, packs, material)
cache.evictAll()
_packs.value = packs
@@ -61,7 +61,7 @@ class IdentityManager(
}
private fun snapshot(): List<IdentityInfo> = store.all().map {
IdentityInfo(it.id, it.name, it.uniqueId, it.securityLevel, it.id == settings.defaultIdentityId,
IdentityInfo(it.id, it.name, it.uniqueId, it.securityLevel, it.id == settings.defaultIdentityId.get(),
it.nickname, it.phoneticNickname)
}
@@ -93,9 +93,9 @@ class IdentityManager(
/** Makes [entry], or else the first identity, the default while there is none. */
private fun adoptDefault(entry: IdentityEntry?) {
if (store.byId(settings.defaultIdentityId) != null) return
if (store.byId(settings.defaultIdentityId.get()) != null) return
val fallback = entry ?: store.all().firstOrNull() ?: return
settings.defaultIdentityId = fallback.id
settings.defaultIdentityId.set(fallback.id)
settings.save()
}
@@ -124,10 +124,10 @@ class IdentityManager(
/** The nickname of the identity a connect with [id] uses; empty when it has none. */
fun nicknameOf(id: String): String =
(store.byId(id) ?: store.byId(settings.defaultIdentityId))?.nickname.orEmpty()
(store.byId(id) ?: store.byId(settings.defaultIdentityId.get()))?.nickname.orEmpty()
fun setDefault(id: String) {
settings.defaultIdentityId = id
settings.defaultIdentityId.set(id)
settings.save()
refresh()
}
@@ -90,6 +90,8 @@ class LevelMonitor(private val settings: Settings, private val audio: AudioBacke
}
/** Push-to-talk would never open without the button, and the test is there to tune the gate. */
private fun testMode() =
if (settings.inputMode == Settings.InputMode.PUSH_TO_TALK) Settings.InputMode.VOICE_ACTIVATION else settings.inputMode
private fun testMode(): Settings.InputMode {
val mode = settings.inputMode.get()
return if (mode == Settings.InputMode.PUSH_TO_TALK) Settings.InputMode.VOICE_ACTIVATION else mode
}
}
@@ -467,7 +467,7 @@ class ServerController internal constructor(
away = session.isAway,
selfClientId = if (session.isConnected) session.selfClientId() else 0,
channelId = if (session.isConnected) self?.channelId ?: 0 else 0,
inputMode = app.settings.inputMode,
inputMode = app.settings.inputMode.get(),
hostButton = if (session.isConnected) session.model().hostButton() else null,
self = session.selfState(),
whisper = session.whisper()?.label,
@@ -489,8 +489,8 @@ class ServerController internal constructor(
delay(TREE_FRAME_MS)
treePending.set(false)
if (structureChanged.getAndSet(false)) roots = session.model().buildTree()
_tree.value = ServerTree.of(roots, session.model(), session.contacts().store(), app.settings.colorContacts,
app.settings.countryFlags, app.settings.clientRowColors)
_tree.value = ServerTree.of(roots, session.model(), session.contacts().store(),
app.settings.colorContacts.get(), app.settings.countryFlags.get(), app.settings.clientRowColors)
val serverName = if (session.isConnected) session.model().serverName.orEmpty() else ""
val channelName = session.currentChannel()?.name.orEmpty()
_chats.update {
@@ -508,7 +508,7 @@ class ServerController internal constructor(
override fun onSystemMessage(text: String) = system(text)
override fun onOwnChat(scope: ChatScope, peerClientId: Int, peerName: String, text: String) {
val self = session.model().getClient(session.selfClientId())?.nickname ?: app.settings.nickname
val self = session.model().getClient(session.selfClientId())?.nickname ?: app.settings.nickname.get()
val html = ChatHtml.message(session.selfClientId(), session.selfUniqueId(), "$self (you)", text, room)
append(key(scope, peerClientId), peerName, html)
if (scope == ChatScope.PRIVATE) app.notifier.sent(id, peerClientId, ChatHtml.plainMessage(text))
@@ -15,6 +15,7 @@ import eu.lixko.ts3j.client.config.BanReasonPresets
import eu.lixko.ts3j.client.config.Bookmark
import eu.lixko.ts3j.client.config.Bookmarks
import eu.lixko.ts3j.client.config.IdentityStore
import eu.lixko.ts3j.client.config.Setting
import eu.lixko.ts3j.client.config.Settings
import eu.lixko.ts3j.client.config.WhisperList
import eu.lixko.ts3j.client.config.WhisperLists
@@ -185,19 +186,21 @@ class SessionController(private val context: Context) {
_contacts.value = contactCopies()
_servers.value.forEach(ServerController::contactsChanged)
}
Setting.onAnyChange(::applyOutputSettings, settings.outputDevice, settings.outputVolume, settings.receiveBuffer)
settings.countryFlags.addListener { servers.value.forEach { it.scheduleTree(structure = false) } }
}
/** The nickname a connect with [identityId] goes by: the identity's own, else the global one. */
fun nicknameFor(identityId: String): String = identities.nicknameOf(identityId).ifBlank { settings.nickname }
val lastAddress: String get() = settings.lastAddress
fun nicknameFor(identityId: String): String = identities.nicknameOf(identityId).ifBlank { settings.nickname.get() }
val lastAddress: String get() = settings.lastAddress.get()
// ---- connection ----
/** Connects to `host[:port]`, as another server; an empty [identityId] means the default identity. */
fun connect(address: String, nickname: String, password: String = "", identityId: String = "") {
val (host, port) = parseAddress(address)
settings.lastAddress = address
if (identities.nicknameOf(identityId).isBlank()) settings.nickname = nickname
settings.lastAddress.set(address)
if (identities.nicknameOf(identityId).isBlank()) settings.nickname.set(nickname)
settings.save()
start(host, port, nickname, password, identityId, "", "")
}
@@ -532,9 +535,8 @@ class SessionController(private val context: Context) {
val soundPack: SoundPack? get() = sounds.pack()
fun setCountryFlags(shown: Boolean) {
settings.countryFlags = shown
settings.countryFlags.set(shown)
settings.save()
servers.value.forEach { it.scheduleTree(structure = false) }
}
/** Puts new client row colours, already set in [settings], into effect. */
@@ -544,7 +546,7 @@ class SessionController(private val context: Context) {
}
fun setDescriptionPictures(shown: Boolean) {
settings.descriptionPictures = shown
settings.descriptionPictures.set(shown)
settings.save()
}
@@ -598,21 +600,17 @@ class SessionController(private val context: Context) {
/** Switches between push to talk and voice activation; returns the new mode. */
fun toggleInputMode(): Settings.InputMode {
settings.inputMode = if (settings.inputMode == Settings.InputMode.PUSH_TO_TALK)
Settings.InputMode.VOICE_ACTIVATION else Settings.InputMode.PUSH_TO_TALK
settings.inputMode.set(if (settings.inputMode.get() == Settings.InputMode.PUSH_TO_TALK)
Settings.InputMode.VOICE_ACTIVATION else Settings.InputMode.PUSH_TO_TALK)
applySettings()
return settings.inputMode
return settings.inputMode.get()
}
/** Saves the settings and puts the voice and device ones into effect at once. */
/** Saves the settings and puts the voice ones into effect at once. */
fun applySettings() {
settings.save()
soundPlayer.setOutputDevice(settings.outputDevice)
try {
for (server in _servers.value) {
server.playback?.setMasterVolume(settings.outputVolume)
server.playback?.setOutputDevice(settings.outputDevice)
server.playback?.setReceiveBuffer(settings.receiveBuffer)
server.microphone?.apply(settings)
server.refresh()
server.scheduleTree(structure = false)
@@ -623,6 +621,16 @@ class SessionController(private val context: Context) {
}
}
/** Output device, volume and receive buffer are global, so they go to every connection. */
private fun applyOutputSettings() {
soundPlayer.setOutputDevice(settings.outputDevice.get())
for (server in _servers.value) {
server.playback?.setMasterVolume(settings.outputVolume.get())
server.playback?.setOutputDevice(settings.outputDevice.get())
server.playback?.setReceiveBuffer(settings.receiveBuffer.get())
}
}
/** Microphone preset and playback usage; the open lines move over at once. */
fun setRoute(route: AudioRoute) {
routePrefs.save(route)
@@ -39,7 +39,7 @@ class WebPictures(cacheDir: File, private val settings: Settings) {
fun retryFailed() = downloads.retryFailed()
/** Whether pictures are shown at all, rather than links to them. */
val enabled: Boolean get() = settings.descriptionPictures
val enabled: Boolean get() = settings.descriptionPictures.get()
suspend fun state(url: String): State {
decoded.get(url)?.let { return State.Shown(it) }
@@ -12,6 +12,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import eu.lixko.ts3j.client.android.SessionController
import eu.lixko.ts3j.client.config.Setting
import eu.lixko.ts3j.client.config.Settings
import kotlin.math.abs
import kotlin.math.roundToInt
@@ -23,56 +24,56 @@ private const val MAX_BITRATE_KBITS = 160f
@Composable
fun NetworkSettingsScreen(vm: SessionController, onBack: () -> Unit) {
val settings = vm.settings
fun change(edit: Settings.() -> Unit) {
settings.edit()
fun <T> change(setting: Setting<T>, value: T) {
setting.set(value)
vm.applySettings()
}
var vbr by remember { mutableStateOf(settings.vbr) }
var fec by remember { mutableStateOf(settings.fec) }
var music by remember { mutableStateOf(settings.music) }
var redundancy by remember { mutableStateOf(settings.redundancyDelay) }
var copies by remember { mutableStateOf(settings.redundancyCopies) }
var receiveBuffer by remember { mutableStateOf(settings.receiveBuffer) }
var vbr by remember { mutableStateOf(settings.vbr.get()) }
var fec by remember { mutableStateOf(settings.fec.get()) }
var music by remember { mutableStateOf(settings.music.get()) }
var redundancy by remember { mutableStateOf(settings.redundancyDelay.get()) }
var copies by remember { mutableStateOf(settings.redundancyCopies.get()) }
var receiveBuffer by remember { mutableStateOf(settings.receiveBuffer.get()) }
SubScreen("Network", onBack) { padding ->
Column(Modifier.padding(padding).verticalScroll(rememberScrollState()).padding(bottom = 8.dp)) {
Section("Encoder")
Level("Bitrate", settings.bitrate / 1000f, MIN_BITRATE_KBITS..MAX_BITRATE_KBITS, { "${it.roundToInt()} kbit/s" }) {
change { bitrate = it.roundToInt() * 1000 }
Level("Bitrate", settings.bitrate.get() / 1000f, MIN_BITRATE_KBITS..MAX_BITRATE_KBITS, { "${it.roundToInt()} kbit/s" }) {
change(settings.bitrate, it.roundToInt() * 1000)
}
Toggle("Variable bitrate (VBR)", vbr) {
vbr = it
change { this.vbr = it }
change(settings.vbr, it)
}
Toggle("Forward error correction (FEC)", fec) {
fec = it
change { this.fec = it }
change(settings.fec, it)
}
Level("Complexity", settings.complexity.toFloat(), 0f..10f, { "${it.roundToInt()}" }, steps = 9) {
change { complexity = it.roundToInt() }
Level("Complexity", settings.complexity.get().toFloat(), 0f..10f, { "${it.roundToInt()}" }, steps = 9) {
change(settings.complexity, it.roundToInt())
}
Toggle("Music codec (stereo, higher fidelity)", music) {
music = it
change { this.music = it }
change(settings.music, it)
}
Section("Redundancy")
ChoiceLevel("Delay", Settings.REDUNDANCY_DELAYS, redundancy) {
redundancy = it
change { redundancyDelay = it }
change(settings.redundancyDelay, it)
}
if (redundancy > 0) {
Level("Copies", copies.toFloat(), 1f..Settings.MAX_REDUNDANCY_COPIES.toFloat(), { "${it.roundToInt()}" },
steps = Settings.MAX_REDUNDANCY_COPIES - 2) {
copies = it.roundToInt()
change { redundancyCopies = copies }
change(settings.redundancyCopies, copies)
}
}
Section("Receive")
ChoiceLevel("Extra buffering", Settings.RECEIVE_BUFFERS, receiveBuffer) {
receiveBuffer = it
change { this.receiveBuffer = it }
change(settings.receiveBuffer, it)
}
}
}
@@ -72,13 +72,13 @@ private fun DesignScreen(vm: SessionController, onBack: () -> Unit) {
SubScreen("Design", onBack) { padding ->
Column(Modifier.padding(padding).verticalScroll(rememberScrollState())) {
IconPackPicker(vm.icons)
var pictures by remember { mutableStateOf(vm.settings.descriptionPictures) }
var pictures by remember { mutableStateOf(vm.settings.descriptionPictures.get()) }
Toggle("Show pictures in descriptions", pictures) {
pictures = it
vm.setDescriptionPictures(it)
}
Section("Channel tree")
var flags by remember { mutableStateOf(vm.settings.countryFlags) }
var flags by remember { mutableStateOf(vm.settings.countryFlags.get()) }
Toggle("Show country flags in the channel tree", flags) {
flags = it
vm.setCountryFlags(it)
@@ -35,6 +35,7 @@ import eu.lixko.ts3j.client.android.SessionController
import eu.lixko.ts3j.client.android.audio.AudioRoute
import eu.lixko.ts3j.client.audio.AudioDevice
import eu.lixko.ts3j.client.audio.InputLevel
import eu.lixko.ts3j.client.config.Setting
import eu.lixko.ts3j.client.config.Settings
import kotlin.math.roundToInt
@@ -44,17 +45,17 @@ private val DENOISE_LEVELS = listOf("Off", "6 dB", "12 dB", "18 dB", "21 dB")
@Composable
fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) {
val settings = vm.settings
fun change(edit: Settings.() -> Unit) {
settings.edit()
fun <T> change(setting: Setting<T>, value: T) {
setting.set(value)
vm.applySettings()
}
var inputMode by remember { mutableStateOf(settings.inputMode) }
var vadMode by remember { mutableStateOf(settings.vadMode) }
var agc by remember { mutableStateOf(settings.agc) }
var mutedTalkWarning by remember { mutableStateOf(settings.mutedTalkWarning) }
var threshold by remember { mutableFloatStateOf(settings.vadThresholdDb.toFloat()) }
var inputDevice by remember { mutableStateOf(settings.inputDevice) }
var outputDevice by remember { mutableStateOf(settings.outputDevice) }
var inputMode by remember { mutableStateOf(settings.inputMode.get()) }
var vadMode by remember { mutableStateOf(settings.vadMode.get()) }
var agc by remember { mutableStateOf(settings.agc.get()) }
var mutedTalkWarning by remember { mutableStateOf(settings.mutedTalkWarning.get()) }
var threshold by remember { mutableFloatStateOf(settings.vadThresholdDb.get().toFloat()) }
var inputDevice by remember { mutableStateOf(settings.inputDevice.get()) }
var outputDevice by remember { mutableStateOf(settings.outputDevice.get()) }
val inputs by vm.inputDevices.collectAsStateWithLifecycle()
val outputs by vm.outputDevices.collectAsStateWithLifecycle()
val route by vm.route.collectAsStateWithLifecycle()
@@ -71,11 +72,11 @@ fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) {
Section("Devices")
DevicePicker("Microphone", inputs, inputDevice) {
inputDevice = it
change { this.inputDevice = it }
change(settings.inputDevice, it)
}
DevicePicker("Speaker", outputs, outputDevice) {
outputDevice = it
change { this.outputDevice = it }
change(settings.outputDevice, it)
}
Picker(
"Microphone preset",
@@ -95,7 +96,7 @@ fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) {
for ((mode, label) in INPUT_MODES) {
Choice(label, inputMode == mode) {
inputMode = mode
change { this.inputMode = mode }
change(settings.inputMode, mode)
}
}
if (inputMode == Settings.InputMode.PUSH_TO_TALK) FloatingButtonToggle(vm)
@@ -110,46 +111,45 @@ fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) {
for ((mode, label) in VAD_MODES) {
Choice(label, vadMode == mode) {
vadMode = mode
change { this.vadMode = mode }
change(settings.vadMode, mode)
}
}
if (vadMode != Settings.VadMode.AUTOMATIC) {
Level("Volume threshold", settings.vadThresholdDb.toFloat(), -50f..50f, { "${it.roundToInt()}" },
Level("Volume threshold", settings.vadThresholdDb.get().toFloat(), -50f..50f, { "${it.roundToInt()}" },
onChange = { threshold = it }) {
change { vadThresholdDb = it.roundToInt().toDouble() }
change(settings.vadThresholdDb, it.roundToInt().toDouble())
}
}
if (vadMode != Settings.VadMode.VOLUME_GATE) {
Level("Speech likelihood", settings.speechThreshold.toFloat(), 0f..1f, { "${(it * 100).roundToInt()} %" }) {
change { speechThreshold = it.toDouble() }
Level("Speech likelihood", settings.speechThreshold.get().toFloat(), 0f..1f, { "${(it * 100).roundToInt()} %" }) {
change(settings.speechThreshold, it.toDouble())
}
}
}
Section("Processing")
val suppression = if (settings.denoise) settings.denoiserLevel + 1 else 0
val suppression = if (settings.denoise.get()) settings.denoiserLevel.get() + 1 else 0
Level("Noise suppression", suppression.toFloat(), 0f..DENOISE_LEVELS.lastIndex.toFloat(),
{ DENOISE_LEVELS[it.roundToInt()] }, steps = DENOISE_LEVELS.lastIndex - 1) {
val step = it.roundToInt()
change {
denoise = step > 0
if (step > 0) denoiserLevel = step - 1
}
settings.denoise.set(step > 0)
if (step > 0) settings.denoiserLevel.set(step - 1)
vm.applySettings()
}
Toggle("Automatic gain control", agc) {
agc = it
change { this.agc = it }
change(settings.agc, it)
}
if (agc) {
Level("Boost quiet speech", settings.agcBoostDb.toFloat(), 0f..Settings.MAX_AGC_BOOST_DB.toFloat(),
Level("Boost quiet speech", settings.agcBoostDb.get().toFloat(), 0f..Settings.MAX_AGC_BOOST_DB.toFloat(),
{ "+${it.roundToInt()} dB" }, steps = Settings.MAX_AGC_BOOST_DB - 1) {
change { agcBoostDb = it.roundToInt() }
change(settings.agcBoostDb, it.roundToInt())
}
}
Section("Playback")
Level("Volume", settings.outputVolume.toFloat(), 0f..2f, { "${(it * 100).roundToInt()} %" }) {
change { outputVolume = it.toDouble() }
Level("Volume", settings.outputVolume.get().toFloat(), 0f..2f, { "${(it * 100).roundToInt()} %" }) {
change(settings.outputVolume, it.toDouble())
}
Section("Notification sounds")
@@ -161,12 +161,12 @@ fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) {
pack = id
}
}
Level("Volume", settings.soundVolume.toFloat(), 0f..1f, { "${(it * 100).roundToInt()} %" }) {
change { soundVolume = it.toDouble() }
Level("Volume", settings.soundVolume.get().toFloat(), 0f..1f, { "${(it * 100).roundToInt()} %" }) {
change(settings.soundVolume, it.toDouble())
}
Toggle("Beep when talking while muted", mutedTalkWarning) {
mutedTalkWarning = it
change { this.mutedTalkWarning = it }
change(settings.mutedTalkWarning, it)
}
}
}