-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathServerAddViewModel.swift
More file actions
117 lines (92 loc) · 3.37 KB
/
ServerAddViewModel.swift
File metadata and controls
117 lines (92 loc) · 3.37 KB
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
//
// ServerAddView.swift
// qBitControl
//
import SwiftUI
class ServerAddViewModel: ObservableObject {
@ObservedObject var serversHelper = ServersHelper.shared
var editServerId: String?
@Published var friendlyName = ""
@Published var url = ""
@Published var username = ""
@Published var password = ""
@Published var basicAuth: Server.BasicAuth?
@Published var isCheckConnection = true;
@Published var isInvalidAlert = false;
@Published var invalidAlertMessage = "";
@Published var isCheckingConnection = false;
public var addButtonColor: Color { self.isCheckingConnection ? Color.gray : Color.blue }
private var alertQueue: [String] = [];
init() { }
init(editServerId: String) {
self.editServerId = editServerId
if let server = serversHelper.getServer(id: editServerId) {
friendlyName = server.name
url = server.url
username = server.username
password = server.password
basicAuth = server.basicAuth
}
}
func validateInputs() -> Bool {
if(!(url.contains("https://") || url.contains("http://"))) {
showAlert(message: "Include protocol in the URL - 'https://' or 'http://' depending on your setup.")
return false;
}
return true;
}
func validateIsConnecting() -> Bool {
if (self.isCheckingConnection) {
//showAlert(message: "Adding, Please wait")
return false;
}
return true;
}
func showAlert(message: String?) {
if let message = message {
alertQueue.append(message)
}
guard !isInvalidAlert, let message = alertQueue.first else {
return;
}
alertQueue.removeFirst()
invalidAlertMessage = message
isInvalidAlert = true
}
func alertDismissed() {
DispatchQueue.main.async {
self.showAlert(message: nil)
}
}
func addServer(server: Server) {
serversHelper.addServer(server: server)
}
func addServer(dismiss: DismissAction) -> Void {
if(!validateInputs()) { return; }
if(!validateIsConnecting()) { return; }
sanitizeInputs()
let server = Server(name: friendlyName, url: url, username: username, password: password, basicAuth: basicAuth)
if(!isCheckConnection) {
if let editServerId = self.editServerId { serversHelper.removeServer(id: editServerId) }
addServer(server: server)
dismiss()
}
self.isCheckingConnection = true
serversHelper.checkConnection(server: server, result: {
didConnect in
DispatchQueue.main.async {
if(didConnect) {
if let editServerId = self.editServerId { self.serversHelper.removeServer(id: editServerId) }
self.addServer(server: server)
dismiss()
} else {
self.showAlert(message: "Can't connect to the server.")
}
self.isCheckingConnection = false
}
})
}
private func sanitizeInputs() {
url = url.replacingOccurrences(of: "/+$", with: "", options: .regularExpression)
}
}