@@ -12,50 +12,20 @@ import { fetchLessons } from "./data/Lessons";
12
12
import { fetchAttendance } from "./data/Attendance" ;
13
13
import { fetchEvaluation } from "./data/Evaluation" ;
14
14
import { papillonNotify } from "./Notifications" ;
15
-
16
15
import { useFlagsStore } from "@/stores/flags" ;
17
16
18
- const notifeeEvent = async ( ) => {
19
- const notifee = ( await import ( "@notifee/react-native" ) ) . default ;
20
- const EventType = ( await import ( "@notifee/react-native" ) ) . EventType ;
21
-
22
- // Gestion des badges quand app en arrière-plan
23
- notifee . onBackgroundEvent ( async ( { type, detail } ) => {
24
- const { notification, pressAction } = detail ;
25
-
26
- switch ( type ) {
27
- case EventType . ACTION_PRESS :
28
- console . log ( `[Notifee] Action press: ${ pressAction ?. id } ` ) ;
29
- break ;
30
- case EventType . DISMISSED :
31
- let badgeCount = await notifee . getBadgeCount ( ) ;
32
- badgeCount = Math . max ( badgeCount - 1 , 0 ) ;
33
- await notifee . setBadgeCount ( badgeCount ) ;
34
- break ;
35
- }
36
- } ) ;
17
+ let isBackgroundFetchRunning = false ;
18
+ const BACKGROUND_TASK_NAME = "background-fetch" ;
37
19
38
- // Gestion des badges quand app en premier plan
39
- notifee . onForegroundEvent ( async ( { type, detail } ) => {
40
- const { notification, pressAction } = detail ;
41
-
42
- switch ( type ) {
43
- case EventType . ACTION_PRESS :
44
- console . log ( `[Notifee] Action press: ${ pressAction ?. id } ` ) ;
45
- break ;
46
- case EventType . DISMISSED :
47
- let badgeCount = await notifee . getBadgeCount ( ) ;
48
- badgeCount = Math . max ( badgeCount - 1 , 0 ) ;
49
- await notifee . setBadgeCount ( badgeCount ) ;
50
- break ;
51
- }
52
- } ) ;
20
+ const fetch = async ( label : string , fn : ( ) => Promise < any > ) => {
21
+ try {
22
+ info ( `▶️ Running background ${ label } ` , "BACKGROUND" ) ;
23
+ await fn ( ) ;
24
+ } catch ( e ) {
25
+ error ( `❌ ${ label } fetch failed: ${ e } ` , "BACKGROUND" ) ;
26
+ }
53
27
} ;
54
28
55
- if ( ! isExpoGo ( ) ) notifeeEvent ( ) ;
56
-
57
- let isBackgroundFetchRunning = false ;
58
-
59
29
const backgroundFetch = async ( ) => {
60
30
const disableBackgroundTasks = useFlagsStore . getState ( ) . defined ( "disablebackgroundtasks" ) ;
61
31
if ( disableBackgroundTasks ) {
@@ -99,18 +69,12 @@ const backgroundFetch = async () => {
99
69
account . personalization . notifications ;
100
70
101
71
if ( notificationsTypesPermissions ?. enabled ) {
102
- info ( "▶️ Running background News" , "BACKGROUND" ) ;
103
- await fetchNews ( ) ;
104
- info ( "▶️ Running background Homeworks" , "BACKGROUND" ) ;
105
- await fetchHomeworks ( ) ;
106
- info ( "▶️ Running background Grades" , "BACKGROUND" ) ;
107
- await fetchGrade ( ) ;
108
- info ( "▶️ Running background Lessons" , "BACKGROUND" ) ;
109
- await fetchLessons ( ) ;
110
- info ( "▶️ Running background Attendance" , "BACKGROUND" ) ;
111
- await fetchAttendance ( ) ;
112
- info ( "▶️ Running background Evaluation" , "BACKGROUND" ) ;
113
- await fetchEvaluation ( ) ;
72
+ await fetch ( "News" , fetchNews ) ;
73
+ await fetch ( "Homeworks" , fetchHomeworks ) ;
74
+ await fetch ( "Grades" , fetchGrade ) ;
75
+ await fetch ( "Lessons" , fetchLessons ) ;
76
+ await fetch ( "Attendance" , fetchAttendance ) ;
77
+ await fetch ( "Evaluation" , fetchEvaluation ) ;
114
78
}
115
79
}
116
80
@@ -127,14 +91,14 @@ const backgroundFetch = async () => {
127
91
}
128
92
} ;
129
93
130
- if ( ! isExpoGo ( ) ) TaskManager . defineTask ( "background-fetch" , backgroundFetch ) ;
94
+ if ( ! isExpoGo ( ) ) TaskManager . defineTask ( BACKGROUND_TASK_NAME , backgroundFetch ) ;
131
95
132
96
const unsetBackgroundFetch = async ( ) =>
133
- await BackgroundFetch . unregisterTaskAsync ( "background-fetch" ) ;
97
+ await BackgroundFetch . unregisterTaskAsync ( BACKGROUND_TASK_NAME ) ;
134
98
135
99
const setBackgroundFetch = async ( ) =>
136
- await BackgroundFetch . registerTaskAsync ( "background-fetch" , {
137
- minimumInterval : 60 * 15 ,
100
+ await BackgroundFetch . registerTaskAsync ( BACKGROUND_TASK_NAME , {
101
+ minimumInterval : 60 * ( __DEV__ ? 1 : 15 ) ,
138
102
stopOnTerminate : false ,
139
103
startOnBoot : true ,
140
104
} ) ;
@@ -152,11 +116,11 @@ const registerBackgroundTasks = async () => {
152
116
}
153
117
154
118
const isRegistered = await TaskManager . isTaskRegisteredAsync (
155
- "background-fetch"
119
+ BACKGROUND_TASK_NAME
156
120
) ;
157
121
158
122
if ( isRegistered ) {
159
- warn (
123
+ info (
160
124
"⚠️ Background task already registered. Unregister background task..." ,
161
125
"BACKGROUND"
162
126
) ;
0 commit comments