aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Views/TrackerView.swift
blob: 83a4ebf3af79439cb11aa5ef7d174e8bfd189e16 (plain)
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
77
78
79
80
81
82
83
84
import SwiftUI

struct TrackerView: View {
  
  //  @Environment(\.modelContext) private var modelContext
  //  @Query private var items: [Item]
  
  @Environment(HotlineState.self) private var appState
  @Environment(HotlineClient.self) private var hotline
  @Environment(HotlineTrackerClient.self) private var tracker
  
  @State private var selectedServer: HotlineServer?
  
  func shouldDisplayDescription(server: HotlineServer) -> Bool {
    guard let name = server.name, let desc = server.description else {
      return false
    }
    
    return desc.count > 0 && desc != name && !desc.contains(/^-+/)
  }
  
  var body: some View {
    @Bindable var config = appState
    
    List(selection: $selectedServer) {
      ForEach(tracker.servers) { server in
        NavigationLink {
          ServerView(server: server)
        } label: {
          HStack(alignment: .firstTextBaseline) {
            Text("🌎").font(.title3)
            VStack(alignment: .leading) {
              Text(server.name!).font(.title3).fontWeight(.medium)
              if shouldDisplayDescription(server: server) {
                Text(server.description!).opacity(0.6).font(.title3)
              }
            }
          }
        }
      }
    }
    .background(Color.white)
    .listStyle(.plain)
    .listRowSpacing(1)
    .frame(maxWidth: .infinity)
    .task {
      tracker.fetch()
    }
    //      .sheet(item: $selectedServer) { item in
    ////        Text("HELLO")
    //        TrackerServerView(server: item)
    //      }
    .refreshable {
      await withCheckedContinuation { continuation in
        tracker.fetch() {
          continuation.resume()
        }
      }
    }
    .navigationTitle("Tracker")
    .navigationBarTitleDisplayMode(.inline)
    .toolbar {
      ToolbarItem(placement: .topBarTrailing) {
        Button {
          config.dismissTracker()
        } label: {
          Image(systemName: "xmark.circle.fill")
            .symbolRenderingMode(.hierarchical)
            .foregroundColor(.gray)
        }
      }
    }
    
    //    .interactiveDismissDisabled()
  }
}

#Preview {
  TrackerView()
    .environment(HotlineClient())
    .environment(HotlineTrackerClient(tracker: HotlineTracker("hltracker.com")))
    .environment(HotlineState())
  //    .modelContainer(for: Item.self, inMemory: true)
}