Skip to content

Commit

Permalink
Add notification manager for web
Browse files Browse the repository at this point in the history
  • Loading branch information
JSMonk committed May 17, 2024
1 parent c420a4f commit 93a04b0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun Partner(controller: AppController, partner: Partner) {
AboutConfTopBanner(partner)
HDivider()

Column(Modifier.padding(16.dp)) {
Column(Modifier.padding(16.dp).fillMaxWidth()) {
Text(
stringResource(partner.title), style = MaterialTheme.typography.h2.copy(
color = MaterialTheme.colors.greyWhite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import kotlinconfapp.shared.generated.resources.*
import kotlinconfapp.shared.generated.resources.Res
import kotlinconfapp.shared.generated.resources.close
import kotlinconfapp.shared.generated.resources.speakers
import kotlinconfapp.shared.generated.resources.talks
import org.jetbrains.compose.resources.ExperimentalResourceApi
Expand Down Expand Up @@ -82,11 +82,11 @@ fun SearchScreen(
selectedTab = it
}
)
Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
Row(horizontalArrangement = Arrangement.Start, modifier = Modifier.fillMaxWidth()) {
IconButton(onClick = { controller.back() }) {
Icon(
painter = Res.drawable.close.painter(),
"Close",
painter = Res.drawable.back.painter(),
"Back",
tint = MaterialTheme.colors.greyGrey5
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
package org.jetbrains.kotlinconf

// TODO implement
private external object Notification {
fun requestPermission(callback: (String) -> Unit)
}

private fun registerNotificationByServiceWorker(delay: Long, title: String, message: String): Unit =
js(
"""{
if (typeof navigator === "undefined" || !navigator.serviceWorker?.ready) return;
navigator.serviceWorker.ready.then((registration) => {
registration.active.postMessage({
command: 'register-notification',
title: title,
body: message,
delay: Number(delay),
});
});
}"""
)

private fun cancelNotificationByServiceWorker(title: String): Unit =
js(
"""{
if (typeof navigator === "undefined" || !navigator.serviceWorker?.ready) return;
navigator.serviceWorker.ready.then((registration) => {
registration.active.postMessage({
command: 'cancel-notification',
title: title,
});
});
}"""
)

actual class NotificationManager actual constructor(
context: ApplicationContext
) {
private var notificationAllowed = false

actual fun requestPermission() {
Notification.requestPermission {
if (it == "granted") {
notificationAllowed = true
} else {
notificationAllowed = false
}
}
}

actual fun schedule(delay: Long, title: String, message: String): String? {
return null
if (!notificationAllowed) return null

registerNotificationByServiceWorker(delay, title, message)
return title
}

actual fun cancel(title: String) {
if (!notificationAllowed) return
cancelNotificationByServiceWorker(title)
}

}
}
2 changes: 1 addition & 1 deletion shared/src/webMain/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
}
.github-link {
position: absolute;
right: 30px;
right: 0;
width: 37px;
height: 37px;
margin-left: 0;
Expand Down
23 changes: 22 additions & 1 deletion shared/src/webMain/resources/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,25 @@ self.addEventListener('activate', event => {
)
)
);
});
});

const map = new Map()

self.addEventListener('message', function(event) {
console.log('Service worker received a message: ', event.data);

if (event.data.command === 'register-notification') {
const id = setTimeout(() => {
self.registration.showNotification(event.data.title, { body: event.data.body });
map.delete(event.data.title)
}, event.data.delay)
map.set(event.data.title, id)
}
if (event.data.command === 'cancel-notification') {
const id = map.get(event.data.title)
if (id != undefined) {
clearTimeout(id)
map.delete(event.data.title)
}
}
});

0 comments on commit 93a04b0

Please sign in to comment.