aboutsummaryrefslogtreecommitdiff
path: root/Hotline/TrackerView.swift
blob: 04498b1299701d832cd86de05ee51856ebda70f4 (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
import SwiftUI
import SwiftData

struct TrackerView: View {
  @Environment(\.modelContext) private var modelContext
  @Query private var items: [Item]
  
  @StateObject var tracker = HotlineTracker(address: "hltracker.com", callback: { s in
    print("ALL DONE")
  })
  
  var body: some View {
    ScrollView {
      VStack(alignment: .leading) {
        ForEach(tracker.servers) { server in
          HStack {
            VStack(alignment: .leading) {
              Text(server.name!).bold()
              Image(systemName: "person.fill").opacity(0.3)
              Text("\(server.users)").font(.system(size: 12))
              Text(server.description!).foregroundColor(.secondary).font(.system(size: 14))
            }
            .padding(EdgeInsets(top: 15, leading: 18, bottom: 15, trailing: 18))
            Spacer()
          }
          .background(.white)
          .cornerRadius(16)
          .padding(EdgeInsets(top: 5, leading: 8, bottom: 5, trailing: 8))
          .frame(minWidth: 0, maxWidth: .infinity)
        }
      }
      .padding()
    }
    .background(Color(white: 0.9))
//    .toolbar {
//      ToolbarItem(placement: .navigationBarTrailing) {
//        EditButton()
//      }
//        ToolbarItem {
//          Button(action: addItem) {
//            Label("Add Item", systemImage: "plus")
//          }
//        }
//    }
    .task {
      tracker.fetch()
    }
  }
  
  private func addItem() {
    withAnimation {
      let newItem = Item(timestamp: Date())
      modelContext.insert(newItem)
    }
  }
  
  private func deleteItems(offsets: IndexSet) {
    withAnimation {
      for index in offsets {
        modelContext.delete(items[index])
      }
    }
  }
}

#Preview {
  TrackerView()
    .modelContainer(for: Item.self, inMemory: true)
}