aboutsummaryrefslogtreecommitdiff
path: root/HoloBar/HoloBarApp.swift
blob: eb28f7f7c546ad780056b1652f6d911721697d89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import SwiftUI

@main
struct HoloBarApp: App {
    @StateObject private var fetcher = CharacterFetcher()

    var body: some Scene {
        MenuBarExtra {
            VStack {
                if fetcher.characters.isEmpty {
                    Text("Loading …")
                } else {
                    ForEach(fetcher.characters) { character in
                        Button(action: {
                            NSWorkspace.shared.open(character.profileURL)
                        }) {
                            HStack {
                                AsyncImage(url: character.avatarURL, content: { $0 }, placeholder: {
                                    Image(systemName: "person.crop.circle")
                                })

                                Text(character.name)
                            }
                        }
                    }
                }

                Divider()
                #if DEBUG
                    Button("Simulate Day Change") {
                        NotificationCenter.default.post(name: .NSCalendarDayChanged, object: nil)
                    }
                #endif
                Button("Refresh") {
                    refreshCharacters()
                }
                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)
        }
    }
}