summaryrefslogtreecommitdiff
path: root/Example Apps/Example Swift/ConnectionIAPTableViewController.swift
blob: 5a389b2d827324aeab40ce70f9a167a77a6e5206 (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
//
//  ConnectionIAPTableViewController.swift
//  SmartDeviceLink-ExampleSwift
//
//  Copyright © 2017 smartdevicelink. All rights reserved.
//
import UIKit

class ConnectionIAPTableViewController: UITableViewController {
    @IBOutlet weak var connectTableViewCell: UITableViewCell!
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var connectButton: UIButton!

    var proxyState = ProxyState.stopped

    override func viewDidLoad() {
        super.viewDidLoad()
        ProxyManager.sharedManager.delegate = self
        title = "iAP"
        table.keyboardDismissMode = .onDrag
        table.isScrollEnabled = false
        configureConnectButton()
    }

    private func configureConnectButton() {
        self.connectTableViewCell.backgroundColor = UIColor.systemRed
        self.connectButton.setTitle("Connect", for: .normal)
        self.connectButton.setTitleColor(.white, for: .normal)
    }
}

extension ConnectionIAPTableViewController: ProxyManagerDelegate {
    func didChangeProxyState(_ newState: ProxyState) {
        proxyState = newState
        var newColor: UIColor? = nil
        var newTitle: String? = nil

        switch newState {
        case .stopped:
            newColor = .systemRed
            newTitle = "Connect"
        case .searching:
            newColor = .systemOrange
            newTitle = "Stop Searching"
        case .connected:
            newColor = .systemGreen
            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)
            })
        }
    }
}

// MARK: - IBActions
extension ConnectionIAPTableViewController {
    @IBAction private func connectButtonWasPressed(_ sender: UIButton) {
        switch proxyState {
        case .stopped:
            ProxyManager.sharedManager.start(with: .iap)
        case .searching:
            ProxyManager.sharedManager.stopConnection()
        case .connected:
            ProxyManager.sharedManager.stopConnection()
        }
    }
}