Skip to content

Commit 944a6a1

Browse files
authored
Merge pull request #132 from psalzAppDev/main
Improvement of code readability
2 parents 3038ca7 + 83db36b commit 944a6a1

File tree

3 files changed

+92
-80
lines changed

3 files changed

+92
-80
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import PackageDescription
66
let package = Package(
77
name: "CodeScanner",
88
platforms: [
9-
.iOS(.v13),
10-
.macOS(.v11)
9+
.iOS(.v13)
1110
],
1211
products: [
1312
// Products define the executables and libraries produced by a package, and make them visible to other packages.

Sources/CodeScanner/CodeScanner.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
8181
public let manualSelect: Bool
8282
public let scanInterval: Double
8383
public let showViewfinder: Bool
84-
public let requirePhotoOutput: Bool
84+
public let requiresPhotoOutput: Bool
8585
public var simulatedData = ""
8686
public var shouldVibrateOnSuccess: Bool
8787
public var isTorchOn: Bool
@@ -96,7 +96,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
9696
manualSelect: Bool = false,
9797
scanInterval: Double = 2.0,
9898
showViewfinder: Bool = false,
99-
requirePhotoOutput: Bool = true,
99+
requiresPhotoOutput: Bool = true,
100100
simulatedData: String = "",
101101
shouldVibrateOnSuccess: Bool = true,
102102
isTorchOn: Bool = false,
@@ -109,7 +109,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
109109
self.scanMode = scanMode
110110
self.manualSelect = manualSelect
111111
self.showViewfinder = showViewfinder
112-
self.requirePhotoOutput = requirePhotoOutput
112+
self.requiresPhotoOutput = requiresPhotoOutput
113113
self.scanInterval = scanInterval
114114
self.simulatedData = simulatedData
115115
self.shouldVibrateOnSuccess = shouldVibrateOnSuccess
@@ -136,6 +136,15 @@ public struct CodeScannerView: UIViewControllerRepresentable {
136136

137137
}
138138

139+
@available(macCatalyst 14.0, *)
140+
extension CodeScannerView {
141+
142+
@available(*, deprecated, renamed: "requiresPhotoOutput")
143+
public var requirePhotoOutput: Bool {
144+
requiresPhotoOutput
145+
}
146+
}
147+
139148
@available(macCatalyst 14.0, *)
140149
struct CodeScannerView_Previews: PreviewProvider {
141150
static var previews: some View {

Sources/CodeScanner/ScannerViewController.swift

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ extension CodeScannerView {
185185
previewLayer.frame = view.layer.bounds
186186
previewLayer.videoGravity = .resizeAspectFill
187187
view.layer.addSublayer(previewLayer)
188-
addviewfinder()
188+
addViewFinder()
189189

190190
reset()
191191

192-
if (captureSession.isRunning == false) {
192+
if !captureSession.isRunning {
193193
DispatchQueue.global(qos: .userInteractive).async {
194194
self.captureSession?.startRunning()
195195
}
@@ -232,7 +232,7 @@ extension CodeScannerView {
232232
NotificationCenter.default.addObserver(
233233
self,
234234
selector: #selector(updateOrientation),
235-
name: Notification.Name("UIDeviceOrientationDidChangeNotification"),
235+
name: UIDevice.orientationDidChangeNotification,
236236
object: nil
237237
)
238238
}
@@ -257,17 +257,17 @@ extension CodeScannerView {
257257
return
258258
}
259259

260-
if (captureSession!.canAddInput(videoInput)) {
260+
if captureSession!.canAddInput(videoInput) {
261261
captureSession!.addInput(videoInput)
262262
} else {
263263
didFail(reason: .badInput)
264264
return
265265
}
266266
let metadataOutput = AVCaptureMetadataOutput()
267267

268-
if (captureSession!.canAddOutput(metadataOutput)) {
268+
if captureSession!.canAddOutput(metadataOutput) {
269269
captureSession!.addOutput(metadataOutput)
270-
captureSession?.addOutput(photoOutput)
270+
captureSession!.addOutput(photoOutput)
271271
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
272272
metadataOutput.metadataObjectTypes = parentView.codeTypes
273273
} else {
@@ -276,7 +276,7 @@ extension CodeScannerView {
276276
}
277277
}
278278

279-
private func addviewfinder() {
279+
private func addViewFinder() {
280280
guard showViewfinder, let imageView = viewFinder else { return }
281281

282282
view.addSubview(imageView)
@@ -292,7 +292,7 @@ extension CodeScannerView {
292292
override public func viewDidDisappear(_ animated: Bool) {
293293
super.viewDidDisappear(animated)
294294

295-
if (captureSession?.isRunning == true) {
295+
if captureSession?.isRunning == true {
296296
DispatchQueue.global(qos: .userInteractive).async {
297297
self.captureSession?.stopRunning()
298298
}
@@ -329,7 +329,7 @@ extension CodeScannerView {
329329
return
330330
}
331331

332-
// Focus to the correct point, make continiuous focus and exposure so the point stays sharp when moving the device closer
332+
// Focus to the correct point, make continuous focus and exposure so the point stays sharp when moving the device closer
333333
device.focusPointOfInterest = focusPoint
334334
device.focusMode = .continuousAutoFocus
335335
device.exposurePointOfInterest = focusPoint
@@ -383,7 +383,7 @@ extension CodeScannerView {
383383
videoCaptureDevice.unlockForConfiguration()
384384
}
385385

386-
if isGalleryPresented && !isGalleryShowing {
386+
if isGalleryPresented, !isGalleryShowing {
387387
openGallery()
388388
}
389389

@@ -405,11 +405,11 @@ extension CodeScannerView {
405405
lastTime = Date()
406406
}
407407

408-
func isPastScanInterval() -> Bool {
408+
var isPastScanInterval: Bool {
409409
Date().timeIntervalSince(lastTime) >= parentView.scanInterval
410410
}
411411

412-
func isWithinManualCaptureInterval() -> Bool {
412+
var isWithinManualCaptureInterval: Bool {
413413
Date().timeIntervalSince(lastTime) <= 0.5
414414
}
415415

@@ -435,53 +435,56 @@ extension CodeScannerView {
435435
@available(macCatalyst 14.0, *)
436436
extension CodeScannerView.ScannerViewController: AVCaptureMetadataOutputObjectsDelegate {
437437
public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
438-
if let metadataObject = metadataObjects.first {
439-
guard !parentView.isPaused && !didFinishScanning && !isCapturing,
440-
let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject,
441-
let stringValue = readableObject.stringValue
442-
else {
443-
return
444-
}
445438

446-
handler = { [self] image in
447-
let result = ScanResult(string: stringValue, type: readableObject.type, image: image, corners: readableObject.corners)
448439

449-
switch parentView.scanMode {
450-
case .once:
440+
guard let metadataObject = metadataObjects.first,
441+
!parentView.isPaused,
442+
!didFinishScanning,
443+
!isCapturing,
444+
let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject,
445+
let stringValue = readableObject.stringValue else {
446+
447+
return
448+
}
449+
450+
handler = { [self] image in
451+
let result = ScanResult(string: stringValue, type: readableObject.type, image: image, corners: readableObject.corners)
452+
453+
switch parentView.scanMode {
454+
case .once:
455+
found(result)
456+
// make sure we only trigger scan once per use
457+
didFinishScanning = true
458+
459+
case .manual:
460+
if !didFinishScanning, isWithinManualCaptureInterval {
451461
found(result)
452-
// make sure we only trigger scan once per use
453462
didFinishScanning = true
463+
}
454464

455-
case .manual:
456-
if !didFinishScanning, isWithinManualCaptureInterval() {
457-
found(result)
458-
didFinishScanning = true
459-
}
460-
461-
case .oncePerCode:
462-
if !codesFound.contains(stringValue) {
463-
codesFound.insert(stringValue)
464-
found(result)
465-
}
465+
case .oncePerCode:
466+
if !codesFound.contains(stringValue) {
467+
codesFound.insert(stringValue)
468+
found(result)
469+
}
466470

467-
case .continuous:
468-
if isPastScanInterval() {
469-
found(result)
470-
}
471+
case .continuous:
472+
if isPastScanInterval {
473+
found(result)
474+
}
471475

472-
case .continuousExcept(let ignoredList):
473-
if isPastScanInterval() && !ignoredList.contains(stringValue) {
474-
found(result)
475-
}
476+
case .continuousExcept(let ignoredList):
477+
if isPastScanInterval, !ignoredList.contains(stringValue) {
478+
found(result)
476479
}
477480
}
481+
}
478482

479-
if parentView.requirePhotoOutput {
480-
isCapturing = true
481-
photoOutput.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
482-
} else {
483-
handler?(nil)
484-
}
483+
if parentView.requiresPhotoOutput {
484+
isCapturing = true
485+
photoOutput.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
486+
} else {
487+
handler?(nil)
485488
}
486489
}
487490
}
@@ -493,40 +496,41 @@ extension CodeScannerView.ScannerViewController: UIImagePickerControllerDelegate
493496
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
494497
isGalleryShowing = false
495498

496-
if let qrcodeImg = info[.originalImage] as? UIImage {
497-
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])!
498-
let ciImage = CIImage(image:qrcodeImg)!
499-
var qrCodeLink = ""
500-
501-
let features = detector.features(in: ciImage)
502-
503-
for feature in features as! [CIQRCodeFeature] {
504-
qrCodeLink = feature.messageString!
505-
if qrCodeLink == "" {
506-
didFail(reason: .badOutput)
507-
} else {
508-
let corners = [
509-
feature.bottomLeft,
510-
feature.bottomRight,
511-
feature.topRight,
512-
feature.topLeft
513-
]
514-
let result = ScanResult(string: qrCodeLink, type: .qr, image: qrcodeImg, corners: corners)
515-
found(result)
516-
}
499+
defer {
500+
dismiss(animated: true)
501+
}
517502

518-
}
503+
guard let qrcodeImg = info[.originalImage] as? UIImage,
504+
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]),
505+
let ciImage = CIImage(image:qrcodeImg) else {
519506

520-
} else {
521-
print("Something went wrong")
507+
return
522508
}
523509

524-
dismiss(animated: true, completion: nil)
510+
var qrCodeLink = ""
511+
512+
let features = detector.features(in: ciImage)
513+
514+
for feature in features as! [CIQRCodeFeature] {
515+
qrCodeLink = feature.messageString!
516+
if qrCodeLink.isEmpty {
517+
didFail(reason: .badOutput)
518+
} else {
519+
let corners = [
520+
feature.bottomLeft,
521+
feature.bottomRight,
522+
feature.topRight,
523+
feature.topLeft
524+
]
525+
let result = ScanResult(string: qrCodeLink, type: .qr, image: qrcodeImg, corners: corners)
526+
found(result)
527+
}
528+
}
525529
}
526530

527531
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
528532
isGalleryShowing = false
529-
dismiss(animated: true, completion: nil)
533+
dismiss(animated: true)
530534
}
531535
}
532536

0 commit comments

Comments
 (0)