Skip to content

Commit

Permalink
Make active survey reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Jan 3, 2024
1 parent 250f907 commit 85f4892
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ constructor(
return
}

surveyRepository.activeSurvey =
surveyRepository.getOfflineSurvey(surveyId)
?: makeSurveyAvailableOffline(surveyId) ?: error("Survey $surveyId not found in remote db")
surveyRepository.getOfflineSurvey(surveyId)
?: makeSurveyAvailableOffline(surveyId) ?: error("Survey $surveyId not found in remote db")

surveyRepository.selectedSurveyId = surveyId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ interface SurveyDao : BaseDao<SurveyEntity> {
@Transaction
@Query("SELECT * FROM survey WHERE id = :id")
suspend fun findSurveyById(id: String): SurveyEntityAndRelations?

@Transaction
@Query("SELECT * FROM survey WHERE id = :id")
fun survey(id: String): Flow<SurveyEntityAndRelations?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class RoomSurveyStore @Inject internal constructor() : LocalSurveyStore {
override val surveys: Flow<List<Survey>>
get() = surveyDao.getAll().map { surveyEntities -> surveyEntities.map { it.toModelObject() } }

override fun survey(id: String): Flow<Survey?> = surveyDao.survey(id).map { it?.toModelObject() }

/**
* Attempts to update persisted data associated with a [Survey] in the local database. If the
* provided survey does not exist, inserts the given survey into the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface LocalSurveyStore {
/** Load surveys stored in local database. */
val surveys: Flow<List<Survey>>

fun survey(id: String): Flow<Survey?>

/** Load last active survey, if any. */
suspend fun getSurveyById(id: String): Survey?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withTimeoutOrNull
import timber.log.Timber

Expand All @@ -57,22 +58,27 @@ constructor(
private val networkManager: NetworkManager,
@ApplicationScope private val externalScope: CoroutineScope
) {
private val _activeSurvey = MutableStateFlow<Survey?>(null)
private val _selectedSurveyIdFlow = MutableStateFlow<String?>(null)
var selectedSurveyId: String?
get() = _selectedSurveyIdFlow.value
set(value) {
_selectedSurveyIdFlow.value = value
}

val activeSurveyFlow: SharedFlow<Survey?> =
_activeSurvey.shareIn(externalScope, replay = 1, started = SharingStarted.Eagerly)
@OptIn(ExperimentalCoroutinesApi::class)
val activeSurveyFlow: StateFlow<Survey?> =
_selectedSurveyIdFlow
.flatMapLatest { id -> offlineSurvey(id) }
.stateIn(externalScope, SharingStarted.Lazily, null)

/**
* The currently active survey, or `null` if no survey is active. Updating this property causes
* [lastActiveSurveyId] to be updated with the id of the specified survey, or `""` if the
* specified survey is `null`.
*/
var activeSurvey: Survey?
get() = _activeSurvey.value
set(value) {
_activeSurvey.value = value
lastActiveSurveyId = value?.id ?: ""
}
var activeSurvey: Survey? = null
get() = activeSurveyFlow.value
private set

val localSurveyListFlow: Flow<List<SurveyListItem>>
get() = localSurveyStore.surveys.map { list -> list.map { it.toListItem(true) } }
Expand All @@ -89,6 +95,9 @@ constructor(
*/
suspend fun getOfflineSurvey(surveyId: String): Survey? = localSurveyStore.getSurveyById(surveyId)

private fun offlineSurvey(id: String?): Flow<Survey?> =
if (id == null) flowOf(null) else localSurveyStore.survey(id)

/**
* Loads the survey with the specified id from remote and writes to local db. If the survey isn't
* found or operation times out, then we return null and don't fetch the survey from local db.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -107,7 +107,8 @@ internal constructor(
// TODO: Since we depend on survey stream from repo anyway, this transformation can be moved
// into the repository.

val activeSurvey = surveyRepository.activeSurveyFlow.distinctUntilChanged()
val activeSurvey = surveyRepository.activeSurveyFlow
viewModelScope.launch { activeSurvey.collect { Timber.e("Updated survey: $it") } }

mapLoiFeatures =
activeSurvey.flatMapLatest {
Expand All @@ -126,7 +127,9 @@ internal constructor(
else loiRepository.getWithinBounds(survey, bounds)
}
.stateIn(viewModelScope, SharingStarted.Lazily, listOf())

viewModelScope.launch {
loisInViewport.collect { Timber.e("Updated loisInViewport: ${it.firstOrNull()}") }
}
adHocLoiJobs =
activeSurvey
.combine(isZoomedInFlow) { survey, isZoomedIn -> Pair(survey, isZoomedIn) }
Expand All @@ -136,6 +139,9 @@ internal constructor(
else survey.jobs.filter { it.canDataCollectorsAddLois }
)
}
viewModelScope.launch {
adHocLoiJobs.collect { Timber.e("Updated adHocLoiJobs: $adHocLoiJobs") }
}
}

override fun onMapCameraMoved(newCameraPosition: CameraPosition) {
Expand Down

0 comments on commit 85f4892

Please sign in to comment.