Skip to content

Commit 0b74ff4

Browse files
State manager
1 parent dcb18e9 commit 0b74ff4

19 files changed

Lines changed: 1568 additions & 181 deletions

src/core/AppStateManager.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,40 @@ import {
22
TrayService,
33
SetupStatus as TraySetupStatus,
44
} from "../services/TrayService";
5+
import { appStore, SetupStatus } from "./AppStore";
56

6-
export type SetupStatus =
7-
| "idle"
8-
| "downloading-models"
9-
| "setting-up-whisper"
10-
| "preparing-app"
11-
| "checking-permissions"
12-
| "starting-server"
13-
| "loading-windows"
14-
| "initializing-plugins"
15-
| "service-ready";
7+
export { SetupStatus } from "./AppStore";
168

179
export class AppStateManager {
18-
private currentSetupStatus: SetupStatus = "idle";
19-
private setupStatusCallbacks: ((status: SetupStatus) => void)[] = [];
2010
private trayService: TrayService | null = null;
11+
private unsubscribe: (() => void) | null = null;
12+
13+
constructor() {
14+
this.unsubscribe = appStore.subscribe(
15+
(state) => state.app.status,
16+
(status) => {
17+
this.trayService?.updateTrayMenu(status as TraySetupStatus);
18+
}
19+
);
20+
}
2121

2222
setTrayService(trayService: TrayService) {
2323
this.trayService = trayService;
2424
}
2525

2626
setSetupStatus(status: SetupStatus) {
27-
this.currentSetupStatus = status;
28-
this.setupStatusCallbacks.forEach((callback) => callback(status));
29-
this.trayService?.updateTrayMenu(status as TraySetupStatus);
27+
appStore.setAppStatus(status);
3028
}
3129

3230
getCurrentStatus(): SetupStatus {
33-
return this.currentSetupStatus;
31+
return appStore.select((state) => state.app.status);
3432
}
3533

36-
onSetupStatusChange(callback: (status: SetupStatus) => void) {
37-
this.setupStatusCallbacks.push(callback);
34+
onSetupStatusChange(callback: (status: SetupStatus) => void): () => void {
35+
return appStore.subscribe(
36+
(state) => state.app.status,
37+
(status) => callback(status)
38+
);
3839
}
3940

4041
getStatusMessage(status: SetupStatus): string {
@@ -51,13 +52,28 @@ export class AppStateManager {
5152
return "Starting server...";
5253
case "loading-windows":
5354
return "Loading windows...";
55+
case "initializing-plugins":
56+
return "Initializing plugins...";
57+
case "service-ready":
58+
return "Ready";
5459
case "idle":
5560
default:
5661
return "WhisperMac - AI Dictation";
5762
}
5863
}
5964

6065
isIdle(): boolean {
61-
return this.currentSetupStatus === "idle";
66+
return appStore.select((state) => state.app.status) === "idle";
67+
}
68+
69+
isServiceReady(): boolean {
70+
return appStore.select((state) => state.app.status) === "service-ready";
71+
}
72+
73+
destroy(): void {
74+
if (this.unsubscribe) {
75+
this.unsubscribe();
76+
this.unsubscribe = null;
77+
}
6278
}
6379
}

0 commit comments

Comments
 (0)