Fix conflict when manually enabled an older profile with a schedule overlap with a newer profile.

This commit is contained in:
Cody Henthorne 2021-12-09 14:50:37 -05:00
parent 76539ff0f2
commit c30a43ef45
3 changed files with 24 additions and 5 deletions

View file

@ -80,7 +80,8 @@ class EditNotificationProfileScheduleViewModel(
repository.manuallyToggleProfile(profileId, schedule)
.toSingleDefault(r)
} else {
Single.just(r)
repository.updateManuallyEnabledDataIfNecessary(profileId, schedule)
.toSingleDefault(r)
}
}
}

View file

@ -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()
}

View file

@ -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)
}
}