Skip to content

Commit

Permalink
reloads forms when event details fields are updated when creating a n…
Browse files Browse the repository at this point in the history
…ew event
  • Loading branch information
ferdyrod committed Jun 24, 2024
1 parent d72f6b6 commit d065e3f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.dhis2.commons.Constants.EVENT_MODE;
import static org.dhis2.commons.extensions.ViewExtensionsKt.closeKeyboard;
import static org.dhis2.form.data.EventRepository.EVENT_ORG_UNIT_UID;
import static org.dhis2.usescases.eventsWithoutRegistration.eventCapture.ui.NonEditableReasonBlockKt.showNonEditableReasonMessage;
import static org.dhis2.utils.granularsync.SyncStatusDialogNavigatorKt.OPEN_ERROR_LOCATION;

Expand All @@ -22,13 +23,15 @@
import org.dhis2.commons.featureconfig.data.FeatureConfigRepository;
import org.dhis2.commons.featureconfig.model.Feature;
import org.dhis2.databinding.SectionSelectorFragmentBinding;
import org.dhis2.form.model.ActionType;
import org.dhis2.form.model.EventMode;
import org.dhis2.form.model.EventRecords;
import org.dhis2.form.ui.FormView;
import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureAction;
import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureActivity;
import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureContract;
import org.dhis2.usescases.general.FragmentGlobalAbstract;
import org.hisp.dhis.android.core.common.ValueType;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
Expand Down Expand Up @@ -78,6 +81,13 @@ public void onAttach(@NotNull Context context) {
public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
String eventUid = getArguments().getString(Constants.EVENT_UID, "");
EventMode eventMode = EventMode.valueOf(getArguments().getString(EVENT_MODE));
loadForm(eventUid, eventMode);

activity.setFormEditionListener(this);
super.onCreate(savedInstanceState);
}

private void loadForm(String eventUid, EventMode eventMode) {
formView = new FormView.Builder()
.locationProvider(locationProvider)
.onLoadingListener(loading -> {
Expand All @@ -87,6 +97,12 @@ public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedI
activity.hideProgress();
}
return Unit.INSTANCE;
}).onItemChangeListener( action -> {
if(action.isEventDetailsRow()){
presenter.showOrHideSaveButton();
}
return Unit.INSTANCE;

})
.onFocused(() -> {
activity.hideNavigationBar();
Expand All @@ -107,8 +123,6 @@ public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedI
featureConfig.isFeatureEnable(Feature.COMPOSE_FORMS)
)
.build();
activity.setFormEditionListener(this);
super.onCreate(savedInstanceState);
}

