@@ -21,18 +21,6 @@ import { setTimer, clearTimer } from 'native-timer';
2121import { configService } from '../ConfigService' ;
2222import { backgroundRuntimeState } from '../BackgroundRuntimeState' ;
2323
24- /**
25- * 由 App 层注入的生命周期回调接口。
26- * 将 native 事件(ForegroundService stop/tempStop)中转到 UI 状态层(settingsStore),
27- * 使 BackgroundServiceManager 本身不依赖任何 Zustand store。
28- */
29- export interface BackgroundServiceLifecycleCallbacks {
30- /** 用户从通知栏点击「停止」时触发,应将 enableBackgroundTasks 设为 false */
31- onForegroundServiceStop ( ) : void ;
32- /** 用户从通知栏点击「临时停止」时触发,应设置 isTempDisabledBackgroundTasks */
33- onForegroundServiceTempStop ( ) : void ;
34- }
35-
3624class BackgroundServiceManager {
3725 private static instance : BackgroundServiceManager | null = null ;
3826
@@ -45,9 +33,6 @@ class BackgroundServiceManager {
4533 /** 取消对 backgroundRuntimeState 的订阅 */
4634 private runtimeUnsub : ( ( ) => void ) | null = null ;
4735
48- /** 由 App 层注入的生命周期回调 */
49- private lifecycleCallbacks : BackgroundServiceLifecycleCallbacks | null = null ;
50-
5136 private constructor ( ) { }
5237
5338 static getInstance ( ) : BackgroundServiceManager {
@@ -57,15 +42,6 @@ class BackgroundServiceManager {
5742 return BackgroundServiceManager . instance ;
5843 }
5944
60- /**
61- * 注入 App 层生命周期回调(应在 start() 之前调用)。
62- * 若未注入则 ForegroundService 的 stop/tempStop 事件不会更新 store,
63- * 但 BackgroundServiceManager 自身逻辑仍正常工作。
64- */
65- setLifecycleCallbacks ( callbacks : BackgroundServiceLifecycleCallbacks ) : void {
66- this . lifecycleCallbacks = callbacks ;
67- }
68-
6945 // ─── 工具 ───────────────────────────────────────────────
7046
7147 private async getShouldRunBackground ( ) : Promise < boolean > {
@@ -219,11 +195,12 @@ class BackgroundServiceManager {
219195 ForegroundService . startService ( ) ;
220196
221197 this . stopSub = ForegroundService . addStopListener ( ( ) => {
222- this . lifecycleCallbacks ?. onForegroundServiceStop ( ) ;
198+ configService . updateConfig ( { enableBackgroundTasks : false } ) . catch ( ( e ) => {
199+ console . error ( '[BackgroundServiceManager] Failed to disable background tasks:' , e ) ;
200+ } ) ;
223201 } ) ;
224202 this . tempStopSub = ForegroundService . addTempStopListener ( ( ) => {
225203 backgroundRuntimeState . setTempDisabled ( true ) ;
226- this . lifecycleCallbacks ?. onForegroundServiceTempStop ( ) ;
227204 } ) ;
228205 } catch ( e ) {
229206 console . error ( '[BackgroundServiceManager] Failed to start foreground service:' , e ) ;
@@ -254,11 +231,12 @@ class BackgroundServiceManager {
254231 if ( config ?. enableForegroundNotification && ! isRunning ) {
255232 ForegroundService . startService ( ) ;
256233 this . stopSub = ForegroundService . addStopListener ( ( ) => {
257- this . lifecycleCallbacks ?. onForegroundServiceStop ( ) ;
234+ configService . updateConfig ( { enableBackgroundTasks : false } ) . catch ( ( e ) => {
235+ console . error ( '[BackgroundServiceManager] Failed to disable background tasks:' , e ) ;
236+ } ) ;
258237 } ) ;
259238 this . tempStopSub = ForegroundService . addTempStopListener ( ( ) => {
260239 backgroundRuntimeState . setTempDisabled ( true ) ;
261- this . lifecycleCallbacks ?. onForegroundServiceTempStop ( ) ;
262240 } ) ;
263241 } else if ( ! config ?. enableForegroundNotification && isRunning ) {
264242 this . _cleanupListeners ( ) ;
0 commit comments