aboutsummaryrefslogtreecommitdiff
path: root/HoloBar/HoloBarApp.swift
blob: 699d13584c741629d09d1a40bdfbe61e66062196 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import SwiftUI

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

  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 {
                if showAvatars {
                  AsyncImage(url: character.avatarURL) { phase in
                    if let image = phase.image {
                      image
                        .resizable()
                        .scaledToFill()
                    } else if phase.error != nil {
                      Image(systemName: "person.crop.circle.badge.exclamationmark")
                        .resizable()
                        .scaledToFit()
                        .foregroundStyle(.gray)
                    } else {
                      Image(systemName: "person.crop.circle")
                        .resizable()
                        .scaledToFit()
                        .foregroundStyle(.gray)
                    }
                  }
                }

                Text(character.name)
              }
            }
          }
        }

        Divider()

        #if DEBUG
          Button("Simulate Day Change") {
            NotificationCenter.default.post(name: .NSCalendarDayChanged, object: nil)
          }
        #endif

        Button("\(showAvatars ? "Hide" : "Show") Avatars") {
          showAvatars.toggle()
        }

        Button("Refresh", action: 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)
    }
  }
}