mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: combine config data stores into RadioConfigRepository (#636)
This commit is contained in:
parent
a4baa93f4e
commit
a2388d1d12
6 changed files with 111 additions and 70 deletions
|
|
@ -3,7 +3,8 @@ package com.geeksville.mesh.repository.datastore
|
|||
import androidx.datastore.core.DataStore
|
||||
import com.geeksville.mesh.android.Logging
|
||||
import com.geeksville.mesh.AppOnlyProtos.ChannelSet
|
||||
import com.geeksville.mesh.ChannelProtos
|
||||
import com.geeksville.mesh.ChannelProtos.Channel
|
||||
import com.geeksville.mesh.ChannelProtos.ChannelSettings
|
||||
import com.geeksville.mesh.ConfigProtos
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
|
|
@ -12,7 +13,7 @@ import java.io.IOException
|
|||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Class that handles saving and retrieving channel settings
|
||||
* Class that handles saving and retrieving [ChannelSet] data.
|
||||
*/
|
||||
class ChannelSetRepository @Inject constructor(
|
||||
private val channelSetStore: DataStore<ChannelSet>
|
||||
|
|
@ -34,20 +35,14 @@ class ChannelSetRepository @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun clearSettings() {
|
||||
channelSetStore.updateData { preference ->
|
||||
preference.toBuilder().clearSettings().build()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the ChannelSettings list with the provided channel and returns the index of the
|
||||
* Updates the [ChannelSettings] list with the provided channel and returns the index of the
|
||||
* admin channel after the update (if not found, returns 0).
|
||||
*/
|
||||
suspend fun updateChannelSettings(channel: ChannelProtos.Channel): Int {
|
||||
suspend fun updateChannelSettings(channel: Channel): Int {
|
||||
channelSetStore.updateData { preference ->
|
||||
if (preference.settingsCount > channel.index) {
|
||||
if (channel.role == ChannelProtos.Channel.Role.DISABLED) {
|
||||
if (channel.role == Channel.Role.DISABLED) {
|
||||
preference.toBuilder().removeSettings(channel.index).build()
|
||||
} else {
|
||||
preference.toBuilder().setSettings(channel.index, channel.settings).build()
|
||||
|
|
@ -59,12 +54,6 @@ class ChannelSetRepository @Inject constructor(
|
|||
return getAdminChannel()
|
||||
}
|
||||
|
||||
suspend fun addAllSettings(channelSet: ChannelSet) {
|
||||
channelSetStore.updateData { preference ->
|
||||
preference.toBuilder().addAllSettings(channelSet.settingsList).build()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setLoraConfig(config: ConfigProtos.Config.LoRaConfig) {
|
||||
channelSetStore.updateData { preference ->
|
||||
preference.toBuilder().setLoraConfig(config).build()
|
||||
|
|
|
|||
|
|
@ -5,19 +5,16 @@ import com.geeksville.mesh.android.Logging
|
|||
import com.geeksville.mesh.ConfigProtos.Config
|
||||
import com.geeksville.mesh.LocalOnlyProtos.LocalConfig
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.first
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Class that handles saving and retrieving config settings
|
||||
* Class that handles saving and retrieving [LocalConfig] data.
|
||||
*/
|
||||
class LocalConfigRepository @Inject constructor(
|
||||
private val localConfigStore: DataStore<LocalConfig>,
|
||||
private val channelSetRepository: ChannelSetRepository,
|
||||
) : Logging {
|
||||
val localConfigFlow: Flow<LocalConfig> = localConfigStore.data
|
||||
.catch { exception ->
|
||||
|
|
@ -30,17 +27,6 @@ class LocalConfigRepository @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private val _setConfigFlow = MutableSharedFlow<Config>()
|
||||
val setConfigFlow: SharedFlow<Config> = _setConfigFlow
|
||||
|
||||
/**
|
||||
* Update LocalConfig and send ConfigProtos.Config Oneof to the radio
|
||||
*/
|
||||
suspend fun setConfig(config: Config) {
|
||||
setLocalConfig(config)
|
||||
_setConfigFlow.emit(config)
|
||||
}
|
||||
|
||||
suspend fun clearLocalConfig() {
|
||||
localConfigStore.updateData { preference ->
|
||||
preference.toBuilder().clear().build()
|
||||
|
|
@ -48,7 +34,7 @@ class LocalConfigRepository @Inject constructor(
|
|||
}
|
||||
|
||||
/**
|
||||
* Update LocalConfig from each ConfigProtos.Config Oneof
|
||||
* Updates [LocalConfig] from each [Config] oneOf.
|
||||
*/
|
||||
suspend fun setLocalConfig(config: Config) {
|
||||
if (config.hasDevice()) setDeviceConfig(config.device)
|
||||
|
|
@ -94,7 +80,6 @@ class LocalConfigRepository @Inject constructor(
|
|||
localConfigStore.updateData { preference ->
|
||||
preference.toBuilder().setLora(config).build()
|
||||
}
|
||||
channelSetRepository.setLoraConfig(config)
|
||||
}
|
||||
|
||||
private suspend fun setBluetoothConfig(config: Config.BluetoothConfig) {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import java.io.IOException
|
|||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Class that handles saving and retrieving config settings
|
||||
* Class that handles saving and retrieving [LocalModuleConfig] data.
|
||||
*/
|
||||
class ModuleConfigRepository @Inject constructor(
|
||||
private val moduleConfigStore: DataStore<LocalModuleConfig>,
|
||||
|
|
@ -34,7 +34,7 @@ class ModuleConfigRepository @Inject constructor(
|
|||
}
|
||||
|
||||
/**
|
||||
* Update LocalModuleConfig from each ModuleConfigProtos.ModuleConfig Oneof
|
||||
* Updates [LocalModuleConfig] from each [ModuleConfig] oneOf.
|
||||
*/
|
||||
suspend fun setLocalModuleConfig(config: ModuleConfig) {
|
||||
if (config.hasMqtt()) setMQTTConfig(config.mqtt)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,84 @@
|
|||
package com.geeksville.mesh.repository.datastore
|
||||
|
||||
import com.geeksville.mesh.AppOnlyProtos.ChannelSet
|
||||
import com.geeksville.mesh.ChannelProtos.Channel
|
||||
import com.geeksville.mesh.ChannelProtos.ChannelSettings
|
||||
import com.geeksville.mesh.ConfigProtos.Config
|
||||
import com.geeksville.mesh.LocalOnlyProtos.LocalConfig
|
||||
import com.geeksville.mesh.LocalOnlyProtos.LocalModuleConfig
|
||||
import com.geeksville.mesh.ModuleConfigProtos.ModuleConfig
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Class responsible for radio configuration data.
|
||||
* Combines access to [ChannelSet], [LocalConfig] & [LocalModuleConfig] data stores.
|
||||
*/
|
||||
class RadioConfigRepository @Inject constructor(
|
||||
private val channelSetRepository: ChannelSetRepository,
|
||||
private val localConfigRepository: LocalConfigRepository,
|
||||
private val moduleConfigRepository: ModuleConfigRepository,
|
||||
) {
|
||||
/**
|
||||
* Flow representing the [ChannelSet] data store.
|
||||
*/
|
||||
val channelSetFlow: Flow<ChannelSet> = channelSetRepository.channelSetFlow
|
||||
|
||||
/**
|
||||
* Clears the [ChannelSet] data in the data store.
|
||||
*/
|
||||
suspend fun clearChannelSet() {
|
||||
channelSetRepository.clearChannelSet()
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the [ChannelSettings] list with the provided channel and returns the index of the
|
||||
* admin channel after the update (if not found, returns 0).
|
||||
* @param channel The [Channel] provided.
|
||||
* @return the index of the admin channel after the update (if not found, returns 0).
|
||||
*/
|
||||
suspend fun updateChannelSettings(channel: Channel): Int {
|
||||
return channelSetRepository.updateChannelSettings(channel)
|
||||
}
|
||||
|
||||
/**
|
||||
* Flow representing the [LocalConfig] data store.
|
||||
*/
|
||||
val localConfigFlow: Flow<LocalConfig> = localConfigRepository.localConfigFlow
|
||||
|
||||
/**
|
||||
* Clears the [LocalConfig] data in the data store.
|
||||
*/
|
||||
suspend fun clearLocalConfig() {
|
||||
localConfigRepository.clearLocalConfig()
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates [LocalConfig] from each [Config] oneOf.
|
||||
* @param config The [Config] to be set.
|
||||
*/
|
||||
suspend fun setLocalConfig(config: Config) {
|
||||
localConfigRepository.setLocalConfig(config)
|
||||
if (config.hasLora()) channelSetRepository.setLoraConfig(config.lora)
|
||||
}
|
||||
|
||||
/**
|
||||
* Flow representing the [LocalModuleConfig] data store.
|
||||
*/
|
||||
val moduleConfigFlow: Flow<LocalModuleConfig> = moduleConfigRepository.moduleConfigFlow
|
||||
|
||||
/**
|
||||
* Clears the [LocalModuleConfig] data in the data store.
|
||||
*/
|
||||
suspend fun clearLocalModuleConfig() {
|
||||
moduleConfigRepository.clearLocalModuleConfig()
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates [LocalModuleConfig] from each [ModuleConfig] oneOf.
|
||||
* @param config The [ModuleConfig] to be set.
|
||||
*/
|
||||
suspend fun setLocalModuleConfig(config: ModuleConfig) {
|
||||
moduleConfigRepository.setLocalModuleConfig(config)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue