A macOS utility to track home-manager JJ repo status
1import Foundation
2import HMStatus
3import SwiftUI
4
5/// Orchestrates periodic jj status checks and publishes results for the UI.
6@MainActor
7final class AppStatusChecker: ObservableObject {
8 @Published private(set) var status: RepoStatus
9
10 let repoPath: String
11 let refreshInterval: TimeInterval
12 private let runner: JujutsuCommandRunner
13 private var timer: Timer?
14
15 init(
16 repoPath: String,
17 refreshInterval: TimeInterval = 300, // 5 minutes
18 runner: JujutsuCommandRunner = LiveJujutsuCommandRunner()
19 ) {
20 self.repoPath = repoPath
21 self.refreshInterval = refreshInterval
22 self.runner = runner
23 self.status = RepoStatus(
24 ahead: 0, behind: 0, dirty: false,
25 error: "Not yet checked",
26 checkedAt: Date()
27 )
28 }
29
30 /// Start periodic status checks.
31 func startPolling() {
32 // Run immediately
33 Task { await refresh() }
34
35 // Then schedule periodic checks
36 timer = Timer.scheduledTimer(withTimeInterval: refreshInterval, repeats: true) {
37 [weak self] _ in
38 guard let self = self else { return }
39 Task { @MainActor in
40 await self.refresh()
41 }
42 }
43 }
44
45 /// Stop periodic status checks.
46 func stopPolling() {
47 timer?.invalidate()
48 timer = nil
49 }
50
51 /// Perform a single status check.
52 func refresh() async {
53 status = await StatusChecker.check(repoPath: repoPath, runner: runner)
54 }
55}