scoop:scoop.ui.component.desktop:0.1.0
src/notification.scoop
Selected production source · src/notification.scoop
1
package scoop.ui.component.desktop3
import scoop.Result4
import scoop.desktop.notification.*5
import scoop.ui.*7
/** Borrows a center and owns only this scope's requests and subscriptions. */8
public class NativeNotificationController internal constructor(9
private val center: NotificationCenter,10
private val mounted: () -> Boolean,11
private val dispatch: (() -> Unit) -> Boolean12
): Closeable {13
private var closed = false14
private val registrations = ArrayList<NativeNotificationRegistration>()16
internal fun checkThread() {17
center.isClosed18
}19
private fun requireOpen() {20
checkThread()21
if (closed || !mounted() || center.isClosed)22
throw IllegalStateException(Some("notification controller is not active"))23
}24
internal fun remove(value: NativeNotificationRegistration) {25
for (index in 0L ..< registrations.size)26
if (registrations[index] === value) {27
registrations.removeAt(index)28
return29
}30
}31
public fun settings(32
callback: (Result<NotificationSettings, NotificationError>) -> Unit33
): Closeable = start({ center.settings(it) }, callback)35
public fun requestPermission(36
callback: (Result<NotificationSettings, NotificationError>) -> Unit37
): Closeable = start({ center.requestPermission(it) }, callback)39
public fun post(40
id: NotificationId,41
content: NotificationContent,42
callback: (Result<NotificationSubmission, NotificationError>) -> Unit43
): Closeable = start({ center.post(id, content, it) }, callback)45
public fun withdraw(46
id: NotificationId,47
callback: (Result<NotificationWithdrawal, NotificationError>) -> Unit48
): Closeable = start({ center.withdraw(id, it) }, callback)50
private fun <T> start(51
submit: ((Result<T, NotificationError>) -> Unit) -> NotificationOperation,52
callback: (Result<T, NotificationError>) -> Unit53
): Closeable {54
requireOpen()55
val entry = NativeNotificationRegistration(this)56
registrations.add(entry)57
try {58
entry.resource =59
Some(60
submit {61
result -> if (entry.active) {62
entry.close()63
if (!closed)64
dispatch {65
if (!closed)66
callback(result)67
}68
}69
}70
)71
} catch (error: Throwable) {72
entry.close()73
throw error74
}75
return entry76
}77
public fun observe(callback: (NotificationEvent) -> Unit): Closeable {78
requireOpen()79
val entry = NativeNotificationRegistration(this)80
registrations.add(entry)81
try {82
entry.resource =83
Some(84
center.observe {85
event -> if (entry.active && !closed)86
dispatch {87
if (entry.active && !closed)88
callback(event)89
}90
}91
)92
} catch (error: Throwable) {93
entry.close()94
throw error95
}96
return entry97
}98
public override fun close() {99
checkThread()100
if (closed)101
return102
closed = true103
for (entry in registrations.toArray())104
entry.close()105
}106
}108
internal class NativeNotificationRegistration(109
private val owner: NativeNotificationController110
): Closeable {111
var resource: Closeable? = None112
var active = true113
public override fun close() {114
owner.checkThread()115
if (!active)116
return117
active = false118
val previous = resource119
resource = None120
owner.remove(this)121
previous?.close()122
}123
}125
public fun ApplicationScope.notificationController(126
center: NotificationCenter127
): NativeNotificationController =128
resource { NativeNotificationController(center, { isMounted }, { action -> dispatch(action) }) }130
public fun ComponentScope.notificationController(131
center: NotificationCenter132
): NativeNotificationController =133
resource { NativeNotificationController(center, { isMounted }, { action -> dispatch(action) }) }