kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: simplify `wantConfig` feedback
rodzic
88a6bcc09d
commit
814cf41f90
|
@ -50,6 +50,7 @@ class BTScanModel @Inject constructor(
|
|||
|
||||
private val context: Context get() = application.applicationContext
|
||||
val devices = MutableLiveData<MutableMap<String, DeviceListEntry>>(mutableMapOf())
|
||||
val errorText = MutableLiveData<String?>(null)
|
||||
|
||||
val isMockInterfaceAddressValid: Boolean by lazy {
|
||||
radioInterfaceService.isAddressValid(radioInterfaceService.mockInterfaceAddress)
|
||||
|
@ -87,6 +88,10 @@ class BTScanModel @Inject constructor(
|
|||
}
|
||||
}.launchIn(viewModelScope)
|
||||
|
||||
serviceRepository.statusMessage
|
||||
.onEach { errorText.value = it }
|
||||
.launchIn(viewModelScope)
|
||||
|
||||
debug("BTScanModel created")
|
||||
}
|
||||
|
||||
|
@ -134,8 +139,6 @@ class BTScanModel @Inject constructor(
|
|||
debug("BTScanModel cleared")
|
||||
}
|
||||
|
||||
val errorText = MutableLiveData<String?>(null)
|
||||
|
||||
fun setErrorText(text: String) {
|
||||
errorText.value = text
|
||||
}
|
||||
|
|
|
@ -127,8 +127,6 @@ class UIViewModel @Inject constructor(
|
|||
val bondedAddress get() = radioInterfaceService.getBondedDeviceAddress()
|
||||
val selectedBluetooth get() = radioInterfaceService.getDeviceAddress()?.getOrNull(0) == 'x'
|
||||
|
||||
val wantConfigState get() = radioConfigRepository.wantConfigState
|
||||
|
||||
private val _packets = MutableStateFlow<List<Packet>>(emptyList())
|
||||
val packets: StateFlow<List<Packet>> = _packets
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ import com.geeksville.mesh.model.NodeDB
|
|||
import com.geeksville.mesh.model.getChannelUrl
|
||||
import com.geeksville.mesh.service.MeshService.ConnectionState
|
||||
import com.geeksville.mesh.service.ServiceRepository
|
||||
import com.geeksville.mesh.service.WantConfigState
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
|
@ -39,28 +38,6 @@ class RadioConfigRepository @Inject constructor(
|
|||
) {
|
||||
val meshService: IMeshService? get() = serviceRepository.meshService
|
||||
|
||||
val wantConfigState: StateFlow<WantConfigState> = serviceRepository.wantConfigState
|
||||
|
||||
fun clearWantConfigState() {
|
||||
serviceRepository.setWantConfigState { WantConfigState() }
|
||||
}
|
||||
|
||||
fun increaseNodeCount() {
|
||||
serviceRepository.setWantConfigState { it.copy(nodeCount = it.nodeCount + 1) }
|
||||
}
|
||||
|
||||
fun increaseChannelCount() {
|
||||
serviceRepository.setWantConfigState { it.copy(channelCount = it.channelCount + 1) }
|
||||
}
|
||||
|
||||
fun increaseConfigCount() {
|
||||
serviceRepository.setWantConfigState { it.copy(configCount = it.configCount + 1) }
|
||||
}
|
||||
|
||||
fun increaseModuleCount() {
|
||||
serviceRepository.setWantConfigState { it.copy(moduleCount = it.moduleCount + 1) }
|
||||
}
|
||||
|
||||
// Connection state to our radio device
|
||||
val connectionState get() = serviceRepository.connectionState
|
||||
fun setConnectionState(state: ConnectionState) = serviceRepository.setConnectionState(state)
|
||||
|
@ -192,6 +169,10 @@ class RadioConfigRepository @Inject constructor(
|
|||
serviceRepository.clearErrorMessage()
|
||||
}
|
||||
|
||||
fun setStatusMessage(text: String) {
|
||||
serviceRepository.setStatusMessage(text)
|
||||
}
|
||||
|
||||
val meshPacketFlow: SharedFlow<MeshPacket> get() = serviceRepository.meshPacketFlow
|
||||
|
||||
suspend fun emitMeshPacket(packet: MeshPacket) = coroutineScope {
|
||||
|
|
|
@ -361,6 +361,9 @@ class MeshService : Service(), Logging {
|
|||
|
||||
var myNodeInfo: MyNodeInfo? = null
|
||||
|
||||
private val configTotal by lazy { ConfigProtos.Config.getDescriptor().fields.size }
|
||||
private val moduleTotal by lazy { ModuleConfigProtos.ModuleConfig.getDescriptor().fields.size }
|
||||
|
||||
private var localConfig: LocalConfig = LocalConfig.getDefaultInstance()
|
||||
private var moduleConfig: LocalModuleConfig = LocalModuleConfig.getDefaultInstance()
|
||||
private var channelSet: AppOnlyProtos.ChannelSet = AppOnlyProtos.ChannelSet.getDefaultInstance()
|
||||
|
@ -1262,7 +1265,8 @@ class MeshService : Service(), Logging {
|
|||
)
|
||||
insertMeshLog(packetToSave)
|
||||
setLocalConfig(config)
|
||||
radioConfigRepository.increaseConfigCount()
|
||||
val configCount = localConfig.allFields.size
|
||||
radioConfigRepository.setStatusMessage("Device config ($configCount / $configTotal)")
|
||||
}
|
||||
|
||||
private fun handleModuleConfig(config: ModuleConfigProtos.ModuleConfig) {
|
||||
|
@ -1275,7 +1279,8 @@ class MeshService : Service(), Logging {
|
|||
)
|
||||
insertMeshLog(packetToSave)
|
||||
setLocalModuleConfig(config)
|
||||
radioConfigRepository.increaseModuleCount()
|
||||
val moduleCount = moduleConfig.allFields.size
|
||||
radioConfigRepository.setStatusMessage("Module config ($moduleCount / $moduleTotal)")
|
||||
}
|
||||
|
||||
private fun handleQueueStatus(queueStatus: MeshProtos.QueueStatus) {
|
||||
|
@ -1298,7 +1303,8 @@ class MeshService : Service(), Logging {
|
|||
)
|
||||
insertMeshLog(packetToSave)
|
||||
if (ch.role != ChannelProtos.Channel.Role.DISABLED) updateChannelSettings(ch)
|
||||
radioConfigRepository.increaseChannelCount()
|
||||
val maxChannels = myNodeInfo?.maxChannels ?: 8
|
||||
radioConfigRepository.setStatusMessage("Channels (${ch.index + 1} / $maxChannels)")
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1340,7 +1346,7 @@ class MeshService : Service(), Logging {
|
|||
insertMeshLog(packetToSave)
|
||||
|
||||
newNodes.add(info)
|
||||
radioConfigRepository.increaseNodeCount()
|
||||
radioConfigRepository.setStatusMessage("Nodes (${newNodes.size} / 100)")
|
||||
}
|
||||
|
||||
|
||||
|
@ -1551,7 +1557,6 @@ class MeshService : Service(), Logging {
|
|||
configNonce += 1
|
||||
newNodes.clear()
|
||||
newMyNodeInfo = null
|
||||
radioConfigRepository.clearWantConfigState()
|
||||
|
||||
if (BluetoothInterface.invalidVersion) onHasSettings() // Device firmware is too old
|
||||
|
||||
|
|
|
@ -7,17 +7,9 @@ import kotlinx.coroutines.flow.MutableSharedFlow
|
|||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.updateAndGet
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
data class WantConfigState(
|
||||
var nodeCount: Int = 0,
|
||||
var channelCount: Int = 0,
|
||||
var configCount: Int = 0,
|
||||
var moduleCount: Int = 0,
|
||||
)
|
||||
|
||||
/**
|
||||
* Repository class for managing the [IMeshService] instance and connection state
|
||||
*/
|
||||
|
@ -50,11 +42,14 @@ class ServiceRepository @Inject constructor() : Logging {
|
|||
_errorMessage.value = null
|
||||
}
|
||||
|
||||
private val _wantConfigState = MutableStateFlow(WantConfigState())
|
||||
val wantConfigState: StateFlow<WantConfigState> = _wantConfigState
|
||||
private val _statusMessage = MutableStateFlow<String?>(null)
|
||||
val statusMessage: StateFlow<String?> get() = _statusMessage
|
||||
|
||||
fun setWantConfigState(update: (old: WantConfigState) -> WantConfigState): WantConfigState =
|
||||
_wantConfigState.updateAndGet(update)
|
||||
fun setStatusMessage(text: String) {
|
||||
if (connectionState.value != MeshService.ConnectionState.CONNECTED) {
|
||||
_statusMessage.value = text
|
||||
}
|
||||
}
|
||||
|
||||
private val _meshPacketFlow = MutableSharedFlow<MeshPacket>()
|
||||
val meshPacketFlow: SharedFlow<MeshPacket> get() = _meshPacketFlow
|
||||
|
|
|
@ -20,7 +20,6 @@ import androidx.fragment.app.activityViewModels
|
|||
import androidx.lifecycle.asLiveData
|
||||
import com.geeksville.mesh.ConfigProtos
|
||||
import com.geeksville.mesh.R
|
||||
import com.geeksville.mesh.ModuleConfigProtos
|
||||
import com.geeksville.mesh.android.*
|
||||
import com.geeksville.mesh.databinding.SettingsFragmentBinding
|
||||
import com.geeksville.mesh.model.BTScanModel
|
||||
|
@ -188,20 +187,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
|||
updateNodeInfo()
|
||||
}
|
||||
|
||||
model.wantConfigState.asLiveData().observe(viewLifecycleOwner) {
|
||||
if (model.isConnected()) return@observe
|
||||
val configTotal = ConfigProtos.Config.getDescriptor().fields.size
|
||||
val moduleTotal = ModuleConfigProtos.ModuleConfig.getDescriptor().fields.size
|
||||
|
||||
binding.scanStatusText.text = when {
|
||||
it.moduleCount > 0 -> "Module config (${it.moduleCount} / $moduleTotal)"
|
||||
it.configCount > 0 -> "Device config (${it.configCount} / $configTotal)"
|
||||
it.channelCount > 0 -> "Channels (${it.channelCount} / ${model.maxChannels})"
|
||||
it.nodeCount > 0 -> "Nodes (${it.nodeCount} / 100)"
|
||||
else -> return@observe
|
||||
}
|
||||
}
|
||||
|
||||
model.localConfig.asLiveData().observe(viewLifecycleOwner) {
|
||||
if (model.isConnected()) updateNodeInfo()
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue