summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-06-26 16:54:06 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-07-01 20:22:02 +0000
commitdb65636ad44cdf807a441d2cea2806d09e48ecb1 (patch)
tree0871142bbb17c81002cf14bf56b576380d8128f4 /android/src
parentc2fbf06016da682ac26cec220bebbbe67069af43 (diff)
downloadmullvadvpn-db65636ad44cdf807a441d2cea2806d09e48ecb1.tar.xz
mullvadvpn-db65636ad44cdf807a441d2cea2806d09e48ecb1.zip
Create `InAppNotificationController` class
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt78
1 files changed, 78 insertions, 0 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
new file mode 100644
index 0000000000..3c1a62d925
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/notification/InAppNotificationController.kt
@@ -0,0 +1,78 @@
+package net.mullvad.mullvadvpn.ui.notification
+
+import kotlin.properties.Delegates.observable
+
+class InAppNotificationController(private val onNotificationChanged: (InAppNotification?) -> Unit) {
+ private val indices = HashMap<InAppNotification, Int>()
+ private val notifications = ArrayList<InAppNotification>()
+
+ private var currentIndex: Int? = null
+
+ var current by observable<InAppNotification?>(null) { _, _, notification ->
+ onNotificationChanged?.invoke(notification)
+ }
+
+ fun register(notification: InAppNotification) {
+ notification.controller = this
+
+ indices.put(notification, notifications.size)
+ notifications.add(notification)
+
+ notificationChanged(notification)
+ }
+
+ fun onResume() {
+ for (notification in notifications) {
+ notification.onResume()
+ }
+ }
+
+ fun onPause() {
+ for (notification in notifications) {
+ notification.onPause()
+ }
+ }
+
+ fun onDestroy() {
+ for (notification in notifications) {
+ notification.onDestroy()
+ }
+ }
+
+ fun notificationChanged(notification: InAppNotification) {
+ if (notification.shouldShow) {
+ maybeShowNotification(notification)
+ } else {
+ maybeHideNotification(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
+ }
+ }
+}