/
HeadingViewController.swift
90 lines (75 loc) · 2.75 KB
/
HeadingViewController.swift
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
89
//
// HeadingViewController.swift
// UIKit-Combine
//
// Created by Joseph Heck on 7/15/19.
// Copyright © 2019 SwiftUI-Notes. All rights reserved.
//
import UIKit
import Combine
import CoreLocation
class HeadingViewController: UIViewController {
var headingSubscriber: AnyCancellable?
let coreLocationProxy = LocationHeadingProxy()
var headingBackgroundQueue: DispatchQueue = DispatchQueue(label: "headingBackgroundQueue")
// MARK - lifecycle methods
@IBOutlet weak var permissionButton: UIButton!
@IBOutlet weak var activateTrackingSwitch: UISwitch!
@IBOutlet weak var headingLabel: UILabel!
@IBOutlet weak var locationPermissionLabel: UILabel!
@IBAction func requestPermission(_ sender: UIButton) {
print("requesting corelocation permission")
let _ = Future<Int, Never> { promise in
self.coreLocationProxy.mgr.requestWhenInUseAuthorization()
return promise(.success(1))
}
.delay(for: 2.0, scheduler: headingBackgroundQueue)
.receive(on: RunLoop.main)
.sink { _ in
print("updating corelocation permission label")
self.updatePermissionStatus()
}
}
@IBAction func trackingToggled(_ sender: UISwitch) {
switch sender.isOn {
case true:
self.coreLocationProxy.enable()
print("Enabling heading tracking")
case false:
self.coreLocationProxy.disable()
print("Disabling heading tracking")
}
}
func updatePermissionStatus() {
let x = CLLocationManager.authorizationStatus()
switch x {
case .authorizedWhenInUse:
locationPermissionLabel.text = "Allowed when in use"
case .notDetermined:
locationPermissionLabel.text = "notDetermined"
case .restricted:
locationPermissionLabel.text = "restricted"
case .denied:
locationPermissionLabel.text = "denied"
case .authorizedAlways:
locationPermissionLabel.text = "authorizedAlways"
@unknown default:
locationPermissionLabel.text = "unknown default"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// request authorization for the corelocation data
self.updatePermissionStatus()
let corelocationsub = coreLocationProxy
.publisher
.print("headingSubscriber")
.receive(on: RunLoop.main)
.sink(receiveCompletion: { completion in },
receiveValue: { someValue in
self.headingLabel.text = String(someValue.trueHeading)
})
headingSubscriber = AnyCancellable(corelocationsub)
}
}