Skip to content

Commit

Permalink
wip: ios
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Dec 20, 2023
1 parent e21a5c2 commit aae2cc5
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/barcode-scanning/ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
50E1A94820377CB70090CE1A /* BarcodeScannerPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E1A94720377CB70090CE1A /* BarcodeScannerPlugin.swift */; };
7C39C34F29A0BB4600AE60BB /* ScanSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C39C34E29A0BB4600AE60BB /* ScanSettings.swift */; };
7C8E4B5E2998FE7B00A35D56 /* BarcodeScannerHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C8E4B5D2998FE7B00A35D56 /* BarcodeScannerHelper.swift */; };
7C9BA37D2B3350D300A5BED8 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9BA37C2B3350D300A5BED8 /* Result.swift */; };
7C9BA37F2B33510600A5BED8 /* SetZoomRatioOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9BA37E2B33510600A5BED8 /* SetZoomRatioOptions.swift */; };
7C9BA3812B33511100A5BED8 /* GetMaxZoomRatioResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9BA3802B33511100A5BED8 /* GetMaxZoomRatioResult.swift */; };
7C9BA3832B33511800A5BED8 /* GetMinZoomRatioResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9BA3822B33511800A5BED8 /* GetMinZoomRatioResult.swift */; };
7C9BA3852B33512000A5BED8 /* GetZoomRatioResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9BA3842B33512000A5BED8 /* GetZoomRatioResult.swift */; };
7CD8CBAE299902D90039652D /* BarcodeScannerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD8CBAD299902D80039652D /* BarcodeScannerView.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -46,6 +51,11 @@
5E23F77F099397094342571A /* Pods-Plugin.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Plugin.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Plugin/Pods-Plugin.debug.xcconfig"; sourceTree = "<group>"; };
7C39C34E29A0BB4600AE60BB /* ScanSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScanSettings.swift; sourceTree = "<group>"; };
7C8E4B5D2998FE7B00A35D56 /* BarcodeScannerHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BarcodeScannerHelper.swift; sourceTree = "<group>"; };
7C9BA37C2B3350D300A5BED8 /* Result.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
7C9BA37E2B33510600A5BED8 /* SetZoomRatioOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetZoomRatioOptions.swift; sourceTree = "<group>"; };
7C9BA3802B33511100A5BED8 /* GetMaxZoomRatioResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetMaxZoomRatioResult.swift; sourceTree = "<group>"; };
7C9BA3822B33511800A5BED8 /* GetMinZoomRatioResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetMinZoomRatioResult.swift; sourceTree = "<group>"; };
7C9BA3842B33512000A5BED8 /* GetZoomRatioResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetZoomRatioResult.swift; sourceTree = "<group>"; };
7CD8CBAD299902D80039652D /* BarcodeScannerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BarcodeScannerView.swift; sourceTree = "<group>"; };
91781294A431A2A7CC6EB714 /* Pods-Plugin.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Plugin.release.xcconfig"; path = "Pods/Target Support Files/Pods-Plugin/Pods-Plugin.release.xcconfig"; sourceTree = "<group>"; };
96ED1B6440D6672E406C8D19 /* Pods-PluginTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PluginTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PluginTests/Pods-PluginTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -98,6 +108,8 @@
50ADFF8A201F53D600D50D53 /* Plugin */ = {
isa = PBXGroup;
children = (
7C9BA3792B3350B100A5BED8 /* Classes */,
7C9BA37B2B3350C600A5BED8 /* Protocols */,
50E1A94720377CB70090CE1A /* BarcodeScannerPlugin.swift */,
2F98D68124C9AAE400613A4C /* BarcodeScanner.swift */,
50ADFF8B201F53D600D50D53 /* BarcodeScannerPlugin.h */,
Expand All @@ -119,6 +131,41 @@
path = PluginTests;
sourceTree = "<group>";
};
7C9BA3782B3350AB00A5BED8 /* Options */ = {
isa = PBXGroup;
children = (
7C9BA37E2B33510600A5BED8 /* SetZoomRatioOptions.swift */,
);
path = Options;
sourceTree = "<group>";
};
7C9BA3792B3350B100A5BED8 /* Classes */ = {
isa = PBXGroup;
children = (
7C9BA37A2B3350B800A5BED8 /* Results */,
7C9BA3782B3350AB00A5BED8 /* Options */,
);
path = Classes;
sourceTree = "<group>";
};
7C9BA37A2B3350B800A5BED8 /* Results */ = {
isa = PBXGroup;
children = (
7C9BA3802B33511100A5BED8 /* GetMaxZoomRatioResult.swift */,
7C9BA3822B33511800A5BED8 /* GetMinZoomRatioResult.swift */,
7C9BA3842B33512000A5BED8 /* GetZoomRatioResult.swift */,
);
path = Results;
sourceTree = "<group>";
};
7C9BA37B2B3350C600A5BED8 /* Protocols */ = {
isa = PBXGroup;
children = (
7C9BA37C2B3350D300A5BED8 /* Result.swift */,
);
path = Protocols;
sourceTree = "<group>";
};
8C8E7744173064A9F6D438E3 /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -327,12 +374,17 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7C9BA37F2B33510600A5BED8 /* SetZoomRatioOptions.swift in Sources */,
7C9BA3832B33511800A5BED8 /* GetMinZoomRatioResult.swift in Sources */,
50E1A94820377CB70090CE1A /* BarcodeScannerPlugin.swift in Sources */,
7C9BA3812B33511100A5BED8 /* GetMaxZoomRatioResult.swift in Sources */,
2F98D68224C9AAE500613A4C /* BarcodeScanner.swift in Sources */,
50ADFFA82020EE4F00D50D53 /* BarcodeScannerPlugin.m in Sources */,
7CD8CBAE299902D90039652D /* BarcodeScannerView.swift in Sources */,
7C9BA37D2B3350D300A5BED8 /* Result.swift in Sources */,
7C8E4B5E2998FE7B00A35D56 /* BarcodeScannerHelper.swift in Sources */,
7C39C34F29A0BB4600AE60BB /* ScanSettings.swift in Sources */,
7C9BA3852B33512000A5BED8 /* GetZoomRatioResult.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
32 changes: 32 additions & 0 deletions packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner
return device.hasTorch
}

@objc public func setZoomRatio(_ options: SetZoomRatioOptions) throws {
let zoomRatio = options.getZoomRatio()

guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
return
}
try device.lockForConfiguration()
device.videoZoomFactor = zoomRatio
device.unlockForConfiguration()
}

@objc public func getZoomRatio() -> GetZoomRatioResult? {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
return nil
}
return GetZoomRatioResult(zoomRatio: device.videoZoomFactor)
}

@objc public func getMinZoomRatio() -> GetMinZoomRatioResult? {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
return nil
}
return GetMinZoomRatioResult(zoomRatio: device.minAvailableVideoZoomFactor)
}

@objc public func getMaxZoomRatio() -> GetMaxZoomRatioResult? {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
return nil
}
return GetMaxZoomRatioResult(zoomRatio: device.maxAvailableVideoZoomFactor)
}

@objc func openSettings(completion: @escaping (Error?) -> Void) {
let url = URL(string: UIApplication.openSettingsURLString)
DispatchQueue.main.async {
Expand Down
4 changes: 4 additions & 0 deletions packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
CAP_PLUGIN_METHOD(toggleTorch, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isTorchEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isTorchAvailable, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setZoomRatio, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getZoomRatio, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getMinZoomRatio, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getMaxZoomRatio, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openSettings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isGoogleBarcodeScannerModuleAvailable, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(installGoogleBarcodeScannerModule, CAPPluginReturnPromise);
Expand Down
49 changes: 49 additions & 0 deletions packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import MLKitBarcodeScanning
*/
@objc(BarcodeScannerPlugin)
public class BarcodeScannerPlugin: CAPPlugin {
public let tag = "BarcodeScanner"
public let errorPathMissing = "path must be provided."
public let errorFileNotExist = "File does not exist."
public let errorInvalidImage = "The file is no valid image."
public let errorNoCaptureDeviceAvailable = "No capture device available."
public let errorCannotAddCaptureInput = "Cannot add input to capture session."
public let errorCannotAddCaptureOutput = "Cannot add output to capture session."
public let errorScanCanceled = "scan canceled."
public let errorZoomRatioMissing = "zoomRatio must be provided."
public let errorPermissionDenied = "User denied access to camera."
public let errorOpenSettingsFailed = "Cannot open settings."
public let barcodeScannedEvent = "barcodeScanned"
Expand Down Expand Up @@ -155,6 +157,53 @@ public class BarcodeScannerPlugin: CAPPlugin {
])
}

