Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
90 lines (75 sloc) 2.75 KB
//
// 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)
}
}
You can’t perform that action at this time.