Skip to content

Commit

Permalink
Merge branch 'feature/stability_improvements' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Jun 24, 2024
2 parents 458fd44 + 91de04c commit 549a918
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions Kukai Mobile/Controls/ScanViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelega
setupClearBox()
setupOutlineView()

AVCaptureDevice.requestAccess(for: .video) { [weak self] (response) in
DispatchQueue.main.async {
if response {
self?.setupCaptureSession()
} else {
self?.failed()
}
}
}

ThemeManager.shared.$themeDidChange
.dropFirst()
.sink { [weak self] _ in
Expand All @@ -73,12 +63,26 @@ class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelega

textfield.text = nil

setupPreviewLayer()
if (captureSession?.isRunning == false) {
// Xcode warning, should be run on a background thread in order to avoid hanging UI thread
DispatchQueue.global(qos: .background).async { [weak self] in
self?.captureSession?.startRunning()
let status = AVCaptureDevice.authorizationStatus(for: .video)
if status == .authorized {
setupCaptureSession()
setupPreviewLayer()
startCaptureSessionIfNeeded()

} else if status == .notDetermined {
AVCaptureDevice.requestAccess(for: .video) { [weak self] (response) in
DispatchQueue.main.async {
if response {
self?.setupCaptureSession()
self?.setupPreviewLayer()
self?.startCaptureSessionIfNeeded()
} else {
self?.failed()
}
}
}
} else {
self.failed()
}

scrollView.setupAutoScroll(focusView: textfield, parentView: self.view)
Expand All @@ -101,6 +105,15 @@ class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelega
self.dismiss(animated: true, completion: nil)
}

func startCaptureSessionIfNeeded() {
if (captureSession?.isRunning == false) {
// Xcode warning, should be run on a background thread in order to avoid hanging UI thread
DispatchQueue.global(qos: .background).async { [weak self] in
self?.captureSession?.startRunning()
}
}
}

@objc func textFieldDone() {
checkForBeaconAndReport(stringToCheck: textfield.text ?? "")
}
Expand Down Expand Up @@ -309,6 +322,10 @@ class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelega
return
}

if previewLayer.superlayer != nil {
previewLayer.removeFromSuperlayer()
}

previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.frame = previewContainerView.bounds
previewLayer.videoGravity = .resizeAspectFill
Expand Down

0 comments on commit 549a918

Please sign in to comment.