Skip to content

Commit

Permalink
#191: Added a check to only show observation errors on days when day …
Browse files Browse the repository at this point in the history
…occur
  • Loading branch information
janoliver20 committed Jul 25, 2024
1 parent 39b541f commit 32c660f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import io.redlink.more.more_app_mutliplatform.extensions.areAllNamesIn
import io.redlink.more.more_app_mutliplatform.extensions.asClosure
import io.redlink.more.more_app_mutliplatform.extensions.asMappedFlow
import io.redlink.more.more_app_mutliplatform.extensions.firstAsFlow
import io.redlink.more.more_app_mutliplatform.extensions.localDateTime
import io.redlink.more.more_app_mutliplatform.extensions.toLocalDate
import io.redlink.more.more_app_mutliplatform.models.ScheduleState
import io.redlink.more.more_app_mutliplatform.observations.DataRecorder
import io.redlink.more.more_app_mutliplatform.observations.ObservationFactory
import io.redlink.more.more_app_mutliplatform.observations.observationTypes.ObservationType
import io.redlink.more.more_app_mutliplatform.services.bluetooth.BluetoothDeviceManager
import io.redlink.more.more_app_mutliplatform.util.StudyScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.transform
import kotlinx.datetime.Clock
import org.mongodb.kbson.ObjectId
Expand Down Expand Up @@ -66,6 +70,19 @@ class ScheduleRepository : Repository<ScheduleSchema>() {
} ?: emptyFlow()
}

fun allSchedulesToday(observationType: ObservationType): Flow<List<ScheduleSchema>> {
val today = Clock.System.now().localDateTime().date
return realm()?.query<ScheduleSchema>(
"observationType = $0",
observationType.observationType
)
?.asMappedFlow()?.transform { list ->
emit(list.filter {
!it.getState().completed()
&& (it.start?.toLocalDate() == today || it.end?.toLocalDate() == today)
})
} ?: flow { emit(emptyList()) }
}

fun firstScheduleIdAvailableForObservationId(observationId: String): Flow<String?> =
firstScheduleAvailableForObservationId(observationId).transform { it?.scheduleId?.toHexString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
package io.redlink.more.more_app_mutliplatform.extensions

import io.realm.kotlin.query.RealmQuery
import io.realm.kotlin.types.RealmInstant
import io.realm.kotlin.types.TypedRealmObject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.transform
import kotlinx.datetime.LocalDate

fun <T: TypedRealmObject> RealmQuery<T>.asMappedFlow(): Flow<List<T>> {
fun <T : TypedRealmObject> RealmQuery<T>.asMappedFlow(): Flow<List<T>> {
return asFlow().transform { emit(it.list) }
}

fun <T: TypedRealmObject> RealmQuery<T>.firstAsFlow(): Flow<T?> {
fun <T : TypedRealmObject> RealmQuery<T>.firstAsFlow(): Flow<T?> {
return first().asFlow().transform { emit(it.obj) }
}
}

fun RealmInstant.toLocalDate(): LocalDate = this.toInstant().localDateTime().date
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext
import kotlinx.datetime.Clock
Expand Down Expand Up @@ -146,16 +147,26 @@ abstract class Observation(val observationType: ObservationType) {
fun observerAccessible(): Boolean {
val errors = observerErrors()
Napier.d(tag = "Observation::observerAccessible") { errors.toString() }
this._observationErrors.update { Pair(observationType.observationType, errors) }
updateObservationErrors()
return errors.isEmpty()
}

protected open fun observerErrors(): Set<String> = emptySet()

fun updateObservationErrors() {
StudyScope.launch(Dispatchers.IO) {
Napier.d(tag = "Observation::updateObservationErrors") { "ObservationErrors for ${observationType.observationType}" }
_observationErrors.update { Pair(observationType.observationType, observerErrors()) }
scheduleRepository.allSchedulesToday(observationType).firstOrNull()?.let {
if (it.isNotEmpty()) {
Napier.d(tag = "Observation::updateObservationErrors") { "ObservationErrors for ${observationType.observationType}" }

_observationErrors.update {
Pair(
observationType.observationType,
observerErrors()
)
}
}
}
}
}

Expand Down

0 comments on commit 32c660f

Please sign in to comment.