Skip to content

Commit

Permalink
Use flowOn operator with questionnaireStateFlow listener in 'validate…
Browse files Browse the repository at this point in the history
…CurrentPageItems'
  • Loading branch information
LZRS committed Nov 27, 2024
1 parent 88466ba commit 5ed5f95
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ import com.google.android.fhir.datacapture.validation.Valid
import com.google.android.fhir.datacapture.validation.ValidationResult
import com.google.android.fhir.datacapture.views.QuestionTextConfiguration
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.withIndex
import kotlinx.coroutines.launch
Expand All @@ -82,6 +87,8 @@ import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComp
import org.hl7.fhir.r4.model.Resource
import timber.log.Timber

internal var questionnaireViewModelCoroutineContext = Dispatchers.Default

internal class QuestionnaireViewModel(application: Application, state: SavedStateHandle) :
AndroidViewModel(application) {

Expand Down Expand Up @@ -582,8 +589,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
this.shouldShowCancelButton = showCancelButton
}

/** [QuestionnaireState] to be displayed in the UI. */
internal val questionnaireStateFlow: StateFlow<QuestionnaireState> =
private val _questionnaireStateFlow: Flow<QuestionnaireState> =
combine(modificationCount, currentPageIndexFlow, isInReviewModeFlow) { _, _, _ ->
getQuestionnaireState()
}
Expand All @@ -595,16 +601,20 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}
}
.map { it.value }
.stateIn(
viewModelScope,
SharingStarted.Lazily,
initialValue =
QuestionnaireState(
items = emptyList(),
displayMode = DisplayMode.InitMode,
bottomNavItem = null,
),
)
.flowOn(questionnaireViewModelCoroutineContext)

/** [QuestionnaireState] to be displayed in the UI. */
internal val questionnaireStateFlow: StateFlow<QuestionnaireState> =
_questionnaireStateFlow.stateIn(
viewModelScope,
SharingStarted.Lazily,
initialValue =
QuestionnaireState(
items = emptyList(),
displayMode = DisplayMode.InitMode,
bottomNavItem = null,
),
)

/** Travers all [calculatedExpression] within a [Questionnaire] and evaluate them. */
private suspend fun initializeCalculatedExpressions() {
Expand Down Expand Up @@ -1112,6 +1122,16 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
* are all [Valid].
*/
private fun validateCurrentPageItems(block: () -> Unit) {
val checkAllValid = {
if (
currentPageItems.filterIsInstance<QuestionnaireAdapterItem.Question>().all {
it.item.validationResult is Valid
}
) {
block()
}
}

if (
currentPageItems.filterIsInstance<QuestionnaireAdapterItem.Question>().any {
it.item.validationResult is NotValidated
Expand All @@ -1123,16 +1143,16 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
// Results in a new questionnaire state being generated synchronously, i.e., the current
// thread will be suspended until the new state is generated.
modificationCount.update { it + 1 }
forceValidation = false
}

if (
currentPageItems.filterIsInstance<QuestionnaireAdapterItem.Question>().all {
it.item.validationResult is Valid
viewModelScope.launch {
_questionnaireStateFlow.take(1).collectLatest {
forceValidation = false
checkAllValid()
}
}
) {
block()
}

checkAllValid()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class QuestionnaireViewModelTest {

@Before
fun setUp() {
questionnaireViewModelCoroutineContext = mainDispatcherRule.testDispatcher

state = SavedStateHandle()
check(
ApplicationProvider.getApplicationContext<DataCaptureTestApplication>()
Expand Down

0 comments on commit 5ed5f95

Please sign in to comment.