summaryrefslogtreecommitdiff
path: root/SmartDeviceLink-ExampleSwift/ConnectionIAPTableViewController.swift
blob: 4034ae5914f5ecfd70e8d2ff055ec852d88d3b16 (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
//
//  ConnectionIAPTableViewController.swift
//  SmartDeviceLink-ExampleSwift
//
//  Copyright © 2017 smartdevicelink. All rights reserved.
//

import UIKit

class ConnectionIAPTableViewController: UITableViewController, ProxyManagerDelegate {
    
    @IBOutlet weak var connectTableViewCell: UITableViewCell!
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var connectButton: UIButton!
    
    var state: ProxyState = ProxyState.stopped
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Set delegate to self
        delegate = self
        // TableView Setup
        table.keyboardDismissMode = .onDrag
        table.isScrollEnabled = false;
        // Button setup
        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) {
        
        // Initialize (or reset) the SDL manager
        switch state {
        case ProxyState.stopped:
            ProxyManager.sharedManager.startIAP()
        case ProxyState.searching:
            ProxyManager.sharedManager.reset()
        case ProxyState.connected:
            ProxyManager.sharedManager.reset()
        }
    }

    // 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: {() -> Void in
                self.connectTableViewCell.backgroundColor = newColor
                self.connectButton.setTitle(newTitle, for: .normal)
                self.connectButton.setTitleColor(.white, for: .normal)
            })
        }
    }
}