Migrate map consent prefs to repo pattern (#2771)

This commit is contained in:
Phil Oliver
2025-08-18 13:46:57 -04:00
committed by GitHub
parent ae9b5f4db3
commit 6ec66bfaea
2 changed files with 23 additions and 14 deletions

View File

@@ -44,6 +44,7 @@ import com.geeksville.mesh.Portnums
import com.geeksville.mesh.Position
import com.geeksville.mesh.R
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.android.prefs.MapConsentPrefs
import com.geeksville.mesh.config
import com.geeksville.mesh.database.entity.MyNodeEntity
import com.geeksville.mesh.deviceProfile
@@ -101,6 +102,7 @@ constructor(
private val app: Application,
private val radioConfigRepository: RadioConfigRepository,
private val locationRepository: LocationRepository,
private val mapConsentPrefs: MapConsentPrefs,
) : ViewModel(),
Logging {
private val meshService: IMeshService?
@@ -545,6 +547,12 @@ constructor(
}
}
fun shouldReportLocation(nodeNum: Int?) = mapConsentPrefs.shouldReportLocation(nodeNum)
fun setShouldReportLocation(nodeNum: Int?, shouldReportLocation: Boolean) {
mapConsentPrefs.setShouldReportLocation(nodeNum, shouldReportLocation)
}
private fun setResponseStateTotal(total: Int) {
_radioConfigState.update { state ->
if (state.responseState is ResponseState.Loading) {

View File

@@ -19,7 +19,6 @@
package com.geeksville.mesh.ui.radioconfig.components
import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.KeyboardActions
@@ -31,13 +30,11 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.content.edit
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.geeksville.mesh.ModuleConfigProtos.ModuleConfig.MQTTConfig
@@ -51,8 +48,6 @@ import com.geeksville.mesh.ui.common.components.PreferenceFooter
import com.geeksville.mesh.ui.common.components.SwitchPreference
import com.geeksville.mesh.ui.radioconfig.RadioConfigViewModel
const val MAP_CONSENT_PREFERENCES_KEY = "map_consent_preferences"
@Composable
fun MQTTConfigScreen(viewModel: RadioConfigViewModel = hiltViewModel()) {
val state by viewModel.radioConfigState.collectAsStateWithLifecycle()
@@ -64,9 +59,12 @@ fun MQTTConfigScreen(viewModel: RadioConfigViewModel = hiltViewModel()) {
}
MQTTConfigItemList(
nodeNum = destNum,
mqttConfig = state.moduleConfig.mqtt,
enabled = state.connected,
shouldReportLocation = viewModel.shouldReportLocation(destNum),
onShouldReportLocationChanged = { shouldReportLocation ->
viewModel.setShouldReportLocation(destNum, shouldReportLocation)
},
onSaveClicked = { mqttInput ->
val config = moduleConfig { mqtt = mqttInput }
viewModel.setModuleConfig(config)
@@ -76,19 +74,16 @@ fun MQTTConfigScreen(viewModel: RadioConfigViewModel = hiltViewModel()) {
@Composable
fun MQTTConfigItemList(
nodeNum: Int? = 0,
mqttConfig: MQTTConfig,
enabled: Boolean,
shouldReportLocation: Boolean,
onShouldReportLocationChanged: (shouldReportLocation: Boolean) -> Unit,
onSaveClicked: (MQTTConfig) -> Unit,
) {
val focusManager = LocalFocusManager.current
var mqttInput by rememberSaveable { mutableStateOf(mqttConfig) }
val sharedPrefs = LocalContext.current.getSharedPreferences(MAP_CONSENT_PREFERENCES_KEY, Context.MODE_PRIVATE)
if (!mqttInput.mapReportSettings.shouldReportLocation) {
val settings =
mqttInput.mapReportSettings.copy {
this.shouldReportLocation = sharedPrefs.getBoolean(nodeNum.toString(), false)
}
val settings = mqttInput.mapReportSettings.copy { this.shouldReportLocation = shouldReportLocation }
mqttInput = mqttInput.copy { mapReportSettings = settings }
}
@@ -206,7 +201,7 @@ fun MQTTConfigItemList(
onMapReportingEnabledChanged = { mqttInput = mqttInput.copy { mapReportingEnabled = it } },
shouldReportLocation = mqttInput.mapReportSettings.shouldReportLocation,
onShouldReportLocationChanged = {
sharedPrefs.edit { putBoolean(nodeNum.toString(), it) }
onShouldReportLocationChanged(it)
val settings = mqttInput.mapReportSettings.copy { this.shouldReportLocation = it }
mqttInput = mqttInput.copy { mapReportSettings = settings }
},
@@ -254,5 +249,11 @@ private const val MIN_INTERVAL_SECS = 3600
@Preview(showBackground = true)
@Composable
private fun MQTTConfigPreview() {
MQTTConfigItemList(mqttConfig = MQTTConfig.getDefaultInstance(), enabled = true, onSaveClicked = {})
MQTTConfigItemList(
mqttConfig = MQTTConfig.getDefaultInstance(),
enabled = true,
shouldReportLocation = true,
onShouldReportLocationChanged = { _ -> },
onSaveClicked = {},
)
}