Update badge counts clientside because we can't count on pushes to remove badges
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { onNextEventLoopTick } from "helpers/timing_helpers"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "unread" ]
|
||||
static classes = [ "unread" ]
|
||||
|
||||
connect() {
|
||||
onNextEventLoopTick(() => this.update())
|
||||
}
|
||||
|
||||
update() {
|
||||
onNextEventLoopTick(() => {
|
||||
if (this.#available) {
|
||||
const unreadCount = this.#unreadCount
|
||||
|
||||
console.log(unreadCount, this.unreadTargets)
|
||||
|
||||
if (unreadCount > 0) {
|
||||
navigator.setAppBadge(unreadCount)
|
||||
} else {
|
||||
navigator.clearAppBadge()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
clear() {
|
||||
onNextEventLoopTick(() => {
|
||||
if (this.#available) {
|
||||
navigator.clearAppBadge()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
get #unreadCount() {
|
||||
return this.unreadTargets.filter(unreadTarget => unreadTarget.classList.contains(this.unreadClass)).length
|
||||
}
|
||||
|
||||
get #available() {
|
||||
return "setAppBadge" in navigator
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user