Skip to content

Commit

Permalink
Not sending concrete data values unless it is no longer loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
HLCaptain committed Nov 18, 2023
1 parent 6abb76c commit 2b47155
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SensorFirestoreDataSource(
private val firestore: FirebaseFirestore,
) : SensorNetworkDataSource {
override fun fetch(uuid: String): Flow<FirestoreSensor> {
Napier.d("Fetching sensors")
Napier.d("Fetching sensor $uuid")
return firestore
.collection(FirestoreSensor.COLLECTION_NAME)
.document(uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PlantManager(
sensorRepository.getSensor(sensorUUID).firstOrNull()
} ?: emptyList()
Napier.d("Plant is $plant")
plant?.let { send(plant.toDomainModel(sensors.filterNotNull())) }
send(plant?.toDomainModel(sensors.filterNotNull()))
}
}.flowOn(dispatcherIO)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SensorManager(

fun getSensorsForPlant(plantUUID: String) = plantManager
.getPlant(plantUUID)
.map { it.sensors }
.map { it?.sensors }

suspend fun unassignSensorFromPlant(sensorUUID: String, plantUUID: String) {
plantRepository.getPlant(plantUUID).firstOrNull()?.let { plant ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package nest.planty.repository

import io.github.aakira.napier.Napier
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.dropWhile
import kotlinx.coroutines.flow.map
import nest.planty.data.store.BrokerMutableStoreBuilder
import nest.planty.data.store.BrokersMutableStoreBuilder
import nest.planty.di.NamedCoroutineDispatcherIO
import nest.planty.domain.model.DomainBroker
import org.koin.core.annotation.Factory
import org.mobilenativefoundation.store.store5.ExperimentalStoreApi
Expand All @@ -18,7 +16,6 @@ import org.mobilenativefoundation.store.store5.StoreWriteRequest
class BrokerRepository(
brokerMutableStoreBuilder: BrokerMutableStoreBuilder,
brokersMutableStoreBuilder: BrokersMutableStoreBuilder,
@NamedCoroutineDispatcherIO private val dispatcherIO: CoroutineDispatcher
) {
@OptIn(ExperimentalStoreApi::class)
private val brokerMutableStore = brokerMutableStoreBuilder.store
Expand All @@ -27,32 +24,30 @@ class BrokerRepository(

@OptIn(ExperimentalStoreApi::class)
fun getBroker(uuid: String) = brokerMutableStore.stream<StoreReadResponse<DomainBroker>>(
StoreReadRequest.cached(
key = uuid,
refresh = true
)
).map {
StoreReadRequest.fresh(key = uuid)
).dropWhile {
it is StoreReadResponse.Loading
}.map {
it.throwIfError()
Napier.d("Read Response: $it")
val data = it.dataOrNull()
Napier.d("Broker is $data")
data
}.flowOn(dispatcherIO)
}

@OptIn(ExperimentalStoreApi::class)
fun getBrokersByUser(userUUID: String) =
brokersMutableStore.stream<StoreReadResponse<List<DomainBroker>>>(
StoreReadRequest.cached(
key = userUUID,
refresh = true
)
).map {
StoreReadRequest.fresh(key = userUUID)
).dropWhile {
it is StoreReadResponse.Loading
}.map {
it.throwIfError()
Napier.d("Read Response: $it")
val data = it.dataOrNull()
Napier.d("Broker is $data")
data
}.flowOn(dispatcherIO)
}

@OptIn(ExperimentalStoreApi::class)
suspend fun upsertBroker(broker: DomainBroker) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nest.planty.repository

import io.github.aakira.napier.Napier
import kotlinx.coroutines.flow.dropWhile
import kotlinx.coroutines.flow.map
import nest.planty.data.store.PlantMutableStoreBuilder
import nest.planty.data.store.PlantsByUserMutableStoreBuilder
Expand All @@ -22,7 +23,9 @@ class PlantRepository(

fun getPlant(uuid: String) = plantMutableStore.stream<StoreReadResponse<Plant>>(
StoreReadRequest.fresh(key = uuid)
).map {
).dropWhile {
it is StoreReadResponse.Loading
}.map {
it.throwIfError()
Napier.d("Read Response: $it")
val data = it.dataOrNull()
Expand Down Expand Up @@ -50,7 +53,9 @@ class PlantRepository(
fun getPlantsByUser(userUUID: String) =
plantsMutableStore.stream<StoreReadResponse<List<Plant>>>(
StoreReadRequest.fresh(key = userUUID)
).map {
).dropWhile {
it is StoreReadResponse.Loading
}.map {
it.throwIfError()
Napier.d("Read Response: $it")
val data = it.dataOrNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nest.planty.repository

import io.github.aakira.napier.Napier
import kotlinx.coroutines.flow.dropWhile
import kotlinx.coroutines.flow.map
import nest.planty.data.store.SensorMutableStoreBuilder
import nest.planty.data.store.SensorsMutableStoreBuilder
Expand All @@ -23,7 +24,9 @@ class SensorRepository(
@OptIn(ExperimentalStoreApi::class)
fun getSensor(uuid: String) = sensorMutableStore.stream<StoreReadResponse<DomainSensor>>(
StoreReadRequest.fresh(key = uuid)
).map {
).dropWhile {
it is StoreReadResponse.Loading
}.map {
it.throwIfError()
Napier.d("Read Response: $it")
val data = it.dataOrNull()
Expand All @@ -34,11 +37,10 @@ class SensorRepository(
@OptIn(ExperimentalStoreApi::class)
fun getSensorsByBroker(brokerUUID: String) =
sensorsMutableStore.stream<StoreReadResponse<List<DomainSensor>>>(
StoreReadRequest.cached(
key = brokerUUID,
refresh = true
)
).map {
StoreReadRequest.fresh(key = brokerUUID)
).dropWhile {
it is StoreReadResponse.Loading
}.map {
it.throwIfError()
Napier.d("Read Response: $it")
val data = it.dataOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PlantSensorEditScreenModel(
@NamedCoroutineDispatcherIO private val dispatcherIO: CoroutineDispatcher,
) : ScreenModel {
val assignedSensors = sensorManager.getSensorsForPlant(plantUUID)
.map { sensors -> sensors.groupBy { it.ownerBroker } }
.map { sensors -> sensors?.groupBy { it.ownerBroker } ?: emptyMap() }
.stateIn(
screenModelScope,
SharingStarted.Eagerly,
Expand Down

0 comments on commit 2b47155

Please sign in to comment.