aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/Trackers/TrackerBookmarkSheet.swift
blob: d66a466cb893534ef955aa8c7787d07d324806b5 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import SwiftUI

struct TrackerBookmarkSheet: View {
  @Environment(\.dismiss) private var dismiss
  @Environment(\.modelContext) private var modelContext
  
  @State private var bookmark: Bookmark? = nil
  @State private var trackerAddress: String = ""
  @State private var trackerName: String = ""
  
  init() {
    
  }
  
  init(_ editingBookmark: Bookmark) {
    _bookmark = .init(initialValue: editingBookmark)
    _trackerAddress = .init(initialValue: editingBookmark.displayAddress)
    _trackerName = .init(initialValue: editingBookmark.name)
  }
  
  var body: some View {
    VStack(alignment: .leading) {
      Form {
        if self.bookmark == nil {
          HStack(alignment: .top, spacing: 10) {
            GroupedIconView(color: .blue, systemName: "point.3.filled.connected.trianglepath.dotted", padding: 5.0)
              .frame(width: 28, height: 28)
            
            VStack(alignment: .leading) {
              Text("Add a Hotline Tracker")
              
              Text("Enter the address and name of a Hotline Tracker you want to add.")
                .foregroundStyle(.secondary)
                .font(.subheadline)
            }
          }
        }
        else {
          HStack(alignment: .top, spacing: 10) {
            GroupedIconView(color: .blue, systemName: "point.3.filled.connected.trianglepath.dotted", padding: 5.0)
              .frame(width: 28, height: 28)
            
            VStack(alignment: .leading) {
              Text("Edit Hotline Tracker")
              
              Text("Change the address and name of your Hotline Tracker.")
                .foregroundStyle(.secondary)
                .font(.subheadline)
            }
          }
        }
        
        Group {
          TextField(text: $trackerAddress) {
            Text("Address")
          }
          TextField(text: $trackerName, prompt: Text("Optional")) {
            Text("Name")
          }
        }
//        .textFieldStyle(.roundedBorder)
        .controlSize(.large)
      }
      .formStyle(.grouped)
    }
    .frame(width: 350)
    .fixedSize(horizontal: true, vertical: true)
    .toolbar {
      ToolbarItem(placement: .confirmationAction) {
        Button {
          self.saveTracker()
        } label: {
          if self.bookmark != nil {
            Text("Save")
          }
          else {
            Text("Add")
          }
        }
      }
      ToolbarItem(placement: .cancellationAction) {
        Button("Cancel") {
          self.trackerName = ""
          self.trackerAddress = ""
          
          self.dismiss()
        }
      }
    }
  }
  
  private func saveTracker() {
    var displayName = trackerName.trimmingCharacters(in: .whitespacesAndNewlines)
    let (host, port) = Tracker.parseTrackerAddressAndPort(trackerAddress)
    
    if displayName.isEmpty {
      displayName = host
    }
    
    if !displayName.isEmpty && !host.isEmpty {
      if !host.isEmpty {
        if self.bookmark != nil {
          // We're editing an existing bookmark.
          self.bookmark?.name = displayName
          self.bookmark?.address = host
          self.bookmark?.port = port
        }
        else {
          // We're creating a new bookmark.
          let newBookmark = Bookmark(type: .tracker, name: displayName, address: host, port: port)
          Bookmark.add(newBookmark, context: modelContext)
        }
        
        self.trackerName = ""
        self.trackerAddress = ""
        
        self.dismiss()
      }
    }
  }
}