Skip to content

Commit 35427e1

Browse files
refactor
1 parent 5aca20d commit 35427e1

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

src/ios/CDVRoomPlan.swift

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Cordova
12
//
23
// CDVRoomPlan.swift
34
// SharinPix
@@ -21,55 +22,42 @@ import WebKit
2122
private var roomCaptureSessionConfig: RoomCaptureSession.Configuration = RoomCaptureSession.Configuration()
2223
private var processedResult: CapturedRoom?
2324

24-
private var currentCommand: CDVInvokedUrlCommand?
25+
private var command: CDVInvokedUrlCommand?
2526

26-
// Required initializer for CDVPlugin
27-
@objc required override init(webViewEngine: WKWebView) {
28-
super.init(webViewEngine: webViewEngine)
27+
func encode(with coder: NSCoder) {
28+
fatalError("Not Needed")
2929
}
3030

3131
required init?(coder: NSCoder) {
3232
fatalError("Not Needed")
3333
}
3434

35-
func encode(with coder: NSCoder) {
36-
fatalError("Not Needed")
35+
// Required initializer for CDVPlugin
36+
@objc required override init(webViewEngine: WKWebView) {
37+
super.init(webViewEngine: webViewEngine)
3738
}
38-
39+
3940
@objc(open:)
4041
func open(command: CDVInvokedUrlCommand) {
41-
self.currentCommand = command
42-
43-
DispatchQueue.main.async { [weak self] in
44-
guard let self = self, let viewController = self.viewController else { return }
45-
46-
self.roomCaptureView = RoomCaptureView(frame: viewController.view.bounds)
47-
self.roomCaptureView.captureSession.delegate = self
48-
self.roomCaptureView.delegate = self
49-
viewController.view.addSubview(self.roomCaptureView)
50-
self.roomCaptureView.translatesAutoresizingMaskIntoConstraints = false
51-
NSLayoutConstraint.activate([
52-
self.roomCaptureView.topAnchor.constraint(equalTo: viewController.view.topAnchor),
53-
self.roomCaptureView.leftAnchor.constraint(equalTo: viewController.view.leftAnchor),
54-
self.roomCaptureView.bottomAnchor.constraint(equalTo: viewController.view.bottomAnchor),
55-
self.roomCaptureView.rightAnchor.constraint(equalTo: viewController.view.rightAnchor)
56-
])
57-
58-
NotificationCenter.default.addObserver(
59-
self,
60-
selector: #selector(self.handleAppWillResignActive),
61-
name: UIApplication.willResignActiveNotification,
62-
object: nil
63-
)
64-
65-
self.startSession()
66-
}
42+
self.command = command
43+
roomCaptureView = RoomCaptureView(frame: viewController.view.bounds)
44+
roomCaptureView.captureSession.delegate = self
45+
roomCaptureView.delegate = self
46+
viewController.view.addSubview(roomCaptureView)
47+
roomCaptureView.translatesAutoresizingMaskIntoConstraints = false
48+
NSLayoutConstraint.activate([
49+
roomCaptureView.topAnchor.constraint(equalTo: viewController.view.topAnchor),
50+
roomCaptureView.leftAnchor.constraint(equalTo: viewController.view.leftAnchor),
51+
roomCaptureView.bottomAnchor.constraint(equalTo: viewController.view.bottomAnchor),
52+
roomCaptureView.rightAnchor.constraint(equalTo: viewController.view.rightAnchor)
53+
]);
54+
NotificationCenter.default.addObserver(self, selector: #selector(cancelScanning), name: UIApplication.willResignActiveNotification, object: nil)
55+
startSession()
6756
}
6857

6958
@objc(isSupported:)
7059
func isSupported(command: CDVInvokedUrlCommand) {
71-
let supported = ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh)
72-
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: supported)
60+
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh))
7361
self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
7462
}
7563

@@ -95,7 +83,7 @@ import WebKit
9583
let result = ["message": error.localizedDescription]
9684
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: result)
9785
pluginResult?.keepCallback = true
98-
self.commandDelegate.send(pluginResult, callbackId: self.currentCommand?.callbackId)
86+
self.commandDelegate.send(pluginResult, callbackId: self.command?.callbackId)
9987
return
10088
}
10189
self.processedResult = processedResult
@@ -120,27 +108,19 @@ import WebKit
120108
}
121109

122110
@objc func cancelScanning(_ sender: UIButton) {
123-
dismissCaptureView()
124111
let result = ["message": "Scanning cancelled"]
125112
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: result)
126113
pluginResult?.keepCallback = true
127-
self.commandDelegate.send(pluginResult, callbackId: self.currentCommand?.callbackId)
128-
currentCommand = nil
129-
}
130-
131-
@objc func handleAppWillResignActive() {
132-
// Called from notification observer when app goes to background
133-
if let button = cancelButton {
134-
cancelScanning(button)
135-
}
114+
self.commandDelegate.send(pluginResult, callbackId: self.command?.callbackId)
115+
dismissCaptureView()
136116
}
137117

138118
func exportResults() {
139119
let documentsDirectory = FileManager.default.temporaryDirectory.appendingPathComponent("cordova-room-plan")
140120
let uuid = NSUUID().uuidString
141121
let modelFile = documentsDirectory.appendingPathComponent(uuid + ".usdz")
142122
let jsonFile = documentsDirectory.appendingPathComponent(uuid + ".json")
143-
123+
144124
do {
145125
try FileManager.default.createDirectory(at: documentsDirectory, withIntermediateDirectories: true, attributes: nil)
146126
let jsonEncoder = JSONEncoder()
@@ -152,21 +132,21 @@ import WebKit
152132
let result = ["model": modelFile.absoluteString, "json": jsonFile.absoluteString, "message": "Scanning completed successfully"]
153133
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: result)
154134
pluginResult?.keepCallback = true
155-
self.commandDelegate.send(pluginResult, callbackId: self.currentCommand?.callbackId)
135+
self.commandDelegate.send(pluginResult, callbackId: self.command?.callbackId)
156136
} else {
157137
let result = ["message": "No results captured"]
158138
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: result)
159139
pluginResult?.keepCallback = true
160-
self.commandDelegate.send(pluginResult, callbackId: self.currentCommand?.callbackId)
140+
self.commandDelegate.send(pluginResult, callbackId: self.command?.callbackId)
161141
}
162142
} catch {
163143
let result = ["message": "Error exporting results"]
164144
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: result)
165145
pluginResult?.keepCallback = true
166-
self.commandDelegate.send(pluginResult, callbackId: self.currentCommand?.callbackId)
146+
self.commandDelegate.send(pluginResult, callbackId: self.command?.callbackId)
167147
}
168148

169-
currentCommand = nil
149+
command = nil
170150
}
171151

172152
private func addButtons() {

0 commit comments

Comments
 (0)