@objc func setZoomRatio(_ call: CAPPluginCall) {
guard let zoomRatio = call.getFloat("zoomRatio") else {
call.reject(errorZoomRatioMissing)
return
}

let options = SetZoomRatioOptions(zoomRatio: zoomRatio)

do {
try implementation?.setZoomRatio(options)
call.resolve()
} catch {
CAPLog.print("[", self.tag, "] ", error)
call.reject(error.localizedDescription)
}
}

@objc func getZoomRatio(_ call: CAPPluginCall) {
guard let result = implementation?.getZoomRatio() else {
call.reject(errorNoCaptureDeviceAvailable)
return
}
if let result = result.toJSObject() as? JSObject {
call.resolve(result)
}
}

@objc func getMinZoomRatio(_ call: CAPPluginCall) {
guard let result = implementation?.getMinZoomRatio() else {
call.reject(errorNoCaptureDeviceAvailable)
return
}
if let result = result.toJSObject() as? JSObject {
call.resolve(result)
}
}

@objc func getMaxZoomRatio(_ call: CAPPluginCall) {
guard let result = implementation?.getMaxZoomRatio() else {
call.reject(errorNoCaptureDeviceAvailable)
return
}
if let result = result.toJSObject() as? JSObject {
call.resolve(result)
}
}

