Skip to content

Commit

Permalink
Update Camera Error View
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonCat4012 committed Sep 21, 2024
1 parent 580fac4 commit 9f3582b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 85 deletions.
205 changes: 120 additions & 85 deletions VGApp/Tabs/Scanner/ScannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,107 +13,136 @@ class ScannerView: UIViewController {
var avPreviewLayer: AVCaptureVideoPreviewLayer!
let scanAlert = UIAlertController(title: "alert_title_scannerAddNew".localized, message: "", preferredStyle: .alert)
var lastScanned: String = ""

override func viewDidLoad() {
self.navigationItem.title = "scannerView_title".localized

scanAlert.addTextField(configurationHandler: {(textfield) in
textfield.placeholder = "scannerView_item_placeholder_name".localized
})
var cameraHadError = false
lazy var errorLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 80))
label.backgroundColor = UIColor.red
label.numberOfLines = 3
label.textAlignment = .center
label.isHidden = true
return label
}()

override func viewDidLoad() {
view.addSubview(errorLabel)
self.navigationItem.title = "scannerView_title".localized

scanAlert.addTextField(configurationHandler: {(textfield) in
textfield.placeholder = "scannerView_item_placeholder_name".localized
})

scanAlert.addAction(UIAlertAction(title: "alert_action_scanner_cancel".localized, style: .cancel, handler: {_ in
self.scanAlert.dismiss(animated: true, completion: nil)
self.avCaptureSession.startRunning()
}))
scanAlert.addAction(UIAlertAction(title: "alert_action_add".localized, style: .default, handler: {_ in
Util.createItem(name: self.scanAlert.textFields![0].text!, code: self.lastScanned)

scanAlert.addAction(UIAlertAction(title: "alert_action_scanner_cancel".localized, style: .cancel, handler: {_ in
self.scanAlert.dismiss(animated: true, completion: nil)
DispatchQueue.global(qos: .background).async {
self.avCaptureSession.startRunning()
}))
scanAlert.addAction(UIAlertAction(title: "alert_action_add".localized, style: .default, handler: {_ in
Util.createItem(name: self.scanAlert.textFields![0].text!, code: self.lastScanned)

DispatchQueue.global(qos: .background).async {
self.avCaptureSession.startRunning()
}
}))
}
}))

super.viewDidLoad()
avCaptureSession = AVCaptureSession()
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else {
self.failed()
return
}
let avVideoInput: AVCaptureDeviceInput

super.viewDidLoad()
avCaptureSession = AVCaptureSession()
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else {
self.failed()
return
}
let avVideoInput: AVCaptureDeviceInput

do {
avVideoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
self.failed()
return
}

if self.avCaptureSession.canAddInput(avVideoInput) {
self.avCaptureSession.addInput(avVideoInput)
} else {
self.failed()
return
}

let metadataOutput = AVCaptureMetadataOutput()

if self.avCaptureSession.canAddOutput(metadataOutput) {
self.avCaptureSession.addOutput(metadataOutput)

metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417]
} else {
self.failed()
return
}

self.avPreviewLayer = AVCaptureVideoPreviewLayer(session: self.avCaptureSession)
self.avPreviewLayer.frame = self.view.layer.bounds
self.avPreviewLayer.videoGravity = .resizeAspectFill
self.view.layer.addSublayer(self.avPreviewLayer)
do {
avVideoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
self.failed()
return
}

if self.avCaptureSession.canAddInput(avVideoInput) {
self.avCaptureSession.addInput(avVideoInput)
} else {
self.failed()
return
}

let metadataOutput = AVCaptureMetadataOutput()

if self.avCaptureSession.canAddOutput(metadataOutput) {
self.avCaptureSession.addOutput(metadataOutput)

DispatchQueue.global(qos: .background).async {
self.avCaptureSession.startRunning()
}

metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417]
} else {
self.failed()
return
}
}

func failed() {
let ac = UIAlertController(title: "alert_title_scanner_noCamera".localized, message: "alert_message_scanner_noCamera".localized, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "alert_action_ok".localized, style: .default))
present(ac, animated: true)
avCaptureSession = nil
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if avCaptureSession?.isRunning == false {
DispatchQueue.global(qos: .background).async {
self.avCaptureSession.startRunning()
}
// cameraHadError = false
self.avPreviewLayer = AVCaptureVideoPreviewLayer(session: self.avCaptureSession)
self.avPreviewLayer.frame = self.view.layer.bounds
self.avPreviewLayer.videoGravity = .resizeAspectFill
self.view.layer.addSublayer(self.avPreviewLayer)

DispatchQueue.global(qos: .background).async {
self.avCaptureSession.startRunning()
}
}
}

func failed() {
let ac = UIAlertController(title: "alert_title_scanner_noCamera".localized, message: "alert_message_scanner_noCamera".localized, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "alert_action_ok".localized, style: .default))
// present(ac, animated: true)
cameraHadError = true
avCaptureSession = nil
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if avCaptureSession?.isRunning == true {
avCaptureSession.stopRunning()
if avCaptureSession?.isRunning == false {
DispatchQueue.global(qos: .background).async {
self.avCaptureSession.startRunning()
}
}

override var prefersStatusBarHidden: Bool {
return true
if avCaptureSession == nil || ((avCaptureSession?.isRunning) == nil) {
cameraHadError = true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
setLabel()
}

func setLabel() {
if !cameraHadError {
errorLabel.isHidden = true
return
}

let frame = view.frame
errorLabel.frame = CGRect(x: 0, y: 0, width: frame.width - 50, height: 80)
errorLabel.center = CGPoint(x: frame.width/2, y: frame.height/2)
errorLabel.text = "scannerView_nocamera".localized
errorLabel.isHidden = false
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if avCaptureSession?.isRunning == true {
avCaptureSession.stopRunning()
}
}

override var prefersStatusBarHidden: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}

}

// evertyhign scanner stuff from http://www.wepstech.com/bar-qr-code-ios-with-swift-5/
extension ScannerView: AVCaptureMetadataOutputObjectsDelegate {
Expand Down Expand Up @@ -149,3 +178,9 @@ class ScannerView: UIViewController {
return all.first?.name
}
}

enum scannerText {
case None
case NoCamera
case NoPermission
}
1 change: 1 addition & 0 deletions VGApp/Util/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"scannerView_title"="Barcode Scanner";
"scannerView_item_placeholder_name"="Name";
"scannerView_title"="Barcode Scanner";
"scannerView_nocamera"="Dieses Gerät besitzt keine Kamera oder die nötigen Berechtigungen wurdne nicht erteilt";

// ListDetailView
"listDetailView_title"="Listeninhalt";
Expand Down

0 comments on commit 9f3582b

Please sign in to comment.