Model.swift 1.42 KB
Newer Older
asahiocean's avatar
asahiocean committed
1
2
import UIKit

asahiocean's avatar
asahiocean committed
3
protocol ModelDelegate: AnyObject {
asahiocean's avatar
asahiocean committed
4
5
6
    func deselectPin(pin: PinAnnotation)
    func modelButtonExecute(sender: UIButton)
}
asahiocean's avatar
asahiocean committed
7
8
9
10

@objc class Model: NSObject {
    
    public static let shared = Model()
asahiocean's avatar
asahiocean committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    weak var trackingButton: UIButton!
    
    @objc dynamic var mainButton: UIButton?
    @objc dynamic var clearButton: UIButton?
    
    weak var delegate: ModelDelegate?
    
    @objc dynamic var graphsBuilt: Bool = false
    @objc dynamic var pins: [PinAnnotation] = []
    
    @objc dynamic var selected: PinAnnotation?
    public var route: [PinAnnotation] = []
    
    var selectedPinsKVO: NSKeyValueObservation!
    var mainButtonTitle: String = "" // To save the button name
asahiocean's avatar
asahiocean committed
26
27
28
    
    fileprivate override init() {
        super.init()
asahiocean's avatar
asahiocean committed
29
30
31
32
33
        mainButtonSetup()
        clearButtonSetup()
        selectedPinsKVO = observe(\.selected, options: .new, changeHandler: { [self] model, change in
            guard let newValue = change.newValue else { return }
            if newValue != nil {
asahiocean's avatar
asahiocean committed
34
35
                guard mainButton?.tag == Model.buttonTag.start.rawValue ||
                        mainButton?.tag == Model.buttonTag.finish.rawValue else { return }
asahiocean's avatar
asahiocean committed
36
37
                mainButton?.setTitle("OK", for: .normal)
            } else {
asahiocean's avatar
asahiocean committed
38
                guard mainButton?.tag != Model.buttonTag.routecalc.rawValue else { return }
asahiocean's avatar
asahiocean committed
39
40
41
                mainButton?.setTitle(mainButtonTitle, for: .normal)
            }
        })
asahiocean's avatar
asahiocean committed
42
43
    }
}