/
GeolocationViewController.swift
61 lines (48 loc) · 1.63 KB
/
GeolocationViewController.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
//
// GeolocationViewController.swift
// RxExample
//
// Created by Carlos García on 19/01/16.
// Copyright © 2016 Krunoslav Zaher. All rights reserved.
//
import UIKit
import CoreLocation
import RxSwift
import RxCocoa
private extension Reactive where Base: UILabel {
var coordinates: Binder<CLLocationCoordinate2D> {
return Binder(base) { label, location in
label.text = "Lat: \(location.latitude)\nLon: \(location.longitude)"
}
}
}
class GeolocationViewController: ViewController {
@IBOutlet weak private var noGeolocationView: UIView!
@IBOutlet weak private var button: UIButton!
@IBOutlet weak private var button2: UIButton!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(noGeolocationView)
let geolocationService = GeolocationService.instance
geolocationService.authorized
.drive(noGeolocationView.rx.isHidden)
.disposed(by: disposeBag)
geolocationService.location
.drive(label.rx.coordinates)
.disposed(by: disposeBag)
button.rx.tap
.bind { [weak self] _ -> Void in
self?.openAppPreferences()
}
.disposed(by: disposeBag)
button2.rx.tap
.bind { [weak self] _ -> Void in
self?.openAppPreferences()
}
.disposed(by: disposeBag)
}
private func openAppPreferences() {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}
}