Skip to content

Commit

Permalink
Open TEI dashboard when a TEI is found after QR code scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Feb 28, 2024
1 parent d7ebf90 commit 01f3fad
Showing 1 changed file with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,7 @@ class SearchTEIViewModel(
}

is FormIntent.OnQrCodeScanned -> {
// TODO: Search based on QR code or update query
updateQuery(
formIntent.uid,
formIntent.value
)
onQrCodeScanned(formIntent)
}

is FormIntent.OnFocus -> {
Expand Down Expand Up @@ -801,6 +797,58 @@ class SearchTEIViewModel(
}
}

private fun onQrCodeScanned(formIntent: FormIntent.OnQrCodeScanned) {
viewModelScope.launch {
updateQuery(
formIntent.uid,
formIntent.value,
)

searching = queryData.isNotEmpty()
uiState = uiState.copy(clearSearchEnabled = queryData.isNotEmpty())

val searchParametersModel = SearchParametersModel(
selectedProgram = searchRepository.getProgram(initialProgramUid),
queryData = queryData,
)
val isOnline = searching && networkUtils.isOnline()
val trackedEntities = withContext(dispatchers.io()) {
searchRepositoryKt.searchTrackedEntitiesImmediate(
searchParametersModel = searchParametersModel,
isOnline = isOnline,
)
}

if (trackedEntities.isEmpty() || trackedEntities.size > 1) return@launch

val tei = trackedEntities.first()
val searchTeiModel = withContext(dispatchers.io()) {
searchRepository.transform(
/* searchItem = */
tei,
/* selectedProgram = */
searchParametersModel.selectedProgram,
/* offlineOnly = */
!(isOnline && FilterManager.getInstance().stateFilters.isEmpty()),
/* sortingItem = */
FilterManager.getInstance().sortingItem,
)
}

// Open TEI dashboard for the found TEI
onTeiClick(
teiUid = searchTeiModel.uid(),
enrollmentUid = searchTeiModel.selectedEnrollment.uid(),
online = searchTeiModel.isOnline,
)

searching = false

clearQueryData()
clearFocus()
}
}

fun clearFocus() {
val updatedItems = uiState.items.map {
if (it.focused) {
Expand Down

0 comments on commit 01f3fad

Please sign in to comment.