diff --git a/app/src/main/java/org/dhis2/usescases/events/EventInfoProvider.kt b/app/src/main/java/org/dhis2/usescases/events/EventInfoProvider.kt index a2da03a56f..ba54ae8ff9 100644 --- a/app/src/main/java/org/dhis2/usescases/events/EventInfoProvider.kt +++ b/app/src/main/java/org/dhis2/usescases/events/EventInfoProvider.kt @@ -14,6 +14,7 @@ import org.dhis2.commons.bindings.enrollment import org.dhis2.commons.bindings.fromCache import org.dhis2.commons.bindings.tei import org.dhis2.commons.date.DateLabelProvider +import org.dhis2.commons.date.DateUtils import org.dhis2.commons.date.toOverdueOrScheduledUiText import org.dhis2.commons.resources.MetadataIconProvider import org.dhis2.commons.resources.ResourceManager @@ -41,6 +42,7 @@ class EventInfoProvider( private val dateLabelProvider: DateLabelProvider, private val metadataIconProvider: MetadataIconProvider, private val profilePictureProvider: ProfilePictureProvider, + private val dateUtils: DateUtils, ) { private val cachedPrograms = mutableMapOf() private val cachedDisplayOrgUnit = mutableMapOf() @@ -276,18 +278,19 @@ class EventInfoProvider( EventStatus.SCHEDULE -> { val text = dueDate.toOverdueOrScheduledUiText(resourceManager) + val color = if (dateUtils.isEventDueDateOverdue(dueDate)) AdditionalInfoItemColor.ERROR.color else AdditionalInfoItemColor.SUCCESS.color AdditionalInfoItem( icon = { Icon( imageVector = Icons.Outlined.Event, contentDescription = text, - tint = AdditionalInfoItemColor.SUCCESS.color, + tint = color, ) }, value = text, isConstantItem = true, - color = AdditionalInfoItemColor.SUCCESS.color, + color = color, ) } diff --git a/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailModule.kt b/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailModule.kt index e95fb9b5b3..d61a575874 100644 --- a/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailModule.kt +++ b/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailModule.kt @@ -139,12 +139,14 @@ class ProgramEventDetailModule( resourceManager: ResourceManager, metadataIconProvider: MetadataIconProvider, profilePictureProvider: ProfilePictureProvider, + dateUtils: DateUtils, ) = EventInfoProvider( d2, resourceManager, DateLabelProvider(context, resourceManager), metadataIconProvider, profilePictureProvider, + dateUtils, ) @Provides diff --git a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEModule.java b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEModule.java index 1dec62ca98..89c30cd2f3 100644 --- a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEModule.java +++ b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEModule.java @@ -178,7 +178,8 @@ SearchRepositoryKt searchRepositoryKt( DispatcherProvider dispatcherProvider, FieldViewModelFactory fieldViewModelFactory, MetadataIconProvider metadataIconProvider, - ColorUtils colorUtils + ColorUtils colorUtils, + DateUtils dateUtils ) { ResourceManager resourceManager = new ResourceManager(moduleContext, colorUtils); DateLabelProvider dateLabelProvider = new DateLabelProvider(moduleContext, new ResourceManager(moduleContext, colorUtils)); @@ -201,7 +202,8 @@ SearchRepositoryKt searchRepositoryKt( resourceManager, dateLabelProvider, metadataIconProvider, - profilePictureProvider + profilePictureProvider, + dateUtils ) ); } @@ -323,6 +325,13 @@ SearchTeiViewModelFactory providesViewModelFactory( ); } + @Provides + @PerActivity + DateUtils provideDateUtils( + ) { + return DateUtils.getInstance(); + } + @Provides @PerActivity ProgramConfigurationRepository provideProgramConfigurationRepository( diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipModule.java b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipModule.java index 119258cb81..8c243f102e 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipModule.java +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipModule.java @@ -4,6 +4,8 @@ import org.dhis2.commons.data.ProgramConfigurationRepository; import org.dhis2.commons.date.DateLabelProvider; +import org.dhis2.commons.date.DateUtils; +import org.dhis2.commons.di.dagger.PerActivity; import org.dhis2.commons.di.dagger.PerFragment; import org.dhis2.commons.resources.MetadataIconProvider; import org.dhis2.commons.resources.ResourceManager; @@ -96,7 +98,8 @@ RelationshipMapsRepository providesRepository( D2 d2, ResourceManager resourceManager, MetadataIconProvider metadataIconProvider, - DateLabelProvider dateLabelProvider + DateLabelProvider dateLabelProvider, + DateUtils dateUtils ) { RelationshipConfiguration config; if (teiUid != null) { @@ -119,7 +122,8 @@ RelationshipMapsRepository providesRepository( resourceManager, dateLabelProvider, metadataIconProvider, - profilePictureProvider + profilePictureProvider, + dateUtils ) ); } @@ -155,6 +159,13 @@ RelationshipsViewModel provideRelationshipsViewModel( ); } + @Provides + @PerFragment + DateUtils provideDateUtils( + ) { + return DateUtils.getInstance(); + } + @Provides @PerFragment GetRelationshipsByType provideGetRelationshipsByType( diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataModule.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataModule.kt index 4c21b073f5..5dc9bde68f 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataModule.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataModule.kt @@ -165,8 +165,9 @@ class TEIDataModule( @PerFragment fun providesTEIEventCardMapper( resourceManager: ResourceManager, + dateUtils: DateUtils, ): TEIEventCardMapper { - return TEIEventCardMapper(resourceManager) + return TEIEventCardMapper(resourceManager, dateUtils) } @Provides diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/ui/mapper/TEIEventCardMapper.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/ui/mapper/TEIEventCardMapper.kt index 1cb3755533..c2754be65e 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/ui/mapper/TEIEventCardMapper.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/ui/mapper/TEIEventCardMapper.kt @@ -14,6 +14,7 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import org.dhis2.R import org.dhis2.commons.data.EventViewModel +import org.dhis2.commons.date.DateUtils import org.dhis2.commons.date.toOverdueOrScheduledUiText import org.dhis2.commons.resources.ResourceManager import org.dhis2.commons.ui.model.ListCardUiModel @@ -34,6 +35,7 @@ import java.util.Date class TEIEventCardMapper( val resourceManager: ResourceManager, + val dateUtils: DateUtils, ) { fun map( @@ -193,18 +195,18 @@ class TEIEventCardMapper( EventStatus.SCHEDULE -> { val text = dueDate.toOverdueOrScheduledUiText(resourceManager) - + val color = if (dateUtils.isEventDueDateOverdue(dueDate)) AdditionalInfoItemColor.ERROR.color else AdditionalInfoItemColor.SUCCESS.color AdditionalInfoItem( icon = { Icon( imageVector = Icons.Outlined.Event, contentDescription = text, - tint = AdditionalInfoItemColor.SUCCESS.color, + tint = color, ) }, value = text, isConstantItem = true, - color = AdditionalInfoItemColor.SUCCESS.color, + color = color, ) } diff --git a/commons/src/androidTest/java/org/dhis2/commons/date/DateUtilsTest.java b/commons/src/androidTest/java/org/dhis2/commons/date/DateUtilsTest.java new file mode 100644 index 0000000000..82c6169e12 --- /dev/null +++ b/commons/src/androidTest/java/org/dhis2/commons/date/DateUtilsTest.java @@ -0,0 +1,26 @@ +package org.dhis2.commons.date; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import java.util.Calendar; +import java.util.Date; + +public class DateUtilsTest { + + + @Test + public void returnsEventOverDueDateCorrectly() { + + Calendar calendar = Calendar.getInstance(); + //should return false for current date + calendar.setTime(new Date()); + assertEquals(false, DateUtils.getInstance().isEventDueDateOverdue(calendar.getTime())); + //false for future date + calendar.add(Calendar.DAY_OF_MONTH, 10); + assertEquals(false, DateUtils.getInstance().isEventDueDateOverdue(calendar.getTime())); + //true for past dates + calendar.add(Calendar.DAY_OF_MONTH, -30); + assertEquals(true, DateUtils.getInstance().isEventDueDateOverdue(calendar.getTime())); + + } +} diff --git a/commons/src/main/java/org/dhis2/commons/date/DateUtils.java b/commons/src/main/java/org/dhis2/commons/date/DateUtils.java index 490f46de22..73229db3ca 100644 --- a/commons/src/main/java/org/dhis2/commons/date/DateUtils.java +++ b/commons/src/main/java/org/dhis2/commons/date/DateUtils.java @@ -335,6 +335,19 @@ public Boolean isEventExpired(@Nullable Date currentDate, Date completedDay, int completedDay.getTime() + TimeUnit.DAYS.toMillis(compExpDays) < date.getTime(); } + /** + * Check if an event due date is overdue + * + * @param dueDate the date the event is due + * @return true or false + */ + public Boolean isEventDueDateOverdue(Date dueDate) { + Date currentDate = getStartOfDay(new Date()); + + return dueDate.before(currentDate); + } + + /** * @param currentDate Date from which calculation will be carried out. Default value is today. * @param expiryDays Number of extra days to add events on previous period