Skip to content

Commit

Permalink
[ANDROAPP-5800] Implement QR and Barcode scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmr committed Feb 14, 2024
1 parent bec2f4a commit 8ad122c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,6 @@ private void initSearchParameters() {
.show(getSupportFragmentManager(), label);
return Unit.INSTANCE;
},
(uid, optionSet, renderingType) -> {

// TODO
return Unit.INSTANCE;
},
() -> {
presenter.onClearClick();
return Unit.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dhis2.usescases.searchTrackEntity.searchparameters

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -26,14 +27,17 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.journeyapps.barcodescanner.ScanOptions
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.dhis2.R
import org.dhis2.commons.Constants
import org.dhis2.commons.orgunitselector.OrgUnitSelectorScope
import org.dhis2.commons.resources.ColorUtils
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.form.data.scan.ScanContract
import org.dhis2.form.model.FieldUiModel
import org.dhis2.form.model.FieldUiModelImpl
import org.dhis2.form.model.UiRenderType
import org.dhis2.form.ui.event.RecyclerViewUiEvents
import org.dhis2.form.ui.intent.FormIntent
import org.dhis2.usescases.searchTrackEntity.SearchTEIViewModel
Expand All @@ -57,18 +61,27 @@ fun SearchParametersScreen(
orgUnitScope: OrgUnitSelectorScope,
label: String,
) -> Unit,
onScanQRCode: (
uid: String,
optionSet: String?,
renderingType: UiRenderType?,
) -> Unit,
onSearchClick: () -> Unit,
onClear: () -> Unit,
) {
val scaffoldState = rememberScaffoldState()
val snackBarHostState = scaffoldState.snackbarHostState
val coroutineScope = rememberCoroutineScope()
val focusManager = LocalFocusManager.current

val qrScanLauncher = rememberLauncherForActivityResult(
contract = ScanContract(),
) { result ->
result.contents?.let { qrData ->
val intent = FormIntent.OnSave(
result.originalIntent.getStringExtra(Constants.UID)!!,
qrData,
ValueType.TEXT,
)
intentHandler(intent)
}
}

val callback = remember {
object : FieldUiModel.Callback {
override fun intent(intent: FormIntent) {
Expand All @@ -86,10 +99,21 @@ fun SearchParametersScreen(
)

is RecyclerViewUiEvents.ScanQRCode -> {
onScanQRCode(
uiEvent.uid,
uiEvent.optionSet,
uiEvent.renderingType,
qrScanLauncher.launch(
ScanOptions().apply {
setDesiredBarcodeFormats()
setPrompt("")
setBeepEnabled(true)
setBarcodeImageEnabled(false)
addExtra(Constants.UID, uiEvent.uid)
uiEvent.optionSet?.let {
addExtra(
Constants.OPTION_SET,
uiEvent.optionSet,
)
}
addExtra(Constants.SCAN_RENDERING_TYPE, uiEvent.renderingType)
},
)
}

Expand Down Expand Up @@ -158,11 +182,11 @@ fun SearchParametersScreen(
modifier = Modifier
.align(Alignment.CenterHorizontally),
style = ButtonStyle.TEXT,
text = "Clear search",
text = resourceManager.getString(R.string.clear_search),
icon = {
Icon(
imageVector = Icons.Outlined.Cancel,
contentDescription = "Clear search",
contentDescription = resourceManager.getString(R.string.clear_search),
tint = SurfaceColor.Primary,
)
},
Expand All @@ -178,7 +202,7 @@ fun SearchParametersScreen(
.padding(16.dp, 8.dp, 16.dp, 8.dp)
.testTag("SEARCH_BUTTON"),
style = ButtonStyle.FILLED,
text = "Search",
text = resourceManager.getString(R.string.search),
) {
onSearchClick()
}
Expand Down Expand Up @@ -213,7 +237,6 @@ fun SearchFormPreview() {
),
intentHandler = {},
onShowOrgUnit = { _, _, _, _ -> },
onScanQRCode = { _, _, _ -> },
onSearchClick = {},
onClear = {},
)
Expand All @@ -231,11 +254,6 @@ fun initSearchScreen(
orgUnitScope: OrgUnitSelectorScope,
label: String,
) -> Unit,
onScanQRCode: (
uid: String,
optionSet: String?,
renderingType: UiRenderType?,
) -> Unit,
onClear: () -> Unit,
) {
viewModel.fetchSearchParameters(
Expand All @@ -249,7 +267,6 @@ fun initSearchScreen(
onSearchClick = viewModel::onSearchClick,
intentHandler = viewModel::onParameterIntent,
onShowOrgUnit = onShowOrgUnit,
onScanQRCode = onScanQRCode,
onClear = {
onClear()
viewModel.clearQueryData()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,5 @@
<string name="delete_tei_dialog_message">This %s and all its data across all programs will be deleted. This action cannot be undone.</string>
<string name="remove_enrollment_dialog_title">Remove from %s?</string>
<string name="remove_enrollment_dialog_message">Data from %s will de deleted. This action cannot be undone.</string>
<string name="clear_search">\"Clear search\"</string>
</resources>

0 comments on commit 8ad122c

Please sign in to comment.