Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [ANDROAPP-5794] Date Selectors #3501

Merged
merged 12 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.dhis2.usescases.eventsWithoutRegistration.eventDetails
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
Expand Down Expand Up @@ -224,8 +225,8 @@ class EventInitialTest {
EventInputDateUiModel(
eventDate = date,
detailsEnabled = details.enabled,
onDateClick = { viewModel.onDateClick() },
onDateSet = { dateValues ->
onDateClick = {} ,
onDateSelected = { dateValues ->
viewModel.onDateSet(dateValues.year, dateValues.month, dateValues.day)
},
onClear = { viewModel.onClearEventReportDate() },
Expand Down Expand Up @@ -257,8 +258,8 @@ class EventInitialTest {
EventInputDateUiModel(
eventDate = date,
detailsEnabled = details.enabled,
onDateClick = { viewModel.onDateClick() },
onDateSet = { dateValues ->
onDateClick = {},
onDateSelected = { dateValues ->
viewModel.onDateSet(dateValues.year, dateValues.month, dateValues.day)
},
onClear = { viewModel.onClearEventReportDate() },
Expand Down Expand Up @@ -287,7 +288,7 @@ class EventInitialTest {
val catCombo by viewModel.eventCatCombo.collectAsState()

ProvideCategorySelector(
modifier = Modifier,
modifier = Modifier.testTag(EMPTY_CATEGORY_SELECTOR),
eventCatComboUiModel = EventCatComboUiModel(
EventCategory("UID", "NO OPTIONS ", 0, emptyList()),
eventCatCombo = catCombo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.ComposeTestRule
import org.dhis2.common.BaseRobot
import org.dhis2.usescases.flow.teiFlow.entity.DateRegistrationUIModel
import org.dhis2.usescases.searchte.robot.searchTeiRobot
import org.dhis2.usescases.flow.teiFlow.entity.EnrollmentListUIModel
import org.dhis2.usescases.flow.teiFlow.entity.RegisterTEIUIModel
import org.dhis2.usescases.searchte.robot.searchTeiRobot
Expand Down Expand Up @@ -100,21 +101,16 @@ class TeiFlowRobot : BaseRobot() {
}
}

fun changeDueDate(
date: DateRegistrationUIModel,
programStage: String,
orgUnit: String,
composeTestRule: ComposeTestRule
) {
fun changeDueDate(date: String, programStage: String, orgUnit: String, composeTestRule: ComposeTestRule) {
teiDashboardRobot {
clickOnStageGroup(programStage)
clickOnEventGroupByStageUsingOU(orgUnit)
}

eventRobot {
clickOnEventDueDate(composeTestRule)
selectSpecificDate(date.year, date.month, date.day)
acceptUpdateEventDate()
clickOnEventReportDate(composeTestRule)
selectSpecificDate(composeTestRule, date)
acceptUpdateEventDate(composeTestRule)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,7 @@ class TeiFlowTest: BaseTest() {
const val LASTNAME = "Stuart"

const val DATE_FORMAT = "dd/M/yyyy"
const val DATE_PICKER_FORMAT = "EEEE, MMMM d, yyyy"

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -432,11 +433,12 @@ class SearchTETest : BaseTest() {
return DateRegistrationUIModel(year, month, day)
}

private fun createOverdueDate() = DateRegistrationUIModel(
currentDate.year,
currentDate.month - 1,
currentDate.day
)
private fun createOverdueDate() : String {
val sdf = SimpleDateFormat(TeiFlowTest.DATE_PICKER_FORMAT)
val calendar = Calendar.getInstance()
calendar.add(Calendar.DAY_OF_YEAR, -7)
return sdf.format(calendar.time)
}

private val dateRegistration = createFirstSpecificDate()
private val dateEnrollment = createEnrollmentDate()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package org.dhis2.usescases.teidashboard.robot

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasAnySibling
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.printToLog
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.PickerActions
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withSubstring
Expand Down Expand Up @@ -80,8 +83,8 @@ class EventRobot : BaseRobot() {
onView(withId(R.id.date)).perform(click())
}

fun acceptUpdateEventDate() {
onView(withId(R.id.acceptBtn)).perform(click())
fun acceptUpdateEventDate(composeTestRule: ComposeTestRule) {
composeTestRule.onNodeWithText("OK",true).performClick()
}

fun clickOnUpdate() {
Expand All @@ -97,12 +100,6 @@ class EventRobot : BaseRobot() {
)
}

fun clickOnFutureAlertDialog(){
clickOnChangeDate()
clickOnEditDate()
acceptUpdateEventDate()
clickOnUpdate()
}

fun checkDetails(eventDate: String, eventOrgUnit: String) {
onView(withId(R.id.eventSecundaryInfo)).check(matches(
Expand Down Expand Up @@ -133,14 +130,20 @@ class EventRobot : BaseRobot() {
onView(withId(R.id.possitive)).perform(click())
}

fun clickOnEventDueDate(composeTestRule: ComposeTestRule) {
composeTestRule.onNodeWithTag("INPUT_DATE_TIME_TEXT").assertIsDisplayed()
composeTestRule.onNodeWithTag("INPUT_DATE_TIME_TEXT").performClick()
fun clickOnEventReportDate(composeTestRule: ComposeTestRule) {

composeTestRule.onNode(hasTestTag("INPUT_DATE_TIME_ACTION_BUTTON") and hasAnySibling(
hasText("Report date")
)).assertIsDisplayed().performClick()

}

fun selectSpecificDate(year: Int, monthOfYear: Int, dayOfMonth: Int) {
onView(withId(R.id.datePicker)).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth))
fun selectSpecificDate( composeTestRule: ComposeTestRule, date: String) {
composeTestRule.onNodeWithTag("DATE_PICKER").assertIsDisplayed().printToLog("DATE-PICKER-COMPONENTS")

composeTestRule.onNodeWithText(date,true).performClick()
composeTestRule.onNodeWithTag("DATE_PICKER").printToLog("DATE-PICKER2")

}

fun checkEventDetails(eventDate: String, eventOrgUnit: String, composeTestRule: ComposeTestRule) {
Expand Down
141 changes: 62 additions & 79 deletions app/src/main/java/org/dhis2/usescases/events/ScheduledEventActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.DatePicker
import androidx.compose.foundation.layout.Column
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.databinding.DataBindingUtil
import org.dhis2.App
import org.dhis2.R
import org.dhis2.commons.data.EventCreationType
import org.dhis2.commons.dialogs.calendarpicker.CalendarPicker
import org.dhis2.commons.dialogs.calendarpicker.OnDatePickerListener
import org.dhis2.databinding.ActivityEventScheduledBinding
import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureActivity
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventDate
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventInputDateUiModel
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvideInputDate
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvidePeriodSelector
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.willShowCalendar
import org.dhis2.usescases.eventsWithoutRegistration.eventInitial.EventInitialActivity
import org.dhis2.usescases.general.ActivityGlobalAbstract
import org.dhis2.utils.DateUtils
Expand All @@ -28,8 +28,6 @@ import org.hisp.dhis.android.core.event.EventStatus
import org.hisp.dhis.android.core.period.PeriodType
import org.hisp.dhis.android.core.program.Program
import org.hisp.dhis.android.core.program.ProgramStage
import java.util.Calendar
import java.util.Date
import javax.inject.Inject

const val EXTRA_EVENT_UID = "EVENT_UID"
Expand Down Expand Up @@ -98,7 +96,6 @@ class ScheduledEventActivity : ActivityGlobalAbstract(), ScheduledEventContract.
override fun setStage(programStage: ProgramStage, event: Event) {
this.stage = programStage
binding.programStage = programStage

binding.scheduledEventFieldContainer.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
Expand All @@ -108,32 +105,67 @@ class ScheduledEventActivity : ActivityGlobalAbstract(), ScheduledEventContract.
?: getString(R.string.report_date),
dateValue = "",
)

ProvideInputDate(
EventInputDateUiModel(
eventDate = eventDate,
allowsManualInput = false,
detailsEnabled = true,
onDateClick = { setEvenDateListener(programStage.periodType()) },
onDateSet = {},
onClear = {},
),

val dueDate = EventDate(
label = programStage.dueDateLabel() ?: getString(R.string.due_date),
dateValue = DateUtils.uiDateFormat().format(event.dueDate() ?: ""),
)
if (programStage.hideDueDate() == false) {
val dueDate = EventDate(
label = programStage.dueDateLabel() ?: getString(R.string.due_date),
dateValue = DateUtils.uiDateFormat().format(event.dueDate() ?: ""),
)

if (willShowCalendar(programStage.periodType())) {
ProvideInputDate(
EventInputDateUiModel(
eventDate = dueDate,
eventDate = eventDate,
allowsManualInput = false,
detailsEnabled = true,
onDateClick = {},
onDateSelected = { date -> presenter.setEventDate(presenter.formatDateValues(date)) },
selectableDates = presenter.getSelectableDates(program, false),
),
)

if (programStage.hideDueDate() == false) {
ProvideInputDate(
EventInputDateUiModel(
eventDate = dueDate,
allowsManualInput = false,
detailsEnabled = true,
onDateClick = {},
onDateSelected = { date ->
presenter.setDueDate(presenter.formatDateValues(date))
},
selectableDates = presenter.getSelectableDates(program, true),
),
)
}
} else {
ProvidePeriodSelector(
uiModel = EventInputDateUiModel(
eventDate = eventDate,
detailsEnabled = true,
onDateClick = { setDueDateListener(programStage.periodType()) },
onDateSet = {},
onClear = {},
onDateClick = { showEventDatePeriodDialog(programStage.periodType()) },
onDateSelected = {},
onClear = { },
required = true,
showField = true,
selectableDates = presenter.getSelectableDates(program, false),
),
modifier = Modifier,
)

if (programStage.hideDueDate() == false) {
ProvidePeriodSelector(
uiModel = EventInputDateUiModel(
eventDate = dueDate,
detailsEnabled = true,
onDateClick = { showDueDatePeriodDialog(programStage.periodType()) },
onDateSelected = {},
onClear = { },
required = true,
showField = true,
selectableDates = presenter.getSelectableDates(program, false),
),
modifier = Modifier,
)
}
}
}
}
Expand All @@ -145,10 +177,8 @@ class ScheduledEventActivity : ActivityGlobalAbstract(), ScheduledEventContract.
binding.name = program.displayName()
}

private fun setEvenDateListener(periodType: PeriodType?) {
if (periodType == null) {
showCustomCalendar(false)
} else {
private fun showEventDatePeriodDialog(periodType: PeriodType?) {
if (periodType != null) {
var minDate =
DateUtils.getInstance().expDate(null, program.expiryDays()!!, periodType)
val lastPeriodDate =
Expand All @@ -174,10 +204,8 @@ class ScheduledEventActivity : ActivityGlobalAbstract(), ScheduledEventContract.
}
}

private fun setDueDateListener(periodType: PeriodType?) {
if (periodType == null) {
showCustomCalendar(true)
} else {
private fun showDueDatePeriodDialog(periodType: PeriodType?) {
if (periodType != null) {
var minDate =
DateUtils.getInstance().expDate(null, program.expiryDays()!!, periodType)
val lastPeriodDate =
Expand All @@ -203,51 +231,6 @@ class ScheduledEventActivity : ActivityGlobalAbstract(), ScheduledEventContract.
}
}

private fun showCustomCalendar(isDueDate: Boolean) {
val dialog = CalendarPicker(this)

if (isDueDate) {
dialog.setInitialDate(event.dueDate())
dialog.setScheduleInterval(stage.standardInterval() ?: 0)
}

if (program.expiryPeriodType() != null) {
val minDate = DateUtils.getInstance().expDate(
null,
program.expiryDays() ?: 0,
program.expiryPeriodType(),
)
dialog.setMinDate(minDate)
}

if (!isDueDate) {
dialog.setMaxDate(Date(System.currentTimeMillis() - 1000))
}
dialog.setListener(object : OnDatePickerListener {
override fun onNegativeClick() {
dialog.dismiss()
}

override fun onPositiveClick(datePicker: DatePicker) {
val date = Calendar.getInstance().apply {
set(Calendar.YEAR, datePicker.year)
set(Calendar.MONTH, datePicker.month)
set(Calendar.DAY_OF_MONTH, datePicker.dayOfMonth)
set(Calendar.HOUR, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.time
if (isDueDate) {
presenter.setDueDate(date)
} else {
presenter.setEventDate(date)
}
}
})
dialog.show()
}

override fun openInitialActivity() {
val bundle = EventInitialActivity.getBundle(
program.uid(),
Expand Down
Loading
Loading