diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-03-03 15:36:21 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-03-03 15:36:21 -0300 |
| commit | 0e63d462e2cb26e299d567fce194ceea89a7069c (patch) | |
| tree | 0bbfabb4c175e3f0c329b73a4162d41b703e3eeb /android | |
| parent | dcd9071a2bc10348368a487872da4305c932c77a (diff) | |
| parent | 173eb905688fe4f23ca9726a0b832f699abb97ed (diff) | |
| download | mullvadvpn-0e63d462e2cb26e299d567fce194ceea89a7069c.tar.xz mullvadvpn-0e63d462e2cb26e299d567fce194ceea89a7069c.zip | |
Merge branch 'prioritize-account-in-app-banner'
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt | 60 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/StatusLevel.kt | 2 |
2 files changed, 20 insertions, 42 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt index a67cd7ea45..aada1847a9 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt @@ -1,78 +1,56 @@ package net.mullvad.mullvadvpn.ui.notification +import java.util.PriorityQueue import kotlin.properties.Delegates.observable class InAppNotificationController(private val onNotificationChanged: (InAppNotification?) -> Unit) { - private val indices = HashMap<InAppNotification, Int>() - private val notifications = ArrayList<InAppNotification>() + private val notificationPrioritizer = + compareByDescending<InAppNotification> { it.shouldShow } + .thenBy { it.status } + .thenBy { notifications.get(it)!! } - private var currentIndex: Int? = null + private val activeNotifications = PriorityQueue(notificationPrioritizer) + private val notifications = HashMap<InAppNotification, Int>() - var current by observable<InAppNotification?>(null) { _, _, notification -> - onNotificationChanged.invoke(notification) + var current by observable<InAppNotification?>(null) { _, oldNotification, newNotification -> + if (oldNotification != newNotification) { + onNotificationChanged.invoke(newNotification) + } } fun register(notification: InAppNotification) { notification.controller = this - indices.put(notification, notifications.size) - notifications.add(notification) + notifications.put(notification, notifications.size) notificationChanged(notification) } fun onResume() { - for (notification in notifications) { + for (notification in notifications.keys) { notification.onResume() } } fun onPause() { - for (notification in notifications) { + for (notification in notifications.keys) { notification.onPause() } } fun onDestroy() { - for (notification in notifications) { + for (notification in notifications.keys) { notification.onDestroy() } } fun notificationChanged(notification: InAppNotification) { - if (notification.shouldShow) { - maybeShowNotification(notification) + if (notification.shouldShow && !activeNotifications.contains(notification)) { + activeNotifications.add(notification) } else { - maybeHideNotification(notification) + activeNotifications.remove(notification) } - } - - private fun maybeShowNotification(notification: InAppNotification) { - indices.get(notification)?.let { index -> - if (index <= (currentIndex ?: Int.MAX_VALUE)) { - current = notification - currentIndex = index - } - } - } - private fun maybeHideNotification(notification: InAppNotification) { - if (current == notification) { - val start = currentIndex!! + 1 - val end = notifications.size - - for (index in start until end) { - val candidate = notifications.get(index) - - if (candidate.shouldShow) { - current = candidate - currentIndex = index - return - } - } - - current = null - currentIndex = null - } + current = activeNotifications.peek() } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/StatusLevel.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/StatusLevel.kt index e592e55647..6960fd656b 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/StatusLevel.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/StatusLevel.kt @@ -1,6 +1,6 @@ package net.mullvad.mullvadvpn.ui.notification enum class StatusLevel { - Warning, Error, + Warning, } |
