-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraViewController.swift
94 lines (81 loc) · 3.21 KB
/
CameraViewController.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
90
91
92
93
94
//
// CameraViewController.swift
// Map+PositioningExample
//
// Created by Evgenii Khrushchev on 04/09/2023.
// Copyright © 2023 Wemap SAS. All rights reserved.
//
import ARKit
import RealityKit
import RxSwift
import UIKit
import WemapPositioningSDKVPSARKit
@available(iOS 13.0, *)
class CameraViewController: UIViewController {
@IBOutlet var infoLabel: UILabel!
@IBOutlet var startScanButton: UIButton!
@IBOutlet var stopScanButton: UIButton!
var session: ARSession!
var vpsLocationSource: VPSARKitLocationSource!
private let stateListener = SerialDisposable()
private let disposeBag = DisposeBag()
private var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView = ARView(frame: view.frame)
arView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.insertSubview(arView, at: 0)
arView.session = session
weak var previousToast: UIView?
vpsLocationSource
.rx.didFail
.emit(onNext: { [unowned self] in
previousToast?.removeFromSuperview()
previousToast = ToastHelper.showToast(message: "VPS failed with error - \($0)", onView: view)
})
.disposed(by: disposeBag)
vpsLocationSource
.rx.didChangeScanStatus
.emit(onNext: { [unowned self] in
if $0 == .started {
infoLabel.text = "VPS scanning started"
startScanButton.isEnabled = false
stopScanButton.isEnabled = true
} else {
infoLabel.text = "VPS scanning stopped"
startScanButton.isEnabled = true
stopScanButton.isEnabled = false
}
})
.disposed(by: disposeBag)
}
@IBAction func startScan() {
vpsLocationSource.startScan()
stateListener.disposable = vpsLocationSource
.rx.didChangeState.asObservable()
.filter { $0 == .accuratePositioning }
.take(1).asSingle()
.subscribe(onSuccess: { [unowned self] _ in
dismiss(animated: true)
})
}
// FIXME: workaround to avoid automatic stopping of the session due to iOS 18 changes.
// before if you set your custom session, you have to start and stop it manually.
// Now even if it's a custom session - it will be automatically stopped when view is hidden.
// And it's not even possible to set nil to reset the session. So here we set new session just to remove control from our own one.
// ARSession does not report any changes in state or errors when it happens, that's why we consider this behaviour as a bug.
// so you can't differentiate this behaviour is tracking lost or a bug. because last reported state is normal, but position is not updated
override func viewWillDisappear(_ animated: Bool) {
if #available(iOS 18.0, *) {
arView.session = ARSession()
}
super.viewWillDisappear(animated)
}
// end
@IBAction func stopScan() {
vpsLocationSource.stopScan()
}
deinit {
stateListener.dispose()
}
}