1+ import { info } from "@tauri-apps/plugin-log" ;
12import { isPermissionGranted , requestPermission , sendNotification } from "@tauri-apps/plugin-notification" ;
3+ import { NOTIFICATIONS_ENABLED } from "~/constants" ;
24
35export const useNotifications = ( ) => {
6+ const _notificationsEnabled = ref ( false ) ;
7+
8+ const getValue = async ( ) : Promise < void > => {
9+ const value = await getStorageItem < boolean > ( NOTIFICATIONS_ENABLED )
10+ _notificationsEnabled . value = value ?? false ;
11+ }
12+
13+ const setValue = async ( value : boolean ) : Promise < void > => {
14+ await saveStorageItem ( NOTIFICATIONS_ENABLED , value )
15+ _notificationsEnabled . value = value ;
16+
17+ if ( value ) {
18+ let permissionGranted = await isPermissionGranted ( ) ;
19+
20+ if ( ! permissionGranted ) {
21+ const permission = await requestPermission ( ) ;
22+ permissionGranted = permission === 'granted' ;
23+ }
24+ }
25+ send ( { title : 'Notifications enabled' , body : 'You will now receive notifications' } ) ;
26+ info ( `Option for enabling notifications: ${ _notificationsEnabled . value } ` ) ;
27+ }
28+
429 const send = async ( options ) => {
30+ if ( ! notificationsEnabled . value ) {
31+ return ;
32+ }
33+
534 let permissionGranted = await isPermissionGranted ( ) ;
635
736 if ( ! permissionGranted ) {
@@ -14,7 +43,17 @@ export const useNotifications = () => {
1443 }
1544 } ;
1645
46+ const notificationsEnabled = computed ( {
47+ get : ( ) => _notificationsEnabled . value ,
48+ set : ( value : boolean ) => setValue ( value ) ,
49+ } ) ;
50+
51+ onMounted ( ( ) => {
52+ getValue ( ) ;
53+ } ) ;
54+
1755 return {
56+ notificationsEnabled,
1857 send
1958 }
2059}
0 commit comments