@Nullable
Expand Down Expand Up @@ -190,6 +204,8 @@ public void onReopen() {

@Override
public void showNonEditableMessage(@NonNull String reason, boolean canBeReOpened) {
binding.editableReasonContainer.setVisibility(View.VISIBLE);

showNonEditableReasonMessage(
binding.editableReasonContainer,
reason,
Expand All @@ -203,6 +219,6 @@ public void showNonEditableMessage(@NonNull String reason, boolean canBeReOpened

@Override
public void hideNonEditableMessage() {
binding.editableReasonContainer.removeAllViews();
binding.editableReasonContainer.setVisibility(View.GONE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class EventCaptureFormPresenter(
when (isEditable) {
is EventEditableStatus.Editable -> {
view.showSaveButton()
view.hideNonEditableMessage()
}

is EventEditableStatus.NonEditable -> {
Expand Down
16 changes: 7 additions & 9 deletions form/src/main/java/org/dhis2/form/data/EventRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ class EventRepository(
private val eventMode: EventMode,
) : DataEntryBaseRepository(FormBaseConfiguration(d2), fieldFactory) {

private val event by lazy {
d2.eventModule().events().uid(eventUid)
.blockingGet()
}
private var event = d2.eventModule().events().uid(eventUid).blockingGet()

private val programStage by lazy {
d2.programModule()
Expand Down Expand Up @@ -139,7 +136,7 @@ class EventRepository(

override fun list(): Flowable<List<FieldUiModel>> {
return d2.programModule().programStageSections()
.byProgramStageUid().eq(event?.programStage())
.byProgramStageUid().eq(programStage?.uid())
.withDataElements()
.get()
.flatMap { programStageSection ->
Expand Down Expand Up @@ -167,7 +164,7 @@ class EventRepository(
sectionUid = EVENT_DATA_SECTION_UID,
sectionName = resources.formatWithEventLabel(
stringResource = R.string.event_data_section_title,
programStageUid = event?.programStage(),
programStageUid = programStage?.uid(),
),
description = null,
isOpen = true,
Expand All @@ -183,6 +180,7 @@ class EventRepository(
}

private fun getEventDetails(): MutableList<FieldUiModel> {
event = d2.eventModule().events().uid(eventUid).blockingGet()
val eventDataItems = mutableListOf<FieldUiModel>()
eventDataItems.apply {
add(createEventDetailsSection())
Expand Down Expand Up @@ -459,7 +457,7 @@ class EventRepository(
sectionUid = EVENT_DETAILS_SECTION_UID,
sectionName = resources.formatWithEventLabel(
stringResource = R.string.event_details_section_title,
programStageUid = event?.programStage(),
programStageUid = programStage?.uid(),
),
description = programStage?.description(),
isOpen = false,
Expand Down Expand Up @@ -487,7 +485,7 @@ class EventRepository(
return Single.fromCallable {
val stageDataElements =
d2.programModule().programStageDataElements().withRenderType()
.byProgramStage().eq(event?.programStage())
.byProgramStage().eq(programStage?.uid())
.orderBySortOrder(RepositoryScope.OrderByDirection.ASC)
.blockingGet()

Expand All @@ -510,7 +508,7 @@ class EventRepository(
)
programStageSection.dataElements()?.forEach { dataElement ->
d2.programModule().programStageDataElements().withRenderType()
.byProgramStage().eq(event?.programStage())
.byProgramStage().eq(programStage?.uid())
.byDataElement().eq(dataElement.uid())
.one().blockingGet()?.let {
fields.add(
Expand Down
1 change: 1 addition & 0 deletions form/src/main/java/org/dhis2/form/model/RowAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ data class RowAction(
val error: Throwable? = null,
val type: ActionType,
val valueType: ValueType? = null,
val isEventDetailsRow: Boolean = false,
)
15 changes: 14 additions & 1 deletion form/src/main/java/org/dhis2/form/ui/FormViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.dhis2.commons.prefs.PreferenceProvider
import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.form.R
import org.dhis2.form.data.DataIntegrityCheckResult
import org.dhis2.form.data.EventRepository.Companion.EVENT_COORDINATE_UID
import org.dhis2.form.data.EventRepository.Companion.EVENT_ORG_UNIT_UID
import org.dhis2.form.data.EventRepository.Companion.EVENT_REPORT_DATE_UID
import org.dhis2.form.data.FormRepository
import org.dhis2.form.data.GeometryController
import org.dhis2.form.data.GeometryParserImpl
Expand Down Expand Up @@ -204,7 +207,11 @@ class FormViewModel(
} else {
val saveResult = repository.save(action.id, action.value, action.extraData)
if (saveResult?.valueStoreResult != ValueStoreResult.ERROR_UPDATING_VALUE) {
repository.updateValueOnList(action.id, action.value, action.valueType)
if (action.isEventDetailsRow) {
repository.fetchFormItems(openErrorLocation)
} else {
repository.updateValueOnList(action.id, action.value, action.valueType)
}
} else {
repository.updateErrorList(
action.copy(
Expand Down Expand Up @@ -630,8 +637,14 @@ class FormViewModel(
error = error,
type = actionType,
valueType = valueType,
isEventDetailsRow = isEventDetailField(uid),
)

private fun isEventDetailField(uid: String): Boolean {
val eventDetailsIds = listOf(EVENT_REPORT_DATE_UID, EVENT_ORG_UNIT_UID, EVENT_COORDINATE_UID)
return eventDetailsIds.contains(uid)
}

fun onItemsRendered() {
loading.value = false
}
Expand Down

0 comments on commit d065e3f

Please sign in to comment.