refactor: improve connection status feedback using new `wantConfig` state

pull/1017/head
andrekir 2024-05-01 09:23:54 -03:00 zatwierdzone przez Andre K
rodzic 78d99e44d0
commit a12c5052bc
5 zmienionych plików z 58 dodań i 22 usunięć

Wyświetl plik

@ -115,6 +115,8 @@ 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

Wyświetl plik

@ -17,6 +17,7 @@ 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
@ -38,6 +39,28 @@ 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)

Wyświetl plik

@ -1255,6 +1255,7 @@ class MeshService : Service(), Logging {
)
insertMeshLog(packetToSave)
setLocalConfig(config)
radioConfigRepository.increaseConfigCount()
}
private fun handleModuleConfig(config: ModuleConfigProtos.ModuleConfig) {
@ -1267,6 +1268,7 @@ class MeshService : Service(), Logging {
)
insertMeshLog(packetToSave)
setLocalModuleConfig(config)
radioConfigRepository.increaseModuleCount()
}
private fun handleQueueStatus(queueStatus: MeshProtos.QueueStatus) {
@ -1289,6 +1291,7 @@ class MeshService : Service(), Logging {
)
insertMeshLog(packetToSave)
if (ch.role != ChannelProtos.Channel.Role.DISABLED) updateChannelSettings(ch)
radioConfigRepository.increaseChannelCount()
}
/**
@ -1330,6 +1333,7 @@ class MeshService : Service(), Logging {
insertMeshLog(packetToSave)
newNodes.add(info)
radioConfigRepository.increaseNodeCount()
}
@ -1540,6 +1544,7 @@ class MeshService : Service(), Logging {
configNonce += 1
newNodes.clear()
newMyNodeInfo = null
radioConfigRepository.clearWantConfigState()
if (BluetoothInterface.invalidVersion) onHasSettings() // Device firmware is too old

Wyświetl plik

@ -7,9 +7,17 @@ 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
*/
@ -42,6 +50,12 @@ class ServiceRepository @Inject constructor() : Logging {
_errorMessage.value = null
}
private val _wantConfigState = MutableStateFlow(WantConfigState())
val wantConfigState: StateFlow<WantConfigState> = _wantConfigState
fun setWantConfigState(update: (old: WantConfigState) -> WantConfigState): WantConfigState =
_wantConfigState.updateAndGet(update)
private val _meshPacketFlow = MutableSharedFlow<MeshPacket>()
val meshPacketFlow: SharedFlow<MeshPacket> get() = _meshPacketFlow

Wyświetl plik

@ -188,32 +188,24 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
updateNodeInfo()
}
model.localConfig.asLiveData().observe(viewLifecycleOwner) {
if (!model.isConnected()) {
val configCount = it.allFields.size
val configTotal = ConfigProtos.Config.getDescriptor().fields.size
if (configCount > 0)
scanModel.setErrorText("Device config ($configCount / $configTotal)")
} else 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
model.moduleConfig.asLiveData().observe(viewLifecycleOwner) {
if (!model.isConnected()) {
val moduleCount = it.allFields.size
val moduleTotal = ModuleConfigProtos.ModuleConfig.getDescriptor().fields.size
if (moduleCount > 0)
scanModel.setErrorText("Module config ($moduleCount / $moduleTotal)")
} else updateNodeInfo()
}
model.channels.asLiveData().observe(viewLifecycleOwner) {
if (!model.isConnected()) {
val maxChannels = model.maxChannels
if (!it.hasLoraConfig() && it.settingsCount > 0)
scanModel.setErrorText("Channels (${it.settingsCount} / $maxChannels)")
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()
}
// Also watch myNodeInfo because it might change later
model.myNodeInfo.asLiveData().observe(viewLifecycleOwner) {
updateNodeInfo()