Store last selected configuration for UART profile

This commit is contained in:
Sylwester Zieliński
2022-03-22 15:25:31 +01:00
parent 8e72402b2f
commit 30d7375c6d
3 changed files with 58 additions and 2 deletions

View File

@@ -0,0 +1,36 @@
package no.nordicsemi.android.uart.data
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton
private const val FILE = "UART_CONFIGURATION"
private const val LAST_CONFIGURATION_KEY = "LAST_CONFIGURATION"
@Singleton
internal class ConfigurationDataSource @Inject constructor(
@ApplicationContext
private val context: Context
) {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = FILE)
private val LAST_CONFIGURATION = stringPreferencesKey(LAST_CONFIGURATION_KEY)
val lastConfigurationName = context.dataStore.data.map {
it[LAST_CONFIGURATION]
}
suspend fun saveConfigurationName(name: String) {
context.dataStore.edit {
it[LAST_CONFIGURATION] = name
}
}
}

View File

@@ -17,10 +17,11 @@ import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class UARTRepository @Inject constructor(
class UARTRepository @Inject internal constructor(
@ApplicationContext
private val context: Context,
private val serviceManager: ServiceManager,
private val configurationDataSource: ConfigurationDataSource
) {
private var manager: UARTManager? = null
@@ -30,6 +31,8 @@ class UARTRepository @Inject constructor(
val isRunning = data.map { it.isRunning() }
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
val lastConfigurationName = configurationDataSource.lastConfigurationName
fun launch(device: BluetoothDevice) {
serviceManager.startService(UARTService::class.java, device)
}
@@ -57,6 +60,10 @@ class UARTRepository @Inject constructor(
manager?.clearItems()
}
suspend fun saveConfigurationName(name: String) {
configurationDataSource.saveConfigurationName(name)
}
private suspend fun UARTManager.start(device: BluetoothDevice) {
try {
connect(device)

View File

@@ -42,6 +42,12 @@ internal class UARTViewModel @Inject constructor(
dataSource.getConfigurations().onEach {
_state.value = _state.value.copy(configurations = it)
}.launchIn(viewModelScope)
repository.lastConfigurationName.onEach {
it?.let {
_state.value = _state.value.copy(selectedConfigurationName = it)
}
}.launchIn(viewModelScope)
}
private fun requestBluetoothDevice() {
@@ -88,6 +94,7 @@ internal class UARTViewModel @Inject constructor(
dataSource.saveConfiguration(UARTConfiguration(null, event.name))
_state.value = _state.value.copy(selectedConfigurationName = event.name)
}
saveLastConfigurationName(event.name)
}
private fun onEditMacro(event: OnEditMacro) {
@@ -99,7 +106,13 @@ internal class UARTViewModel @Inject constructor(
}
private fun onConfigurationSelected(event: OnConfigurationSelected) {
_state.value = _state.value.copy(selectedConfigurationName = event.configuration.name)
saveLastConfigurationName(event.configuration.name)
}
private fun saveLastConfigurationName(name: String) {
viewModelScope.launch {
repository.saveConfigurationName(name)
}
}
private fun addNewMacro(macro: UARTMacro) {