Refactored AudioScreen.kt to use LaunchedEffect for state initialization

This commit is contained in:
Juha Heinanen
2026-07-02 21:04:18 +03:00
parent 01309c241d
commit 7ca288060c

View File

@ -44,6 +44,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -137,19 +138,19 @@ private fun AudioScreen(
} }
} }
private var newCallVolume = BaresipService.callVolume private var newCallVolume = 0
private var oldMicGain = "" private var oldMicGain = ""
private var newMicGain = "" private var newMicGain = ""
private var oldSpeakerPhone = BaresipService.speakerPhone private var oldSpeakerPhone = false
private var newSpeakerPhone = oldSpeakerPhone private var newSpeakerPhone = false
private var oldAudioModules = ArrayList<String>() private var oldAudioModules = ArrayList<String>()
private var newAudioModules = mutableMapOf<String, Boolean>() private var newAudioModules = mutableMapOf<String, Boolean>()
private var oldOpusBitrate = "" private var oldOpusBitrate = ""
private var newOpusBitrate = oldOpusBitrate private var newOpusBitrate = ""
private var oldOpusPacketLoss = "" private var oldOpusPacketLoss = ""
private var newOpusPacketLoss = oldOpusPacketLoss private var newOpusPacketLoss = ""
private var newAudioDelay = BaresipService.audioDelay.toString() private var newAudioDelay = ""
private var newToneCountry = BaresipService.toneCountry private var newToneCountry = ""
private var newRingtoneUri = "" private var newRingtoneUri = ""
private var save = false private var save = false
@ -161,13 +162,29 @@ private val showAlert = mutableStateOf(false)
@Composable @Composable
private fun AudioContent(contentPadding: PaddingValues) { private fun AudioContent(contentPadding: PaddingValues) {
oldSpeakerPhone = Config.variable("speaker_phone") == "yes" val ctx = LocalContext.current
newSpeakerPhone = oldSpeakerPhone
oldAudioModules = Config.variables("module") LaunchedEffect(Unit) {
oldOpusBitrate = Config.variable("opus_bitrate") save = false
oldOpusPacketLoss = Config.variable("opus_packet_loss") oldSpeakerPhone = Config.variable("speaker_phone") == "yes"
if (!BaresipService.agcAvailable) newSpeakerPhone = oldSpeakerPhone
oldMicGain = Config.variable("augain") oldAudioModules = Config.variables("module")
newAudioModules.clear()
oldOpusBitrate = Config.variable("opus_bitrate")
newOpusBitrate = oldOpusBitrate
oldOpusPacketLoss = Config.variable("opus_packet_loss")
newOpusPacketLoss = oldOpusPacketLoss
if (!BaresipService.agcAvailable)
oldMicGain = Config.variable("augain")
newMicGain = oldMicGain
newCallVolume = BaresipService.callVolume
newAudioDelay = BaresipService.audioDelay.toString()
newToneCountry = BaresipService.toneCountry
newRingtoneUri = if (Preferences(ctx).ringtoneUri == "")
RingtoneManager.getActualDefaultRingtoneUri(ctx, RingtoneManager.TYPE_RINGTONE).toString()
else
Preferences(ctx).ringtoneUri!!
}
if (showAlert.value) { if (showAlert.value) {
AlertDialog( AlertDialog(
@ -206,11 +223,8 @@ private fun Ringtone() {
val ringToneTitle = stringResource(R.string.ringtone) val ringToneTitle = stringResource(R.string.ringtone)
val selectRingToneMessage = stringResource(R.string.select_ringtone) val selectRingToneMessage = stringResource(R.string.select_ringtone)
val ctx = LocalContext.current val ctx = LocalContext.current
var ringtoneUri by remember { var ringtoneUri by remember(newRingtoneUri) {
mutableStateOf(if (Preferences(ctx).ringtoneUri == "") mutableStateOf(newRingtoneUri)
RingtoneManager.getActualDefaultRingtoneUri(ctx, RingtoneManager.TYPE_RINGTONE).toString()
else
Preferences(ctx).ringtoneUri!!)
} }
newRingtoneUri = ringtoneUri newRingtoneUri = ringtoneUri
val launcher = rememberLauncherForActivityResult( val launcher = rememberLauncherForActivityResult(
@ -282,8 +296,8 @@ private fun ToneCountry() {
} }
val countryNames = arrayListOf("BG", "BR", "DE", "CZ", "ES", "FI", "FR", "GB", "JP", "NO", "NZ", "SE", "RU", "US") val countryNames = arrayListOf("BG", "BR", "DE", "CZ", "ES", "FI", "FR", "GB", "JP", "NO", "NZ", "SE", "RU", "US")
val countryValues = arrayListOf("bg", "br", "de", "cz", "es", "fi", "fr", "uk", "jp", "no", "nz", "se", "ru", "us") val countryValues = arrayListOf("bg", "br", "de", "cz", "es", "fi", "fr", "uk", "jp", "no", "nz", "se", "ru", "us")
val itemPosition = remember { val itemPosition = remember(newToneCountry) {
mutableIntStateOf(countryValues.indexOf(BaresipService.toneCountry)) mutableIntStateOf(countryValues.indexOf(newToneCountry))
} }
Box { Box {
Row( Row(
@ -338,7 +352,7 @@ private fun SpeakerPhone() {
showAlert.value = true showAlert.value = true
}, },
fontSize = 18.sp) fontSize = 18.sp)
var speakerPhone by remember { mutableStateOf(oldSpeakerPhone) } var speakerPhone by remember(oldSpeakerPhone) { mutableStateOf(oldSpeakerPhone) }
Switch( Switch(
checked = speakerPhone, checked = speakerPhone,
onCheckedChange = { speakerPhone = it onCheckedChange = { speakerPhone = it
@ -370,8 +384,8 @@ private fun CallVolume() {
} }
val volNames = listOf("--", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10") val volNames = listOf("--", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
val volValues = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val volValues = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val itemPosition = remember { val itemPosition = remember(newCallVolume) {
mutableIntStateOf(volValues.indexOf(BaresipService.callVolume)) mutableIntStateOf(volValues.indexOf(newCallVolume))
} }
Box { Box {
Row( Row(
@ -419,7 +433,7 @@ private fun MicGain() {
) { ) {
val microphoneGainTitle = stringResource(R.string.microphone_gain) val microphoneGainTitle = stringResource(R.string.microphone_gain)
val microphoneGainHelp = stringResource(R.string.microphone_gain_help) val microphoneGainHelp = stringResource(R.string.microphone_gain_help)
var micGain by remember { mutableStateOf(oldMicGain) } var micGain by remember(oldMicGain) { mutableStateOf(oldMicGain) }
newMicGain = micGain newMicGain = micGain
OutlinedTextField( OutlinedTextField(
value = micGain, value = micGain,
@ -463,7 +477,7 @@ private fun AudioModules() {
) { ) {
Text(text = String.format(stringResource(R.string.bullet_item), module), fontSize = 18.sp) Text(text = String.format(stringResource(R.string.bullet_item), module), fontSize = 18.sp)
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
var checked by remember { mutableStateOf(oldAudioModules.contains("${module}.so")) } var checked by remember(oldAudioModules) { mutableStateOf(oldAudioModules.contains("${module}.so")) }
Switch( Switch(
checked = checked, checked = checked,
onCheckedChange = { onCheckedChange = {
@ -485,7 +499,7 @@ private fun OpusBitRate() {
) { ) {
val opusBitRateTitle = stringResource(R.string.opus_bit_rate) val opusBitRateTitle = stringResource(R.string.opus_bit_rate)
val opusBitRateHelp = stringResource(R.string.opus_bit_rate_help) val opusBitRateHelp = stringResource(R.string.opus_bit_rate_help)
var opusBitrate by remember { mutableStateOf(oldOpusBitrate) } var opusBitrate by remember(oldOpusBitrate) { mutableStateOf(oldOpusBitrate) }
newOpusBitrate = opusBitrate newOpusBitrate = opusBitrate
OutlinedTextField( OutlinedTextField(
value = opusBitrate, value = opusBitrate,
@ -517,7 +531,7 @@ private fun OpusPacketLoss() {
) { ) {
val opusPacketLossTitle = stringResource(R.string.opus_packet_loss) val opusPacketLossTitle = stringResource(R.string.opus_packet_loss)
val opusPacketLossHelp = stringResource(R.string.opus_packet_loss_help) val opusPacketLossHelp = stringResource(R.string.opus_packet_loss_help)
var opusPacketLoss by remember { mutableStateOf(oldOpusPacketLoss) } var opusPacketLoss by remember(oldOpusPacketLoss) { mutableStateOf(oldOpusPacketLoss) }
newOpusPacketLoss = opusPacketLoss newOpusPacketLoss = opusPacketLoss
OutlinedTextField( OutlinedTextField(
value = opusPacketLoss, value = opusPacketLoss,
@ -549,7 +563,7 @@ private fun AudioDelay() {
) { ) {
val audioDelayTitle = stringResource(R.string.audio_delay) val audioDelayTitle = stringResource(R.string.audio_delay)
val audioDelayHelp = stringResource(R.string.audio_delay_help) val audioDelayHelp = stringResource(R.string.audio_delay_help)
var audioDelay by remember { mutableStateOf(BaresipService.audioDelay.toString()) } var audioDelay by remember(newAudioDelay) { mutableStateOf(newAudioDelay) }
newAudioDelay = audioDelay newAudioDelay = audioDelay
OutlinedTextField( OutlinedTextField(
value = audioDelay, value = audioDelay,