aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/ConnectView.swift
blob: 84195221bda980915b3add88c72d10ad797f9819 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import SwiftUI

struct ConnectView: View {
  @Environment(\.dismiss) private var dismiss
  @Environment(\.modelContext) private var modelContext
  
  @Binding var address: String
  @Binding var login: String
  @Binding var password: String
  
  var action: (() -> Void)? = nil
  
  @State private var bookmarkSheetPresented: Bool = false
  @State private var bookmarkName: String = ""
  
  private enum FocusFields {
    case address
    case login
    case password
  }
  
  @FocusState private var focusedField: FocusFields?
  
  var body: some View {
    VStack(alignment: .center, spacing: 0) {
      Form {
        HStack(alignment: .top, spacing: 10) {
          Image("Server Large")
            .resizable()
            .scaledToFit()
            .frame(width: 28, height: 28)
          
          VStack(alignment: .leading) {
            Text("Connect to Server")
            Text("Enter the address of a Hotline server to connect to.")
              .foregroundStyle(.secondary)
              .font(.subheadline)
          }
        }
        
        TextField(text: self.$address) {
          Text("Address")
        }
        .focused($focusedField, equals: .address)
        
        TextField(text: self.$login, prompt: Text("Optional")) {
          Text("Login")
        }
        .focused($focusedField, equals: .login)
        
        SecureField(text: self.$password, prompt: Text("Optional")) {
          Text("Password")
        }
        .focused($focusedField, equals: .password)
      }
      .formStyle(.grouped)
      .fixedSize(horizontal: false, vertical: true)
      
      HStack {
        Button {
          if !self.address.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
            self.bookmarkSheetPresented = true
          }
        } label: {
          Image(systemName: "bookmark.fill")
        }
        .disabled(self.address.isEmpty)
        .controlSize(.regular)
        .buttonStyle(.automatic)
        .help("Bookmark server")
        
        Spacer()
        
        Button("Cancel") {
          self.dismiss()
        }
        .controlSize(.regular)
        .buttonStyle(.automatic)
        .keyboardShortcut(.cancelAction)
        
        Button("Connect") {
          self.action?()
//          self.connectToServer()
        }
        .controlSize(.regular)
        .buttonStyle(.automatic)
        .keyboardShortcut(.defaultAction)
      }
      .padding(.horizontal, 20)
    }
//    .onChange(of: self.address) {
//      let (a, p) = Server.parseServerAddressAndPort(connectAddress)
//      server.address = a
//      server.port = p
//    }
//    .onChange(of: connectLogin) {
//      server.login = connectLogin.trimmingCharacters(in: .whitespacesAndNewlines)
//    }
//    .onChange(of: connectPassword) {
//      server.password = connectPassword
//    }
    .frame(maxWidth: 380)
    .padding()
    .onAppear {
      self.focusedField = .address
    }
    .sheet(isPresented: self.$bookmarkSheetPresented) {
      VStack(alignment: .leading) {
        Text("Save Bookmark")
          .foregroundStyle(.secondary)
          .padding(.bottom, 4)
        TextField("Bookmark Name", text: self.$bookmarkName)
          .textFieldStyle(.roundedBorder)
          .controlSize(.large)
      }
      .frame(width: 250)
      .padding()
      .toolbar {
        ToolbarItem(placement: .cancellationAction) {
          Button("Cancel") {
            self.bookmarkSheetPresented = false
            self.bookmarkName = ""
          }
        }
        
        ToolbarItem(placement: .confirmationAction) {
          Button("Save") {
            let name = String(self.bookmarkName.trimmingCharacters(in: .whitespacesAndNewlines))
            if !name.isEmpty {
              self.bookmarkSheetPresented = false
              self.bookmarkName = ""

              let (host, port) = Server.parseServerAddressAndPort(self.address)
              let login: String? = self.login.isBlank ? nil : self.login
              let password: String? = self.password.isBlank ? nil : self.password
              
              if !host.isEmpty {
                let newBookmark = Bookmark(type: .server, name: name, address: host, port: port, login: login, password: password)
                Bookmark.add(newBookmark, context: modelContext)
              }
            }
          }
        }
      }
    }
  }
}