Skip to content

Commit a639ada

Browse files
committed
Use new unreadCountStore for setting appIcon Badge
Signed-off-by: Markus Stoll <[email protected]>
1 parent 9f4342c commit a639ada

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

src/main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ ipcMain.handle('app:getSystemL10n', () => ({
8585
}))
8686
ipcMain.handle('app:enableWebRequestInterceptor', (event, ...args) => enableWebRequestInterceptor(...args))
8787
ipcMain.handle('app:disableWebRequestInterceptor', (event, ...args) => disableWebRequestInterceptor(...args))
88-
ipcMain.handle('app:setBadgeCount', async (event, count) => app.setBadgeCount(count))
88+
ipcMain.handle('app:setBadgeCount', async (event, count) => {
89+
return app.setBadgeCount(count)
90+
})
8991
ipcMain.on('app:relaunch', () => {
9092
app.relaunch()
9193
app.exit(0)

src/talk/renderer/TalkDesktop.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
-->
55

66
<script setup lang="ts">
7-
import { provide, ref } from 'vue'
7+
import { provide, ref, onMounted } from 'vue'
88
import TitleBar from './TitleBar/TitleBar.vue'
99
import TalkWrapper from './TalkWrapper/TalkWrapper.vue'
1010
import { createViewer } from './Viewer/Viewer.js'
1111
import { useNotificationsStore } from './notifications/notifications.store.js'
12+
import unreadCountStore from '../../../spreed/src/store/unreadCountStore.js'
1213

1314
const isTalkInitialized = ref(false)
1415
provide('talk:isInitialized', isTalkInitialized)

src/talk/renderer/TalkWrapper/TalkWrapper.vue

+66-19
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,85 @@
44
-->
55

66
<script setup lang="ts">
7-
import { onMounted } from 'vue'
7+
import { onMounted, onUnmounted } from 'vue'
88
import { onTalkHashDirty, onTalkHashUpdate, openConversation, setTalkHash } from './talk.service.ts'
99
import { registerTalkDesktopSettingsSection } from '../Settings/index.ts'
1010
import { subscribeBroadcast } from '../../../shared/broadcast.service.ts'
1111
import { appData } from '../../../app/AppData.js'
12+
import unreadCountStore, { EVENTS } from '@talk/src/store/unreadCountStore'
13+
import { subscribe, emit as emitEvent } from '@nextcloud/event-bus'
1214

1315
const emit = defineEmits<{
1416
(event: 'ready'): void
1517
}>()
1618

19+
// Handler for unread count updates
20+
let unsubscribeUnreadCount: (() => void) | null = null
21+
22+
const waitForTalkAndStore = async () => {
23+
console.debug('TalkWrapper: Waiting for Talk instance and store...')
24+
let attempts = 0
25+
const maxAttempts = 50
26+
const delay = 100
27+
28+
while (attempts < maxAttempts) {
29+
if (window.OCA?.Talk?.instance && window.store && window.TALK_DESKTOP) {
30+
console.debug('TalkWrapper: Talk instance, store, and TALK_DESKTOP found')
31+
return true
32+
}
33+
await new Promise(resolve => setTimeout(resolve, delay))
34+
attempts++
35+
}
36+
throw new Error('Talk instance, store, or TALK_DESKTOP not initialized after maximum attempts')
37+
}
38+
1739
onMounted(async () => {
18-
// Importing the main Talk entry point mounts a Vue app to the #content
19-
await import('@talk/src/main.js')
40+
try {
41+
await import('@talk/src/main.js')
42+
43+
registerTalkDesktopSettingsSection()
44+
subscribeBroadcast('talk:conversation:open', ({ token, directCall }) => openConversation(token, { directCall }))
45+
46+
await waitForTalkAndStore()
47+
const store = window.store
48+
49+
// Register store module if not already registered
50+
if (!store.hasModule('unreadCount')) {
51+
store.registerModule('unreadCount', unreadCountStore)
52+
}
2053

21-
// Additional integrations
22-
registerTalkDesktopSettingsSection()
23-
subscribeBroadcast('talk:conversation:open', ({ token, directCall }) => openConversation(token, { directCall }))
54+
// Initialize badge handling
55+
window.TALK_DESKTOP.setBadgeCount(0)
56+
57+
// Subscribe to unread count updates
58+
unsubscribeUnreadCount = subscribe(EVENTS.UNREAD_COUNT_UPDATED, ({ unreadMessages }) => {
59+
window.TALK_DESKTOP.setBadgeCount(unreadMessages || 0)
60+
})
61+
62+
// Now trigger the initial calculation
63+
store.dispatch('unreadCount/recalculateTotalUnreadCounters')
64+
65+
if (appData.talkHash) {
66+
setTalkHash(appData.talkHash)
67+
}
68+
onTalkHashUpdate((hash: string) => {
69+
appData.setTalkHash(hash).persist()
70+
})
71+
onTalkHashDirty(() => {
72+
appData.setTalkHashDirty(true).persist()
73+
})
74+
75+
emit('ready')
76+
} catch (error) {
77+
console.error('TalkWrapper: Error during initialization:', error)
78+
}
79+
})
2480

25-
// If there is a talkHash - set it initially
26-
if (appData.talkHash) {
27-
setTalkHash(appData.talkHash)
81+
// Cleanup event listeners
82+
onUnmounted(() => {
83+
if (unsubscribeUnreadCount) {
84+
unsubscribeUnreadCount()
2885
}
29-
// Handle Talk Hash updates
30-
onTalkHashUpdate((hash: string) => {
31-
appData.setTalkHash(hash).persist()
32-
})
33-
onTalkHashDirty(() => {
34-
appData.setTalkHashDirty(true).persist()
35-
})
36-
37-
// Ready
38-
emit('ready')
3986
})
4087
</script>
4188

src/talk/renderer/notifications/notifications.store.js

-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export function createNotificationStore() {
9494
if (notifications.length > _oldcount) {
9595
_oldcount = notifications.length
9696
if (state.backgroundFetching && document.hidden) {
97-
window.TALK_DESKTOP.setBadgeCount()
9897
window.TALK_DESKTOP.flashAppIcon(true)
9998
// If we didn't already highlight, store the title so we can restore on tab-view
10099
if (!document.title.startsWith('* ')) {
@@ -110,7 +109,6 @@ export function createNotificationStore() {
110109
* the Talk might have altered it.
111110
*/
112111
function _restoreTitle() {
113-
window.TALK_DESKTOP.setBadgeCount(0)
114112
window.TALK_DESKTOP.flashAppIcon(false)
115113
if (document.title.startsWith('* ')) {
116114
document.title = document.title.substring(2)

0 commit comments

Comments
 (0)