Skip to content

Commit

Permalink
fix: option set not searching
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Nov 28, 2024
1 parent 143b352 commit 74a9379
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ private FieldUiModel transform(@NonNull ProgramStageDataElement stage, DataEleme
dataValue = option.get(0).displayName();
}
optionSetConfig = new OptionSetConfiguration(
null,
OptionSetConfiguration.Companion.optionDataFlow(
d2.optionModule().options().byOptionSetUid().eq(optionSet).getPagingData(10),
option1 -> metadataIconProvider.invoke(option1.style()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ class SearchRepositoryImplKt(

val optionSetConfiguration = attribute.optionSet()?.let {
OptionSetConfiguration(
d2.optionModule().options()
searchEmitter = null,
optionFlow = d2.optionModule().options()
.byOptionSetUid().eq(attribute.optionSet()!!.uid())
.getPagingData(10)
.map { pagingData ->
Expand Down Expand Up @@ -357,7 +358,8 @@ class SearchRepositoryImplKt(

val optionSetConfiguration = attribute.optionSet()?.let {
OptionSetConfiguration(
d2.optionModule().options()
searchEmitter = null,
optionFlow = d2.optionModule().options()
.byOptionSetUid().eq(attribute.optionSet()!!.uid())
.getPagingData(10)
.map { pagingData ->
Expand Down
49 changes: 33 additions & 16 deletions form/src/main/java/org/dhis2/form/data/DataEntryBaseRepository.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
@file:OptIn(ExperimentalCoroutinesApi::class)

package org.dhis2.form.data

import androidx.compose.ui.graphics.Color
import androidx.paging.PagingData
import androidx.paging.map
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import org.dhis2.commons.resources.MetadataIconProvider
import org.dhis2.form.data.metadata.FormBaseConfiguration
Expand All @@ -13,6 +20,7 @@ import org.dhis2.form.model.SectionUiModelImpl
import org.dhis2.form.ui.FieldViewModelFactory
import org.hisp.dhis.android.core.imports.TrackerImportConflict
import org.hisp.dhis.android.core.program.SectionRenderingType
import timber.log.Timber

abstract class DataEntryBaseRepository(
private val conf: FormBaseConfiguration,
Expand Down Expand Up @@ -69,25 +77,34 @@ abstract class DataEntryBaseRepository(

override fun options(
optionSetUid: String,
query: String,
optionsToHide: List<String>,
optionGroupsToHide: List<String>,
optionGroupsToShow: List<String>,
): Flow<PagingData<OptionSetConfiguration.OptionData>> {
return conf.options(
optionSetUid,
query,
optionsToHide,
optionGroupsToHide,
optionGroupsToShow,
).map { pagingData ->
pagingData.map { option ->
OptionSetConfiguration.OptionData(
option,
metadataIconProvider(option.style(), defaultStyleColor),
)
}
}
): Pair<MutableStateFlow<String>, Flow<PagingData<OptionSetConfiguration.OptionData>>> {
val searchFlow = MutableStateFlow("")
return Pair(
searchFlow,
searchFlow.debounce(300)
.flatMapLatest { query ->
conf.options(
optionSetUid,
query,
optionsToHide,
optionGroupsToHide,
optionGroupsToShow,
).map { pagingData ->
pagingData.map { option ->
OptionSetConfiguration.OptionData(
option,
metadataIconProvider(option.style(), defaultStyleColor),
)
}
}
}
.catch {
Timber.e(it)
},
)
}

override fun dateFormatConfiguration(): String? {
Expand Down
5 changes: 3 additions & 2 deletions form/src/main/java/org/dhis2/form/data/DataEntryRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.dhis2.form.data
import androidx.paging.PagingData
import io.reactivex.Flowable
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import org.dhis2.form.model.EventMode
import org.dhis2.form.model.FieldUiModel
import org.dhis2.form.model.OptionSetConfiguration
Expand Down Expand Up @@ -40,11 +41,11 @@ interface DataEntryRepository {
fun disableCollapsableSections(): Boolean?

fun getSpecificDataEntryItems(uid: String): List<FieldUiModel>

fun options(
optionSetUid: String,
query: String = "",
optionsToHide: List<String>,
optionGroupsToHide: List<String>,
optionGroupsToShow: List<String>,
): Flow<PagingData<OptionSetConfiguration.OptionData>>
): Pair<MutableStateFlow<String>, Flow<PagingData<OptionSetConfiguration.OptionData>>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,15 @@ class EnrollmentRepository(

var optionSetConfig: OptionSetConfiguration? = null
if (!optionSet.isNullOrEmpty()) {
val (searchEmitter, optionFlow) = options(
optionSetUid = optionSet,
optionsToHide = emptyList(),
optionGroupsToHide = emptyList(),
optionGroupsToShow = emptyList(),
)
optionSetConfig = OptionSetConfiguration(
options(optionSet, "", emptyList(), emptyList(), emptyList()),
searchEmitter = searchEmitter,
optionFlow = optionFlow,
)
}

Expand Down
31 changes: 19 additions & 12 deletions form/src/main/java/org/dhis2/form/data/EventRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ class EventRepository(
.withTrackedEntityType()
.byUid().eq(programUid)
.one().blockingGet()?.let { program ->
val firstAvailablePeriodDate = getFirstAvailablePeriod(event?.enrollment(), programStage)
val firstAvailablePeriodDate =
getFirstAvailablePeriod(event?.enrollment(), programStage)
var minDate = dateUtils.expDate(
firstAvailablePeriodDate,
program.expiryDays() ?: 0,
Expand Down Expand Up @@ -478,7 +479,11 @@ class EventRepository(
}
val calendar = DateUtils.getInstance().getCalendarByDate(minEventDate)

return dateUtils.getNextPeriod(programStage?.periodType(), calendar.time ?: event?.eventDate(), if (stageLastDate == null) 0 else 1)
return dateUtils.getNextPeriod(
programStage?.periodType(),
calendar.time ?: event?.eventDate(),
if (stageLastDate == null) 0 else 1,
)
}

private fun getStageLastDate(): Date? {
Expand All @@ -489,12 +494,14 @@ class EventRepository(
.eq(enrollmentUid).byProgramStageUid()
.eq(programStageUid)
.byDeleted().isFalse
.orderByEventDate(RepositoryScope.OrderByDirection.DESC).blockingGet().filter { it.uid() != eventUid }
.orderByEventDate(RepositoryScope.OrderByDirection.DESC).blockingGet()
.filter { it.uid() != eventUid }
val scheduleEvents =
d2.eventModule().events().byEnrollmentUid().eq(enrollmentUid).byProgramStageUid()
.eq(programStageUid)
.byDeleted().isFalse
.orderByDueDate(RepositoryScope.OrderByDirection.DESC).blockingGet().filter { it.uid() != eventUid }
.orderByDueDate(RepositoryScope.OrderByDirection.DESC).blockingGet()
.filter { it.uid() != eventUid }

var activeDate: Date? = null
var scheduleDate: Date? = null
Expand Down Expand Up @@ -633,15 +640,15 @@ class EventRepository(
.byCode()
.eq(dataValue).one().blockingGet()?.displayName()
}

val (searchEmitter, optionFlow) = options(
optionSetUid = optionSet!!,
optionsToHide = emptyList(),
optionGroupsToHide = emptyList(),
optionGroupsToShow = emptyList(),
)
optionSetConfig = OptionSetConfiguration(
optionFlow = options(
optionSetUid = optionSet!!,
"",
emptyList(),
emptyList(),
emptyList(),
),
searchEmitter = searchEmitter,
optionFlow = optionFlow,
)
}
val fieldRendering = getValueTypeDeviceRendering(programStageDataElement)
Expand Down
17 changes: 9 additions & 8 deletions form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -801,18 +801,19 @@ class FormRepositoryImpl(
}

override fun fetchOptions(uid: String, optionSetUid: String) {
val flow = dataEntryRepository.options(
optionSetUid,
query = "",
ruleEffectsResult?.optionsToHide(uid) ?: emptyList(),
ruleEffectsResult?.optionGroupsToHide(uid) ?: emptyList(),
ruleEffectsResult?.optionGroupsToShow(uid) ?: emptyList(),
val (searchEmitter, flow) = dataEntryRepository.options(
optionSetUid = optionSetUid,
optionsToHide = ruleEffectsResult?.optionsToHide(uid) ?: emptyList(),
optionGroupsToHide = ruleEffectsResult?.optionGroupsToHide(uid) ?: emptyList(),
optionGroupsToShow = ruleEffectsResult?.optionGroupsToShow(uid) ?: emptyList(),
)

val newConf = OptionSetConfiguration(flow)
val newConf = OptionSetConfiguration(
searchEmitter = searchEmitter,
optionFlow = flow,
)

itemList.let { list ->

list.find { item ->
item.uid == uid
}?.let { item ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package org.dhis2.form.model
import androidx.paging.PagingData
import androidx.paging.map
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import org.dhis2.ui.MetadataIconData
import org.hisp.dhis.android.core.option.Option

data class OptionSetConfiguration(
val searchEmitter: MutableStateFlow<String>? = null,
val optionFlow: Flow<PagingData<OptionData>>,
) {

companion object {
fun optionDataFlow(
flow: Flow<PagingData<Option>>,
Expand All @@ -26,11 +27,6 @@ data class OptionSetConfiguration(
}
}

data class OptionConfigData(
val options: List<OptionData>,
val metadataIconMap: Map<String, MetadataIconData>,
)

data class OptionData(
val option: Option,
val metadataIconData: MetadataIconData,
Expand Down
1 change: 0 additions & 1 deletion form/src/main/java/org/dhis2/form/ui/intent/FormIntent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ sealed class FormIntent {
val uid: String,
val optionSetUid: String,
val value: String?,
val query: String,
) : FormIntent()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.dhis2.form.ui.provider.inputfield

import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.paging.compose.collectAsLazyPagingItems
import kotlinx.coroutines.launch
import org.dhis2.form.extensions.inputState
import org.dhis2.form.extensions.legend
import org.dhis2.form.extensions.supportingText
Expand All @@ -20,13 +23,28 @@ fun ProvideDropdownInput(
modifier: Modifier,
inputStyle: InputStyle,
fieldUiModel: FieldUiModel,
fetchOptions: (query: String) -> Unit,
fetchOptions: () -> Unit,
) {
var selectedItem by remember(fieldUiModel) {
mutableStateOf(DropdownItem(fieldUiModel.displayName ?: ""))
}

val optionsData = fieldUiModel.optionSetConfiguration?.optionFlow?.collectAsLazyPagingItems()
val optionSetConfiguration by remember(fieldUiModel) {
mutableStateOf(fieldUiModel.optionSetConfiguration)
}

val optionsData = optionSetConfiguration?.optionFlow?.collectAsLazyPagingItems()

val useDropdown by remember {
derivedStateOf {
optionSetConfiguration?.searchEmitter?.value?.isEmpty() == true && (
optionsData?.itemCount
?: 0
) < 15
}
}

val scope = rememberCoroutineScope()

InputDropDown(
modifier = modifier,
Expand All @@ -42,18 +60,19 @@ fun ProvideDropdownInput(
DropdownItem(optionsData?.get(index)?.option?.displayName() ?: "")
},
onSearchOption = { query ->
fetchOptions(query)
scope.launch { fieldUiModel.optionSetConfiguration?.searchEmitter?.emit(query) }
},
itemCount = optionsData?.itemCount ?: 0,
useDropDown = (optionsData?.itemCount ?: 0) < 15,
useDropDown = useDropdown,
onItemSelected = { index, newSelectedItem ->
selectedItem = newSelectedItem
fieldUiModel.onSave(
optionsData?.get(index)?.option?.code(),
)
},
loadOptions = {
fetchOptions("")
loadOptions = fetchOptions,
onDismiss = {
scope.launch { fieldUiModel.optionSetConfiguration?.searchEmitter?.emit("") }
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,12 @@ fun FieldProvider(
fieldUiModel = fieldUiModel,
intentHandler = intentHandler,
context = context,
fetchOptions = { query ->
fetchOptions = {
intentHandler(
FormIntent.FetchOptions(
fieldUiModel.uid,
fieldUiModel.optionSet!!,
value = fieldUiModel.value,
query = query,
),
)
},
Expand Down Expand Up @@ -486,7 +485,7 @@ fun ProvideByOptionSet(
fieldUiModel: FieldUiModel,
intentHandler: (FormIntent) -> Unit,
context: Context,
fetchOptions: (query: String) -> Unit,
fetchOptions: () -> Unit,
) {
when (fieldUiModel.renderingType) {
UiRenderType.HORIZONTAL_RADIOBUTTONS,
Expand Down
Loading

0 comments on commit 74a9379

Please sign in to comment.