diff options
| author | Fuwn <[email protected]> | 2025-02-10 04:59:47 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-02-10 04:59:47 -0800 |
| commit | fcddfd8b8daf543be908ccf69b751788dee2b13f (patch) | |
| tree | 45d631aedd60238e08139d80828c0044ab08cd79 | |
| parent | docs: add a README (diff) | |
| download | holobar-fcddfd8b8daf543be908ccf69b751788dee2b13f.tar.xz holobar-fcddfd8b8daf543be908ccf69b751788dee2b13f.zip | |
feat(HoloBarApp): automatic refresh on date change
| -rw-r--r-- | HoloBar/HoloBarApp.swift | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/HoloBar/HoloBarApp.swift b/HoloBar/HoloBarApp.swift index 39250fd..a3b45e7 100644 --- a/HoloBar/HoloBarApp.swift +++ b/HoloBar/HoloBarApp.swift @@ -6,29 +6,44 @@ struct HoloBarApp: App { var body: some Scene { MenuBarExtra { - if fetcher.characters.isEmpty { - Text("Loading …") - } else { - ForEach(fetcher.characters) { character in - Button(character.name) { - NSWorkspace.shared.open(character.profileURL) + VStack { + if fetcher.characters.isEmpty { + Text("Loading …") + } else { + ForEach(fetcher.characters) { character in + Button(character.name) { + NSWorkspace.shared.open(character.profileURL) + } } } - } - - Divider() - Button("Refresh") { - let today = Calendar.current.dateComponents([.month, .day], from: Date()) - if let month = today.month, let day = today.day { - fetcher.fetchCharacters(for: month, day: day) + Divider() + #if DEBUG + Button("Simulate Day Change") { + NotificationCenter.default.post(name: .NSCalendarDayChanged, object: nil) + } + #endif + Button("Refresh") { + refreshCharacters() + } + Button("Quit") { + NSApplication.shared.terminate(nil) } } - Button("Quit") { - NSApplication.shared.terminate(nil) + .onReceive(NotificationCenter.default.publisher(for: .NSCalendarDayChanged)) { _ in + refreshCharacters() } } label: { Text("HL") } } + + private func refreshCharacters() { + let today = Calendar.current.dateComponents([.month, .day], from: Date()) + + if let month = today.month, let day = today.day { + fetcher.characters.removeAll() + fetcher.fetchCharacters(for: month, day: day) + } + } } |