aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/Trackers/ServerBookmarkSheet.swift
blob: 6ad165702bcb3f0eb284529d03e9518517e16544 (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
import SwiftUI

struct ServerBookmarkSheet: View {
  @Environment(\.dismiss) private var dismiss
  @Environment(\.modelContext) private var modelContext

  @State private var bookmark: Bookmark
  @State private var serverName: String = ""
  @State private var serverAddress: String = ""
  @State private var serverLogin: String = ""
  @State private var serverPassword: String = ""

  init(_ editingBookmark: Bookmark) {
    _bookmark = .init(initialValue: editingBookmark)
    _serverName = .init(initialValue: editingBookmark.name)
    _serverAddress = .init(initialValue: editingBookmark.displayAddress)
    _serverLogin = .init(initialValue: editingBookmark.login ?? "")
    _serverPassword = .init(initialValue: editingBookmark.password ?? "")
  }

  var body: some View {
    Form {
      Section {
        TextField(text: $serverName) {
          Text("Name")
        }
      }
       
      Section {
        TextField(text: $serverAddress) {
          Text("Address")
        }
        TextField(text: $serverLogin, prompt: Text("Optional")) {
          Text("Login")
        }
        SecureField(text: $serverPassword, prompt: Text("Optional")) {
          Text("Password")
        }
      }
    }
    .formStyle(.grouped)
    .frame(width: 350)
    .fixedSize(horizontal: true, vertical: true)
    .toolbar {
      ToolbarItem(placement: .confirmationAction) {
        Button("Save") {
          let displayName = self.serverName.trimmingCharacters(in: .whitespacesAndNewlines)
          let (host, port) = Server.parseServerAddressAndPort(self.serverAddress)
          let login = self.serverLogin.trimmingCharacters(in: .whitespacesAndNewlines)
          let password = self.serverPassword

          if !displayName.isEmpty && !host.isEmpty {
            self.bookmark.name = displayName
            self.bookmark.address = host
            self.bookmark.port = port
            self.bookmark.login = login.isEmpty ? nil : login
            self.bookmark.password = password.isEmpty ? nil : password

            self.dismiss()
          }
        }
      }
      ToolbarItem(placement: .cancellationAction) {
        Button("Cancel") {
          self.dismiss()
        }
      }
    }
  }
}