scoop:scoop.desktop:0.1.0
src/notification/events.scoop
Selected production source · src/notification/events.scoop
1
package scoop.desktop.notification3
import scoop.Result4
import scoop.window.WindowErrorKind5
import scoop.window.WindowException7
internal fun replyError(reply: NotificationReply): NotificationError =8
NotificationError(9
when (reply.data.status) {10
1 -> NotificationErrorKind.Unsupported11
2 -> NotificationErrorKind.PermissionRequired12
3 -> NotificationErrorKind.Disabled13
4 -> NotificationErrorKind.NotConfigured14
5 -> NotificationErrorKind.Unavailable15
6 -> NotificationErrorKind.InvalidContent16
else -> NotificationErrorKind.NativeFailure17
},18
if (reply.data.code != 0L)19
Some(reply.data.code)20
else21
None,22
if (reply.message.byteLength != 0L)23
Some(reply.message)24
else25
None26
)28
internal fun settingsReply(reply: NotificationReply): Result<29
NotificationSettings,30
NotificationError31
> {32
if (reply.data.status != 0)33
return Result.Failure<NotificationSettings, NotificationError>(replyError(reply))34
val availability =35
when (reply.data.detail) {36
0 -> NotificationAvailability.Available37
1 -> NotificationAvailability.PermissionRequired38
2 -> NotificationAvailability.Disabled39
3 -> NotificationAvailability.NotConfigured40
else -> NotificationAvailability.ServiceUnavailable41
}42
val flags = reply.data.capabilities43
return Result.Success<NotificationSettings, NotificationError>(44
NotificationSettings(45
availability,46
NotificationCapabilities(47
flags.and(1) != 0,48
flags.and(2) != 0,49
flags.and(4) != 0,50
flags.and(8) != 0,51
flags.and(16) != 0,52
flags.and(32) != 053
)54
)55
)56
}58
internal fun eventReply(reply: NotificationReply): NotificationEvent {59
val id = NotificationId(reply.id)60
return when (reply.data.kind) {61
5 -> NotificationEvent.Activated(62
id,63
if (reply.action.byteLength == 0L)64
None65
else66
Some(NotificationActionId(reply.action)),67
reply.payload68
)69
6 -> NotificationEvent.Closed(70
id,71
when (reply.data.detail) {72
1 -> NotificationCloseReason.Expired73
2 -> NotificationCloseReason.Dismissed74
3 -> NotificationCloseReason.Withdrawn75
else -> NotificationCloseReason.Unknown76
}77
)78
7 -> NotificationEvent.Failed(id, replyError(reply))79
else -> throw WindowException("invalid notification event", WindowErrorKind.NativeFailure)80
}81
}