Decouple ConnectionsScreen from UiViewModel (#3126)

This commit is contained in:
Phil Oliver 2025-09-17 11:10:43 -04:00 committed by GitHub
parent 7afab16011
commit 1d30367ddc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 153 additions and 63 deletions

View file

@ -79,6 +79,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
@ -333,15 +334,6 @@ constructor(
private val _showQuickChat = MutableStateFlow(uiPrefs.showQuickChat)
val showQuickChat: StateFlow<Boolean> = _showQuickChat
private val _hasShownNotPairedWarning = MutableStateFlow(uiPrefs.hasShownNotPairedWarning)
val hasShownNotPairedWarning: StateFlow<Boolean> = _hasShownNotPairedWarning.asStateFlow()
fun suppressNoPairedWarning() {
_hasShownNotPairedWarning.value = true
uiPrefs.hasShownNotPairedWarning = true
}
fun toggleShowIgnored() = toggle(_showIgnored) { uiPrefs.showIgnored = it }
fun toggleShowQuickChat() = toggle(_showQuickChat) { uiPrefs.showQuickChat = it }
@ -818,26 +810,21 @@ constructor(
if (config.lora != newConfig.lora) setConfig(newConfig)
}
fun refreshProvideLocation() {
viewModelScope.launch { setProvideLocation(getProvidePref()) }
}
private fun getProvidePref(): Boolean = uiPrefs.shouldProvideNodeLocation(myNodeNum)
private val _provideLocation = MutableStateFlow(getProvidePref())
val provideLocation: StateFlow<Boolean>
get() = _provideLocation.asStateFlow()
get() =
myNodeInfo
.flatMapLatest { myNodeEntity ->
// When myNodeInfo changes, set up emissions for the "provide-location-nodeNum" pref.
if (myNodeEntity == null) {
flowOf(false)
} else {
uiPrefs.shouldProvideNodeLocation(myNodeEntity.myNodeNum)
}
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), false)
fun setProvideLocation(value: Boolean) {
viewModelScope.launch {
uiPrefs.setShouldProvideNodeLocation(myNodeNum, value)
_provideLocation.value = value
if (value) {
meshService?.startProvideLocation()
} else {
meshService?.stopProvideLocation()
}
}
myNodeNum?.let { uiPrefs.setShouldProvideNodeLocation(it, value) }
}
fun setOwner(name: String) {