@objc func openSettings(_ call: CAPPluginCall) {
implementation?.openSettings(completion: { error in
if let error = error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import Capacitor

@objc public class SetZoomRatioOptions: NSObject {
private var zoomRatio: CGFloat

init(zoomRatio: Float) {
self.zoomRatio = CGFloat(zoomRatio)
}

func getZoomRatio() -> CGFloat {
return zoomRatio
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import Capacitor

@objc public class GetMaxZoomRatioResult: NSObject, Result {
let zoomRatio: Float

init(zoomRatio: CGFloat) {
self.zoomRatio = Float(zoomRatio)
}

public func toJSObject() -> AnyObject {
var result = JSObject()
result["zoomRatio"] = zoomRatio
return result as AnyObject
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import Capacitor

@objc public class GetMinZoomRatioResult: NSObject, Result {
let zoomRatio: Float

init(zoomRatio: CGFloat) {
self.zoomRatio = Float(zoomRatio)
}

public func toJSObject() -> AnyObject {
var result = JSObject()
result["zoomRatio"] = zoomRatio
return result as AnyObject
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import Capacitor

@objc public class GetZoomRatioResult: NSObject, Result {
let zoomRatio: Float

init(zoomRatio: CGFloat) {
self.zoomRatio = Float(zoomRatio)
}

public func toJSObject() -> AnyObject {
var result = JSObject()
result["zoomRatio"] = zoomRatio
return result as AnyObject
}
}
6 changes: 6 additions & 0 deletions packages/barcode-scanning/ios/Plugin/Protocols/Result.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import Capacitor

@objc public protocol Result {
@objc func toJSObject() -> AnyObject
}

0 comments on commit aae2cc5

Please sign in to comment.