Skip to content

Commit 9d5bde3

Browse files
feat(android,ios)!: Provide hint and return barcode format (#44)
* feat: Allow passing hint and correctly deserialize. * tmp: Use local aar for native library * chore: Fix temporary aar location * tmp: use xcframework instead of podspec * feat(ios): Pass hint to scanner * refactor(ios): Creation of scan parameters Parameter model is now on native library (which allows for more flexibility in adding new parameters), but decoding is still in plugin. * fix(ios): Simplify decoding of parameters * feat!: pass ScanResult object References: https://outsystemsrd.atlassian.net/browse/RMET-4433 BREAKING CHANGE: This modifies what is returned by the plugin, requires changes by the client. * tmp: Update temporary aar and xcframework * fix(ios): plugin.xml declarations * tmp: update xcframework * chore: Use stable versions of native libraries * fix(android): Library declaration in build.gradle * chore: prepare for release of 2.0.0 * chore: remove @retroactive Only available since Swift 6, which not all projects will necessarily use. * chore: Update iOS library pod
1 parent 2f28286 commit 9d5bde3

File tree

7 files changed

+65
-30
lines changed

7 files changed

+65
-30
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
The changes documented here do not include those from the original repository.
88

9+
## [2.0.0]
10+
11+
### 01-09-2025
12+
13+
- Feature: Android - provide `hint` and return `format`.
14+
15+
**BREAKING CHANGES**: The `scanBarcode` now returns an object instead of a string.
16+
917
## [1.2.1]
1018

1119
### 20-08-2025

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.outsystems.plugins.barcode",
3-
"version": "1.2.1",
3+
"version": "2.0.0",
44
"description": "Cordova Bridge for the OutSystems Officially Supported Barcode Plugin.",
55
"keywords": [
66
"ecosystem:cordova",

plugin.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<plugin id="com.outsystems.plugins.barcode" version="1.2.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
2+
<plugin id="com.outsystems.plugins.barcode" version="2.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
33
<name>OSBarcode</name>
44
<description>Cordova Bridge for the OutSystems Officially Supported Barcode Plugin.</description>
55
<author>OutSystems Inc</author>
@@ -26,6 +26,7 @@
2626
<config-file parent="/*" target="AndroidManifest.xml"/>
2727
<source-file src="src/android/com/outsystems/plugins/barcode/OSBarcode.kt" target-dir="app/src/main/kotlin/com/outsystems/plugins/barcode"/>
2828
<framework src="src/android/com/outsystems/plugins/barcode/build.gradle" custom="true" type="gradleReference" />
29+
2930
</platform>
3031
<platform name="ios">
3132
<config-file parent="/*" target="config.xml">
@@ -42,16 +43,16 @@
4243

4344
<source-file src="src/ios/OSBarcode.swift" />
4445
<source-file src="src/ios/OSBarcodeError.swift" />
45-
<source-file src="src/ios/OSBarcodeScanArgumentsModel.swift" />
46-
46+
4747
<source-file src="src/ios/OSBARCArgumentMappable.swift" target-dir="extensions" />
48+
<source-file src="src/ios/OSBARCScanParameters+Decodable.swift" target-dir="extensions" />
4849

4950
<podspec>
5051
<config>
5152
<source url="https://cdn.cocoapods.org/"/>
5253
</config>
5354
<pods use-frameworks="true">
54-
<pod name="OSBarcodeLib" spec="1.1.3" />
55+
<pod name="OSBarcodeLib" spec="2.0.1" />
5556
</pods>
5657
</podspec>
5758
</platform>

src/android/com/outsystems/plugins/barcode/OSBarcode.kt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@ package com.outsystems.plugins.barcode
22

33
import android.content.Intent
44
import com.google.gson.Gson
5+
import com.google.gson.GsonBuilder
6+
import com.google.gson.JsonDeserializationContext
7+
import com.google.gson.JsonDeserializer
8+
import com.google.gson.JsonElement
59
import com.outsystems.plugins.barcode.controller.OSBARCController
610
import com.outsystems.plugins.barcode.model.OSBARCError
711
import com.outsystems.plugins.barcode.model.OSBARCScanParameters
12+
import com.outsystems.plugins.barcode.model.OSBARCScanResult
13+
import com.outsystems.plugins.barcode.model.OSBARCScannerHint
814
import com.outsystems.plugins.oscordova.CordovaImplementation
9-
import kotlinx.coroutines.runBlocking
1015
import org.apache.cordova.CallbackContext
1116
import org.apache.cordova.CordovaInterface
1217
import org.apache.cordova.CordovaWebView
1318
import org.json.JSONArray
19+
import org.json.JSONObject
20+
import java.lang.reflect.Type
1421

1522
class OSBarcode : CordovaImplementation() {
16-
1723
companion object {
1824
private const val ERROR_FORMAT_PREFIX = "OS-PLUG-BARC-"
1925
}
2026

2127
override var callbackContext: CallbackContext? = null
2228
private lateinit var barcodeController: OSBARCController
23-
val gson by lazy { Gson() }
29+
val gson: Gson by lazy {
30+
GsonBuilder()
31+
.registerTypeAdapter(OSBARCScannerHint::class.java, OSBARCScannerHintAdapter())
32+
.create()
33+
}
2434

2535
override fun execute(
2636
action: String,
@@ -45,8 +55,12 @@ class OSBarcode : CordovaImplementation() {
4555
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
4656
super.onActivityResult(requestCode, resultCode, intent)
4757
barcodeController.handleActivityResult(requestCode, resultCode, intent,
48-
{ result ->
49-
sendPluginResult(result, null)
58+
{ result: OSBARCScanResult ->
59+
val jsonObject = JSONObject().apply {
60+
put("ScanResult", result.text)
61+
put("format", result.format.ordinal)
62+
}
63+
sendPluginResult(jsonObject, null)
5064
},
5165
{ error ->
5266
sendPluginResult(null, Pair(formatErrorCode(error.code), error.description))
@@ -98,4 +112,16 @@ class OSBarcode : CordovaImplementation() {
98112
return ERROR_FORMAT_PREFIX + code.toString().padStart(4, '0')
99113
}
100114

115+
private class OSBARCScannerHintAdapter : JsonDeserializer<OSBARCScannerHint> {
116+
override fun deserialize(
117+
json: JsonElement?,
118+
typeOfT: Type?,
119+
context: JsonDeserializationContext?
120+
): OSBARCScannerHint {
121+
return json?.asInt?.let {
122+
OSBARCScannerHint.entries.getOrNull(it)
123+
} ?: OSBARCScannerHint.UNKNOWN
124+
}
125+
}
126+
101127
}

src/android/com/outsystems/plugins/barcode/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repositories{
2626
dependencies {
2727
implementation("com.github.outsystems:oscore-android:1.2.0@aar")
2828
implementation("com.github.outsystems:oscordova-android:2.0.1@aar")
29-
implementation("io.ionic.libs:ionbarcode-android:1.2.1@aar")
29+
implementation("io.ionic.libs:ionbarcode-android:2.0.0@aar")
3030

3131
implementation 'androidx.appcompat:appcompat:1.7.0'
3232
implementation "androidx.activity:activity-ktx:1.9.3"
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import OSBarcodeLib
22

3-
struct OSBarcodeScanArgumentsModel: Decodable {
4-
let scanInstructions: String
5-
let scanButtonText: String?
6-
let cameraDirection: OSBARCCameraModel
7-
let scanOrientation: OSBARCOrientationModel
3+
extension OSBARCScanParameters: Decodable {
84

95
enum CodingKeys: CodingKey {
106
case scanButton
117
case scanInstructions
128
case scanText
139
case cameraDirection
1410
case scanOrientation
11+
case hint
1512
}
1613

17-
init(from decoder: Decoder) throws {
14+
public init(from decoder: Decoder) throws {
1815
let container = try decoder.container(keyedBy: CodingKeys.self)
1916

2017
let scanInstructions = try container.decode(String.self, forKey: .scanInstructions)
@@ -31,13 +28,15 @@ struct OSBarcodeScanArgumentsModel: Decodable {
3128
let scanOrientationInt = try container.decode(Int.self, forKey: .scanOrientation)
3229
let scanOrientation = OSBARCOrientationModel(value: scanOrientationInt)
3330

34-
self.init(scanInstructions, scanButtonText, cameraDirection, scanOrientation)
35-
}
36-
37-
private init(_ scanInstructions: String, _ scanButtonText: String?, _ cameraDirection: OSBARCCameraModel, _ scanOrientation: OSBARCOrientationModel) {
38-
self.scanInstructions = scanInstructions
39-
self.scanButtonText = scanButtonText
40-
self.cameraDirection = cameraDirection
41-
self.scanOrientation = scanOrientation
31+
let hintInt = try container.decode(Int.self, forKey: .hint)
32+
let hint = OSBARCScannerHint(rawValue: hintInt)
33+
34+
self.init(
35+
scanInstructions: scanInstructions,
36+
scanButtonText: scanButtonText,
37+
cameraDirection: cameraDirection,
38+
scanOrientation: scanOrientation,
39+
hint: hint
40+
)
4241
}
4342
}

src/ios/OSBarcode.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class OSBarcode: CDVPlugin {
1717

1818
guard let argumentsDictionary = command.argument(at: 0) as? [String: Any],
1919
let argumentsData = try? JSONSerialization.data(withJSONObject: argumentsDictionary),
20-
let argumentsModel = try? JSONDecoder().decode(OSBarcodeScanArgumentsModel.self, from: argumentsData)
20+
let parameters = try? JSONDecoder().decode(OSBARCScanParameters.self, from: argumentsData)
2121
else { return self.send(error: .scanInputArgumentsIssue, for: command.callbackId) }
2222

2323
Task {
2424
do {
25-
guard let scannedBarcode = try await self.plugin?.scanBarcode(with: argumentsModel.scanInstructions, argumentsModel.scanButtonText, argumentsModel.cameraDirection, and: argumentsModel.scanOrientation) else {
25+
guard let scanResult = try await self.plugin?.scanBarcode(with: parameters) else {
2626
return self.send(error: .scanningError, for: command.callbackId)
2727
}
28-
self.send(successfulResult: scannedBarcode, for: command.callbackId)
28+
self.send(successfulResult: scanResult, for: command.callbackId)
2929
} catch OSBARCManagerError.cameraAccessDenied {
3030
self.send(error: .cameraAccessDenied, for: command.callbackId)
3131
} catch OSBARCManagerError.scanningCancelled {
@@ -37,8 +37,9 @@ class OSBarcode: CDVPlugin {
3737
}
3838

3939
private extension OSBarcode {
40-
func send(successfulResult: String, for callbackId: String) {
41-
let pluginResult = CDVPluginResult(status: .ok, messageAs: successfulResult)
40+
func send(successfulResult: OSBARCScanResult, for callbackId: String) {
41+
let resultDict: [String : Any] = ["ScanResult": successfulResult.text, "format": successfulResult.format.rawValue]
42+
let pluginResult = CDVPluginResult(status: .ok, messageAs: resultDict)
4243
self.commandDelegate.send(pluginResult, callbackId: callbackId)
4344
}
4445

0 commit comments

Comments
 (0)