blob: 3cf1e91c364d72238ddd8faf9979068f497f7966 (
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
|
//
// ConnectionTCPTableViewController.swift
// SmartDeviceLink-ExampleSwift
//
// Copyright © 2017 smartdevicelink. All rights reserved.
//
import UIKit
class ConnectionTCPTableViewController: UITableViewController, UINavigationControllerDelegate, ProxyManagerDelegate {
@IBOutlet weak var ipAddressTextField: UITextField!
@IBOutlet weak var portTextField: UITextField!
@IBOutlet weak var connectTableViewCell: UITableViewCell!
@IBOutlet weak var connectButton: UIButton!
@IBOutlet weak var table: UITableView!
var state: ProxyState = .stopped
override func viewDidLoad() {
super.viewDidLoad()
ProxyManager.sharedManager.delegate = self
table.keyboardDismissMode = .onDrag
table.isScrollEnabled = false
ipAddressTextField.text = AppUserDefaults.shared.ipAddress
portTextField.text = AppUserDefaults.shared.port
initButton()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func initButton() {
self.connectTableViewCell.backgroundColor = UIColor.red
self.connectButton.setTitle("Connect", for: .normal)
self.connectButton.setTitleColor(.white, for: .normal)
}
// MARK: - IBActions
@IBAction func connectButtonWasPressed(_ sender: UIButton) {
let ipAddress = ipAddressTextField.text
let port = portTextField.text
if ipAddress != "" || port != "" {
AppUserDefaults.shared.ipAddress = ipAddress
AppUserDefaults.shared.port = port
switch state {
case .stopped:
ProxyManager.sharedManager.start(with: .tcp)
case .searching:
ProxyManager.sharedManager.resetConnection()
case .connected:
ProxyManager.sharedManager.resetConnection()
}
} else {
let alertMessage = UIAlertController(title: "Missing Info!", message: "Make sure to set your IP Address and Port", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
}
// MARK: - Delegate Functions
func didChangeProxyState(_ newState: ProxyState) {
state = newState
var newColor: UIColor? = nil
var newTitle: String? = nil
switch newState {
case .stopped:
newColor = UIColor.red
newTitle = "Connect"
case .searching:
newColor = UIColor.blue
newTitle = "Stop Searching"
case .connected:
newColor = UIColor.green
newTitle = "Disconnect"
}
if (newColor != nil) || (newTitle != nil) {
DispatchQueue.main.async(execute: {[weak self]() -> Void in
self?.connectTableViewCell.backgroundColor = newColor
self?.connectButton.setTitle(newTitle, for: .normal)
self?.connectButton.setTitleColor(.white, for: .normal)
})
}
}
}
|