aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-02-10 04:59:47 -0800
committerFuwn <[email protected]>2025-02-10 04:59:47 -0800
commitfcddfd8b8daf543be908ccf69b751788dee2b13f (patch)
tree45d631aedd60238e08139d80828c0044ab08cd79
parentdocs: add a README (diff)
downloadholobar-fcddfd8b8daf543be908ccf69b751788dee2b13f.tar.xz
holobar-fcddfd8b8daf543be908ccf69b751788dee2b13f.zip
feat(HoloBarApp): automatic refresh on date change
-rw-r--r--HoloBar/HoloBarApp.swift45
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)
+ }
+ }
}