@@ -23,14 +23,13 @@ protocol CustomServerSelectionViewControllerDelegate: class {
23
23
class CustomServerSelectionViewController : UIViewController {
24
24
25
25
private struct Strings {
26
- static let missingIPTitle = " Missing IP Address "
27
- static let invalidPortTitle = " Invalid Server Port "
28
- static let serverAddressLabel = " Server address "
26
+ static let missingIPTitle = " Missing IP Address "
27
+ static let invalidPortTitle = " Invalid Server Port "
28
+ static let serverAddressLabel = " Server address "
29
29
static let serverAddressPlaceholder = " https://your-server.com "
30
- static let serverPortLabel = " Server Port "
31
- static let serverPortPlaceholder = " 8096 "
32
- static let connectTitle = " Connect to Server "
33
-
30
+ static let serverPortLabel = " Server Port "
31
+ static let serverPortPlaceholder = " 8096 "
32
+ static let connectTitle = " Connect to Server "
34
33
}
35
34
36
35
enum Errors : LocalizedError {
@@ -39,62 +38,83 @@ class CustomServerSelectionViewController: UIViewController {
39
38
40
39
public var errorDescription : String ? {
41
40
switch self {
42
- case . missingIP: return Strings . missingIPTitle
43
- case . invalidServerPort: return Strings . invalidPortTitle
41
+ case . missingIP:
42
+ return Strings . missingIPTitle
43
+ case . invalidServerPort:
44
+ return Strings . invalidPortTitle
44
45
}
45
46
}
46
47
}
47
48
48
- lazy var scrollView : UIScrollView = ViewBuilder . scrollView ( subview: self . contentView)
49
- lazy var contentView : UIStackView = ViewBuilder . stackView ( arrangedSubviews: [ self . serverIPLabel,
50
- self . serverIPField,
51
- self . serverPortLabel,
52
- self . serverPortField,
53
- self . errorTextLabel,
54
- self . connectButton] ,
55
- layoutMargins: UIEdgeInsets ( top: 20 , left: 20 , bottom: 20 , right: 20 ) )
56
-
57
- lazy var serverIPLabel = ViewBuilder . textLabel ( font: . title3,
58
- text: Strings . serverAddressLabel)
59
- lazy var serverIPField : UITextField = ViewBuilder . textField ( placeholder: Strings . serverAddressPlaceholder,
60
- text: " " ,
61
- keybordType: . URL)
62
-
63
- lazy var serverPortLabel = ViewBuilder . textLabel ( font: . title3,
64
- text: Strings . serverPortLabel)
65
- lazy var serverPortField : UITextField = ViewBuilder . textField ( placeholder: Strings . serverPortPlaceholder,
66
- keybordType: . numberPad)
67
-
68
- lazy var errorTextLabel : UILabel = ViewBuilder . textLabel ( textColor: . red,
69
- font: . callout,
70
- isHidden: true )
71
-
72
- lazy var connectButton : UIButton = ViewBuilder . button ( title: Strings . connectTitle,
73
- color: UIColor ( red: 20 / 255 , green: 200 / 255 , blue: 20 / 255 , alpha: 1 ) ,
74
- target: self ,
75
- selector: #selector( self . connectButtonWasTapped) )
49
+ lazy private var serverIPField : UITextField = ViewBuilder . textField ( placeholder: Strings . serverAddressPlaceholder,
50
+ text: " " , keybordType: . URL)
51
+
52
+ lazy private var serverPortField : UITextField = ViewBuilder . textField ( placeholder: Strings . serverPortPlaceholder,
53
+ keybordType: . numberPad)
54
+
55
+ lazy private var errorTextLabel : UILabel = ViewBuilder . textLabel ( textColor: . red,
56
+ font: . callout,
57
+ isHidden: true )
58
+
59
+ lazy private var connectButton : UIButton = ViewBuilder . button ( title: Strings . connectTitle,
60
+ color: UIColor ( red: 20 / 255 , green: 200 / 255 , blue: 20 / 255 , alpha: 1 ) ,
61
+ target: self ,
62
+ selector: #selector( self . connectButtonWasTapped) )
76
63
77
64
weak var delegate : CustomServerSelectionViewControllerDelegate ?
78
65
79
66
override func viewDidLoad( ) {
80
67
setupViewController ( )
81
68
}
82
69
70
+ override func viewDidDisappear( _ animated: Bool ) {
71
+ resetViewController ( )
72
+ }
73
+
83
74
private func setupViewController( ) {
75
+ let serverIPLabel : UILabel = ViewBuilder . textLabel ( font: . title3, text: Strings . serverAddressLabel)
76
+ let serverPortLabel : UILabel = ViewBuilder . textLabel ( font: . title3, text: Strings . serverPortLabel)
77
+
84
78
title = " Custom Connection "
85
79
view. backgroundColor = . black
80
+
81
+ let views = [ serverIPLabel,
82
+ serverIPField,
83
+ serverPortLabel,
84
+ serverPortField,
85
+ errorTextLabel,
86
+ connectButton]
87
+
88
+ let contentView : UIStackView = ViewBuilder . stackView ( arrangedSubviews: views,
89
+ layoutMargins: UIEdgeInsets ( top: 20 , left: 20 , bottom: 20 , right: 20 ) )
90
+
91
+ let scrollView : UIScrollView = ViewBuilder . scrollView ( subview: contentView)
92
+
86
93
view. addSubview ( scrollView)
87
94
scrollView. fillSuperView ( )
95
+
96
+ serverIPField. autocapitalizationType = . none
97
+ }
98
+
99
+ private func resetViewController( ) {
100
+ resetError ( )
88
101
}
89
102
90
103
private func getServerConnection( ) throws -> ServerConnection {
91
104
92
- guard var ipAddress = serverIPField. text,
93
- !ipAddress . isEmpty else { throw Errors . missingIP }
105
+ guard var ipAddress = serverIPField. text, ipAddress . isNotBlankOrEmpty else { throw Errors . missingIP }
106
+
94
107
if !ipAddress. contains ( " : " ) {
95
108
ipAddress = " http:// " + ipAddress
96
109
}
97
- let portString = serverPortField. text? . isEmpty == true ? Strings . serverPortPlaceholder : serverPortField. text!
110
+
111
+ let portString : String
112
+ if let userDefinedPort = serverPortField. text {
113
+ portString = userDefinedPort. isNotBlankOrEmpty ? userDefinedPort : Strings . serverPortPlaceholder
114
+ } else {
115
+ portString = Strings . serverPortPlaceholder
116
+ }
117
+
98
118
guard let port = Int ( portString) else { throw Errors . invalidServerPort }
99
119
100
120
return ServerConnection ( ipAddress: ipAddress, port: port)
@@ -105,9 +125,15 @@ class CustomServerSelectionViewController: UIViewController {
105
125
errorTextLabel. isHidden = false
106
126
}
107
127
128
+ private func resetError( ) {
129
+ errorTextLabel. text = " "
130
+ errorTextLabel. isHidden = true
131
+ }
132
+
108
133
@objc
109
- func connectButtonWasTapped( ) {
134
+ private func connectButtonWasTapped( ) {
110
135
do {
136
+ resetError ( )
111
137
let connection = try getServerConnection ( )
112
138
delegate? . connectToServer ( connection)
113
139
} catch {
0 commit comments