Skip to content

Commit

Permalink
fix: [ANDROAPP-6608] Display enrollment form error/warning dialog in …
Browse files Browse the repository at this point in the history
…landscape dashboard (#3865)
  • Loading branch information
Balcan authored Nov 11, 2024
1 parent 219c46a commit cb924d1
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 272 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.dhis2.usescases.enrollment

import org.dhis2.R
import org.dhis2.commons.resources.EventResourcesProvider
import org.dhis2.form.data.EnrollmentRepository
import org.dhis2.form.data.metadata.EnrollmentConfiguration

class DateEditionWarningHandler(
private val conf: EnrollmentConfiguration?,
private val eventResourcesProvider: EventResourcesProvider,
) {
private var hasShownIncidentDateEditionWarning = false
private var hasShownEnrollmentDateEditionWarning = false

fun shouldShowWarning(
fieldUid: String,
showWarning: (message: String) -> Unit,
) {
if (fieldUid == EnrollmentRepository.ENROLLMENT_DATE_UID &&
conf?.hasEventsGeneratedByEnrollmentDate() == true &&
!hasShownEnrollmentDateEditionWarning
) {
hasShownEnrollmentDateEditionWarning = true
showWarning(buildMessage())
} else if (fieldUid == EnrollmentRepository.INCIDENT_DATE_UID &&
conf?.hasEventsGeneratedByIncidentDate() == true &&
!hasShownIncidentDateEditionWarning
) {
hasShownIncidentDateEditionWarning = true
showWarning(buildMessage())
}
}

private fun buildMessage() = eventResourcesProvider.formatWithProgramEventLabel(
R.string.enrollment_date_edition_warning_event_label,
conf?.program()?.uid(),
2,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import org.dhis2.commons.Constants.PROGRAM_UID
import org.dhis2.commons.Constants.TEI_UID
import org.dhis2.commons.data.TeiAttributesInfo
import org.dhis2.commons.dialogs.imagedetail.ImageDetailActivity
import org.dhis2.commons.featureconfig.data.FeatureConfigRepository
import org.dhis2.commons.resources.EventResourcesProvider
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.databinding.EnrollmentActivityBinding
import org.dhis2.form.data.GeometryController
import org.dhis2.form.data.GeometryParserImpl
import org.dhis2.form.model.EnrollmentRecords
import org.dhis2.form.model.EventMode
import org.dhis2.form.ui.FormView
import org.dhis2.form.ui.provider.FormResultDialogProvider
Expand All @@ -48,17 +45,14 @@ class EnrollmentActivity : ActivityGlobalAbstract(), EnrollmentView {
@Inject
lateinit var resourceManager: ResourceManager

@Inject
lateinit var eventResourcesProvider: EventResourcesProvider

@Inject
lateinit var presenter: EnrollmentPresenterImpl

@Inject
lateinit var enrollmentResultDialogProvider: FormResultDialogProvider
lateinit var dateEditionWarningHandler: DateEditionWarningHandler

@Inject
lateinit var featureConfig: FeatureConfigRepository
lateinit var enrollmentResultDialogProvider: FormResultDialogProvider

lateinit var binding: EnrollmentActivityBinding
lateinit var mode: EnrollmentMode
Expand Down Expand Up @@ -106,32 +100,6 @@ class EnrollmentActivity : ActivityGlobalAbstract(), EnrollmentView {
),
)?.inject(this)

formView = FormView.Builder()
.locationProvider(locationProvider)
.onItemChangeListener { action -> presenter.updateFields(action) }
.onLoadingListener { loading ->
if (loading) {
showProgress()
} else {
hideProgress()
presenter.showOrHideSaveButton()
}
}
.onFinishDataEntry { presenter.finish(mode) }
.eventCompletionResultDialogProvider(enrollmentResultDialogProvider)
.factory(supportFragmentManager)
.setRecords(
EnrollmentRecords(
enrollmentUid = enrollmentUid,
enrollmentMode = org.dhis2.form.model.EnrollmentMode.valueOf(
enrollmentMode.name,
),
),
)
.openErrorLocation(openErrorLocation)
.setProgramUid(programUid)
.build()

super.onCreate(savedInstanceState)

if (presenter.getEnrollment() == null ||
Expand All @@ -141,17 +109,29 @@ class EnrollmentActivity : ActivityGlobalAbstract(), EnrollmentView {
}

forRelationship = intent.getBooleanExtra(FOR_RELATIONSHIP, false)
binding = DataBindingUtil.setContentView(this, R.layout.enrollment_activity)
binding.view = this

mode = enrollmentMode

val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.formViewContainer, formView)
fragmentTransaction.commit()
binding = DataBindingUtil.setContentView(this, R.layout.enrollment_activity)
binding.view = this

binding.save.setOnClickListener {
performSaveClick()
formView = buildEnrollmentForm(
config = EnrollmentFormBuilderConfig(
enrollmentUid = enrollmentUid,
programUid = programUid,
enrollmentMode = org.dhis2.form.model.EnrollmentMode.valueOf(
enrollmentMode.name,
),
hasWriteAccess = presenter.hasWriteAccess(),
openErrorLocation = openErrorLocation,
containerId = R.id.formViewContainer,
loadingView = binding.toolbarProgress,
saveButton = binding.save,
),
locationProvider = locationProvider,
dateEditionWarningHandler = dateEditionWarningHandler,
enrollmentResultDialogProvider = enrollmentResultDialogProvider,
) {
presenter.finish(enrollmentMode)
}

presenter.init()
Expand Down Expand Up @@ -350,27 +330,9 @@ class EnrollmentActivity : ActivityGlobalAbstract(), EnrollmentView {
formView.onSaveClick()
}

override fun showProgress() {
runOnUiThread {
binding.toolbarProgress.show()
}
}

override fun hideProgress() {
runOnUiThread {
binding.toolbarProgress.hide()
}
}

override fun showDateEditionWarning(programUid: String?) {
override fun showDateEditionWarning(message: String?) {
val dialog = MaterialAlertDialogBuilder(this, R.style.DhisMaterialDialog)
.setMessage(
eventResourcesProvider.formatWithProgramEventLabel(
R.string.enrollment_date_edition_warning_event_label,
programUid,
2,
),
)
.setMessage(message)
.setPositiveButton(R.string.button_ok, null)
dialog.show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface EnrollmentFormRepository {
fun generateEvents(): Single<Pair<String, String?>>
fun getProfilePicture(): String
fun getProgramStageUidFromEvent(eventUi: String): String?
fun hasWriteAccess(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.dhis2.bindings.profilePicturePath
import org.dhis2.data.dhislogic.DhisEnrollmentUtils
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.arch.repositories.`object`.ReadOnlyOneObjectRepositoryFinalImpl
import org.hisp.dhis.android.core.enrollment.EnrollmentAccess
import org.hisp.dhis.android.core.enrollment.EnrollmentObjectRepository
import org.hisp.dhis.android.core.program.Program
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance
Expand Down Expand Up @@ -33,4 +34,11 @@ class EnrollmentFormRepositoryImpl(

override fun getProgramStageUidFromEvent(eventUi: String) =
d2.eventModule().events().uid(eventUi).blockingGet()?.programStage()

override fun hasWriteAccess(): Boolean {
return d2.enrollmentModule().enrollmentService().blockingGetEnrollmentAccess(
tei.uid(),
programUid,
) == EnrollmentAccess.WRITE_ACCESS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.dhis2.commons.network.NetworkUtils
import org.dhis2.commons.prefs.PreferenceProviderImpl
import org.dhis2.commons.reporting.CrashReportController
import org.dhis2.commons.resources.DhisPeriodUtils
import org.dhis2.commons.resources.EventResourcesProvider
import org.dhis2.commons.resources.MetadataIconProvider
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.commons.schedulers.SchedulerProvider
Expand Down Expand Up @@ -81,15 +82,21 @@ class EnrollmentModule(

@Provides
@PerActivity
fun provideDataEntryRepository(
fun provideEnrollmentConfiguration(
d2: D2,
metadataIconProvider: MetadataIconProvider,
) = EnrollmentConfiguration(d2, enrollmentUid, metadataIconProvider)

@Provides
@PerActivity
fun provideDataEntryRepository(
modelFactory: FieldViewModelFactory,
enrollmentFormLabelsProvider: EnrollmentFormLabelsProvider,
metadataIconProvider: MetadataIconProvider,
enrollmentConfiguration: EnrollmentConfiguration,
): EnrollmentRepository {
return EnrollmentRepository(
fieldFactory = modelFactory,
conf = EnrollmentConfiguration(d2, enrollmentUid, metadataIconProvider),
conf = enrollmentConfiguration,
enrollmentMode = EnrollmentMode.valueOf(enrollmentMode.name),
enrollmentFormLabelsProvider = enrollmentFormLabelsProvider,
)
Expand Down Expand Up @@ -129,12 +136,21 @@ class EnrollmentModule(
)
}

@Provides
@PerActivity
fun provideDateEditionWarningHandler(
enrollmentConfiguration: EnrollmentConfiguration,
eventResourcesProvider: EventResourcesProvider,
) = DateEditionWarningHandler(
enrollmentConfiguration,
eventResourcesProvider,
)

@Provides
@PerActivity
fun providePresenter(
d2: D2,
enrollmentObjectRepository: EnrollmentObjectRepository,
dataEntryRepository: EnrollmentRepository,
teiRepository: TrackedEntityInstanceObjectRepository,
programRepository: ReadOnlyOneObjectRepositoryFinalImpl<Program>,
schedulerProvider: SchedulerProvider,
Expand All @@ -143,12 +159,12 @@ class EnrollmentModule(
matomoAnalyticsController: MatomoAnalyticsController,
eventCollectionRepository: EventCollectionRepository,
teiAttributesProvider: TeiAttributesProvider,
dateEditionWarningHandler: DateEditionWarningHandler,
): EnrollmentPresenterImpl {
return EnrollmentPresenterImpl(
enrollmentView,
d2,
enrollmentObjectRepository,
dataEntryRepository,
teiRepository,
programRepository,
schedulerProvider,
Expand All @@ -157,6 +173,7 @@ class EnrollmentModule(
matomoAnalyticsController,
eventCollectionRepository,
teiAttributesProvider,
dateEditionWarningHandler,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.dhis2.commons.matomo.Labels.Companion.CLICK
import org.dhis2.commons.matomo.MatomoAnalyticsController
import org.dhis2.commons.schedulers.SchedulerProvider
import org.dhis2.commons.schedulers.defaultSubscribe
import org.dhis2.form.data.EnrollmentRepository
import org.dhis2.form.model.RowAction
import org.dhis2.usescases.teiDashboard.TeiAttributesProvider
import org.dhis2.utils.analytics.AnalyticsHelper
Expand All @@ -39,7 +38,6 @@ class EnrollmentPresenterImpl(
val view: EnrollmentView,
val d2: D2,
private val enrollmentObjectRepository: EnrollmentObjectRepository,
private val dataEntryRepository: EnrollmentRepository,
private val teiRepository: TrackedEntityInstanceObjectRepository,
private val programRepository: ReadOnlyOneObjectRepositoryFinalImpl<Program>,
private val schedulerProvider: SchedulerProvider,
Expand All @@ -48,11 +46,11 @@ class EnrollmentPresenterImpl(
private val matomoAnalyticsController: MatomoAnalyticsController,
private val eventCollectionRepository: EventCollectionRepository,
private val teiAttributesProvider: TeiAttributesProvider,
private val dateEditionWarningHandler: DateEditionWarningHandler,
) {

private val disposable = CompositeDisposable()
private val backButtonProcessor: FlowableProcessor<Boolean> = PublishProcessor.create()
private var hasShownIncidentDateEditionWarning = false
private var hasShownEnrollmentDateEditionWarning = false

fun init() {
view.setSaveButtonVisible(false)
Expand Down Expand Up @@ -120,24 +118,6 @@ class EnrollmentPresenterImpl(
)
}

private fun shouldShowDateEditionWarning(uid: String): Boolean {
return if (uid == EnrollmentRepository.ENROLLMENT_DATE_UID &&
dataEntryRepository.hasEventsGeneratedByEnrollmentDate() &&
!hasShownEnrollmentDateEditionWarning
) {
hasShownEnrollmentDateEditionWarning = true
true
} else if (uid == EnrollmentRepository.INCIDENT_DATE_UID &&
dataEntryRepository.hasEventsGeneratedByIncidentDate() &&
!hasShownIncidentDateEditionWarning
) {
hasShownIncidentDateEditionWarning = true
true
} else {
false
}
}

fun subscribeToBackButton() {
disposable.add(
backButtonProcessor
Expand Down Expand Up @@ -173,8 +153,8 @@ class EnrollmentPresenterImpl(

fun updateFields(action: RowAction? = null) {
action?.let {
if (shouldShowDateEditionWarning(it.id)) {
view.showDateEditionWarning(getProgram()?.uid())
dateEditionWarningHandler.shouldShowWarning(fieldUid = it.id) { message ->
view.showDateEditionWarning(message)
}
}
}
Expand Down Expand Up @@ -238,6 +218,8 @@ class EnrollmentPresenterImpl(
}
}

fun hasWriteAccess() = enrollmentFormRepository.hasWriteAccess()

fun showOrHideSaveButton() {
val teiUid = teiRepository.blockingGet()?.uid() ?: ""
val programUid = getProgram()?.uid() ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ interface EnrollmentView : AbstractActivityContracts.View {
fun setResultAndFinish()
fun requestFocus()
fun performSaveClick()
fun showProgress()
fun hideProgress()
fun displayTeiPicture(picturePath: String)
fun showDateEditionWarning(programUid: String?)
fun showDateEditionWarning(message: String?)
}
Loading

0 comments on commit cb924d1

Please sign in to comment.