Use SettingsViewModel to store settings modifications before they are saved

This commit is contained in:
Juha Heinanen
2025-10-18 10:57:11 +03:00
parent 9e64fcd07f
commit 609578abe6
4 changed files with 1176 additions and 1162 deletions

View File

@ -1,8 +1,6 @@
package com.tutpro.baresip
import android.content.Context
import androidx.activity.ComponentActivity
import androidx.activity.compose.LocalActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
@ -50,7 +48,6 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
@ -62,13 +59,16 @@ fun NavGraphBuilder.audioScreenRoute(
navController: NavController,
) {
composable("audio") {
val activity = LocalActivity.current
val viewModel: ViewModel = viewModel(activity as ComponentActivity)
val ctx = LocalContext.current
AudioScreen(
onBack = { navController.popBackStack() },
onBack = {
navController.popBackStack()
},
checkOnClick = {
viewModel.setAudioSettingsResult(checkOnClick(ctx))
val result = checkOnClick(ctx) // Your existing logic that returns true/false
navController.previousBackStackEntry
?.savedStateHandle
?.set("audio_settings_result", result)
navController.popBackStack()
},
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,100 @@
package com.tutpro.baresip
import android.app.role.RoleManager
import android.content.Context
import android.content.Context.POWER_SERVICE
import android.content.Context.ROLE_SERVICE
import android.media.RingtoneManager
import android.os.Build
import android.os.PowerManager
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.mutableIntStateOf
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import java.io.File
class SettingsViewModel: ViewModel() {
val autoStart = MutableStateFlow(false)
val listenAddress = MutableStateFlow("")
val addressFamily = MutableStateFlow("")
val dnsServers = MutableStateFlow("")
val oldDnsServers = if (Config.variable("dyn_dns") == "yes")
""
else {
val servers = Config.variables("dns_server")
var serverList = ""
for (server in servers)
serverList += ", $server"
serverList.trimStart(',').trimStart(' ')
}
val tlsCertificateFile = MutableStateFlow(false)
val verifyServer = MutableStateFlow(false)
val caFile = MutableStateFlow(false)
val userAgent = MutableStateFlow("")
val contactsMode = MutableStateFlow("")
val ringtoneUri = MutableStateFlow("")
val batteryOptimizations = MutableStateFlow(false)
val darkTheme = MutableStateFlow(false)
val dynamicColors = MutableStateFlow(false)
val colorblind = MutableStateFlow(false)
val proximitySensing = MutableStateFlow(false)
val defaultDialer = MutableStateFlow(false)
val debug = MutableStateFlow(false)
val sipTrace = MutableStateFlow(false)
private var isLoaded = false
fun loadSettings(ctx: Context) {
if (isLoaded) return else isLoaded = true
autoStart.value = Config.variable("auto_start") == "yes"
listenAddress.value = Config.variable("sip_listen")
val familyValues = listOf("", "ipv4", "ipv6")
val itemPosition =
mutableIntStateOf(familyValues.indexOf(Config.variable("net_af").lowercase()))
addressFamily.value = familyValues[itemPosition.intValue]
dnsServers.value = oldDnsServers
tlsCertificateFile.value = File(BaresipService.filesPath + "/cert.pem").exists()
verifyServer.value = Config.variable("sip_verify_server") == "yes"
caFile.value = File(BaresipService.filesPath + "/ca_certs.crt").exists()
userAgent.value = Config.variable("user_agent")
contactsMode.value = Config.variable("contacts_mode").lowercase()
ringtoneUri.value = if (Preferences(ctx).ringtoneUri == "")
RingtoneManager.getActualDefaultRingtoneUri(ctx, RingtoneManager.TYPE_RINGTONE).toString()
else
Preferences(ctx).ringtoneUri!!
val powerManager = ctx.getSystemService(POWER_SERVICE) as PowerManager
batteryOptimizations.value = !powerManager.isIgnoringBatteryOptimizations(ctx.packageName)
darkTheme.value = Preferences(ctx).displayTheme == AppCompatDelegate.MODE_NIGHT_YES
dynamicColors.value = BaresipService.dynamicColors.value
colorblind.value = Config.variable("colorblind") == "yes"
proximitySensing.value = Config.variable("proximity_sensing") == "yes"
if (Build.VERSION.SDK_INT >= 29) {
val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager
defaultDialer.value = roleManager.isRoleHeld(RoleManager.ROLE_DIALER)
}
debug.value = Config.variable("log_level") == "0"
sipTrace.value = BaresipService.sipTrace
}
}

View File

@ -1,16 +1,15 @@
package com.tutpro.baresip
import android.app.Application
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableIntStateOf
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
sealed class NavigationCommand {
@ -109,17 +108,6 @@ class ViewModel(application: Application) : AndroidViewModel(application) {
_dialpadIcon.value = newIcon
}
private val _audioSettingsResult = mutableStateOf<Boolean?>(null)
val audioSettingsResult: State<Boolean?> get() = _audioSettingsResult
fun setAudioSettingsResult(result: Boolean) {
_audioSettingsResult.value = result
}
fun clearAudioSettingsResult() {
_audioSettingsResult.value = null
}
private val _selectedCallRow = MutableStateFlow<CallRow?>(null)
fun selectCallRow(callRow: CallRow) {