diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt index 519e35cb93..15a59e781e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt @@ -80,7 +80,8 @@ class EditNotificationProfileScheduleViewModel( repository.manuallyToggleProfile(profileId, schedule) .toSingleDefault(r) } else { - Single.just(r) + repository.updateManuallyEnabledDataIfNecessary(profileId, schedule) + .toSingleDefault(r) } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfilesRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfilesRepository.kt index 9c92b07229..e3a09ba354 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfilesRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfilesRepository.kt @@ -15,6 +15,8 @@ import org.thoughtcrime.securesms.notifications.profiles.NotificationProfile import org.thoughtcrime.securesms.notifications.profiles.NotificationProfileSchedule import org.thoughtcrime.securesms.notifications.profiles.NotificationProfiles import org.thoughtcrime.securesms.recipients.RecipientId +import org.thoughtcrime.securesms.util.toLocalDateTime +import org.thoughtcrime.securesms.util.toMillis /** * One stop shop for all your Notification Profile data needs. @@ -112,9 +114,9 @@ class NotificationProfilesRepository { SignalStore.notificationProfileValues().lastProfilePopupTime = 0 } else { val inScheduledWindow = schedule.isCurrentlyActive(now) - SignalStore.notificationProfileValues().manuallyEnabledProfile = if (inScheduledWindow) 0 else profileId - SignalStore.notificationProfileValues().manuallyEnabledUntil = if (inScheduledWindow) 0 else Long.MAX_VALUE - SignalStore.notificationProfileValues().manuallyDisabledAt = if (inScheduledWindow) 0 else now + SignalStore.notificationProfileValues().manuallyEnabledProfile = profileId + SignalStore.notificationProfileValues().manuallyEnabledUntil = if (inScheduledWindow) schedule.endDateTime(now.toLocalDateTime()).toMillis() else Long.MAX_VALUE + SignalStore.notificationProfileValues().manuallyDisabledAt = now } } .doOnComplete { ApplicationDependencies.getDatabaseObserver().notifyNotificationProfileObservers() } @@ -131,5 +133,21 @@ class NotificationProfilesRepository { .subscribeOn(Schedulers.io()) } + fun updateManuallyEnabledDataIfNecessary(profileId: Long, schedule: NotificationProfileSchedule, now: Long = System.currentTimeMillis()): Completable { + return Completable.fromAction { + val profiles = database.getProfiles() + val activeProfile = NotificationProfiles.getActiveProfile(profiles, now) + + if (profileId == activeProfile?.id) { + val inScheduledWindow = schedule.isCurrentlyActive(now) + SignalStore.notificationProfileValues().manuallyEnabledProfile = if (inScheduledWindow) profileId else 0 + SignalStore.notificationProfileValues().manuallyEnabledUntil = if (inScheduledWindow) schedule.endDateTime(now.toLocalDateTime()).toMillis() else Long.MAX_VALUE + SignalStore.notificationProfileValues().manuallyDisabledAt = if (inScheduledWindow) now else 0 + } + } + .doOnComplete { ApplicationDependencies.getDatabaseObserver().notifyNotificationProfileObservers() } + .subscribeOn(Schedulers.io()) + } + class NotificationProfileNotFoundException : Throwable() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt index d65115e470..a1fb446178 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt @@ -60,7 +60,7 @@ data class NotificationProfileSchedule( } fun endDateTime(now: LocalDateTime): LocalDateTime { - return end.toLocalDateTime(now) + return end.toLocalDateTime(now).plusDays(if (end < start) 1 else 0) } }