Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpeziStorage #108

Merged
merged 24 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import edu.stanford.spezi.modules.storage.di.Storage
import edu.stanford.spezi.modules.storage.key.KeyValueStorage
import edu.stanford.spezi.modules.storage.key.getSerializableList
import edu.stanford.spezi.modules.storage.key.putSerializable
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -23,14 +22,11 @@ internal class BLEPairedDevicesStorage @Inject constructor(
private val bluetoothAdapter: BluetoothAdapter,
private val bleDevicePairingNotifier: BLEDevicePairingNotifier,
@Storage.Encrypted
private val encryptedKeyValueStorage: KeyValueStorage,
private val storage: KeyValueStorage,
private val timeProvider: TimeProvider,
@Dispatching.IO private val ioScope: CoroutineScope,
) {
private val logger by speziLogger()
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, error ->
logger.e(error) { "Error executing paired devices storage operations" }
}

private val _pairedDevices = MutableStateFlow(emptyList<BLEDevice>())
val pairedDevices = _pairedDevices.asStateFlow()
Expand All @@ -40,8 +36,8 @@ internal class BLEPairedDevicesStorage @Inject constructor(
observeUnpairingEvents()
}

fun updateDeviceConnection(device: BluetoothDevice, connected: Boolean) = execute {
if (isPaired(device).not()) return@execute
fun updateDeviceConnection(device: BluetoothDevice, connected: Boolean) {
if (isPaired(device).not()) return
val currentDevices = getCurrentStoredDevices()
currentDevices.removeAll { it.address == device.address }
val newDevice = BLEDevice(
Expand All @@ -54,24 +50,24 @@ internal class BLEPairedDevicesStorage @Inject constructor(
update(devices = currentDevices + newDevice)
}

private fun refreshState() = execute {
val systemBoundDevices = bluetoothAdapter.bondedDevices ?: return@execute
private fun refreshState() {
val systemBoundDevices = bluetoothAdapter.bondedDevices ?: return
val newDevices = getCurrentStoredDevices().filter { storedDevice ->
systemBoundDevices.any { it.address == storedDevice.address }
}
logger.i { "refreshing with $newDevices" }
update(devices = newDevices)
}

fun onStopped() = execute {
fun onStopped() {
val devices = getCurrentStoredDevices().map {
it.copy(connected = false, lastSeenTimeStamp = timeProvider.currentTimeMillis())
}
update(devices = devices)
}

private fun update(devices: List<BLEDevice>) = execute {
encryptedKeyValueStorage.putSerializable(key = KEY, devices)
private fun update(devices: List<BLEDevice>) {
storage.putSerializable(key = KEY, devices)
_pairedDevices.update { devices }
logger.i { "Updating local storage with $devices" }
}
Expand Down Expand Up @@ -112,12 +108,8 @@ internal class BLEPairedDevicesStorage @Inject constructor(
}
}

private suspend fun getCurrentStoredDevices() =
encryptedKeyValueStorage.getSerializableList<BLEDevice>(key = KEY).toMutableList()

private fun execute(block: suspend () -> Unit) {
ioScope.launch(coroutineExceptionHandler) { block() }
}
private fun getCurrentStoredDevices() =
storage.getSerializableList<BLEDevice>(key = KEY).toMutableList()

private companion object {
const val KEY = "paired_ble_devices"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BLEPairedDevicesStorageTest {
private val pairedDevicesStorage by lazy {
BLEPairedDevicesStorage(
bluetoothAdapter = adapter,
encryptedKeyValueStorage = storage,
storage = storage,
ioScope = SpeziTestScope(),
bleDevicePairingNotifier = bleDevicePairingNotifier,
timeProvider = timeProvider,
Expand Down

This file was deleted.

Loading
Loading