@regulaforensics/cordova-plugin-document-reader-api 9.11.938-rc → 9.11.939-rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -172,6 +172,28 @@ func methodCall(_ method: String, _ callback: @escaping Callback) {
172
172
  }
173
173
  }
174
174
 
175
+ // MARK: - Completions
176
+
177
+ func generateSuccessCompletion(_ success: Bool, _ error: Error?) -> [String: Any?] {
178
+ return ["success": success, "error": error?.encode()]
179
+ }
180
+
181
+ func generateCompletion(_ action: Int, _ results: DocumentReaderResults?, _ error: Error?) -> [String: Any?] {
182
+ return [
183
+ "action": action,
184
+ "results": [0, 2, 3, 4, 6].contains(action) ? results?.encode() : nil,
185
+ "error": error?.encode(),
186
+ ]
187
+ }
188
+
189
+ func generateDeviceEngagementCompletion(_ deviceEngagement: DocReader.DeviceEngagement?, _ error: Error?) -> [String: Any?] {
190
+ return ["deviceEngagement": deviceEngagement?.encode(), "error": error?.encode()]
191
+ }
192
+
193
+ func generateFinalizePackageCompletion(_ action: Int, _ info: RGLTransactionInfo?, _ error: Error?) -> [String: Any?] {
194
+ return ["action": action, "info": info?.encode(), "error": error?.encode()]
195
+ }
196
+
175
197
  let completion: DocumentReaderCompletion = { action, results, error in
176
198
  sendEvent(completionEvent, generateCompletion(action.encode(), results, error))
177
199
  }
@@ -1,4 +1,3 @@
1
- import Foundation
2
1
  import UIKit
3
2
 
4
3
  public typealias Callback = (Any?) -> Void
@@ -63,13 +62,15 @@ func privateInit<T, Fn>(
63
62
  return call(f)!.takeUnretainedValue() as! T
64
63
  }
65
64
 
65
+ // MARK: - Converting
66
+
66
67
  extension Data {
67
68
  static func decode(_ it: Any?) -> Self? {
68
69
  guard var it = it as? String else { return nil }
69
70
  if it.hasPrefix("data") { it = String(it[it.index(after: it.firstIndex(of: ",")!)...]) }
70
71
  return Self(base64Encoded: it)
71
72
  }
72
-
73
+
73
74
  func encode() -> String {
74
75
  return self.base64EncodedString()
75
76
  }
@@ -80,8 +81,87 @@ extension UIImage {
80
81
  guard let it = it as? String else { return nil }
81
82
  return Self(data: Data.decode(it)!)
82
83
  }
83
-
84
+
84
85
  func encode() -> String {
85
86
  return self.pngData()!.encode()
86
87
  }
87
88
  }
89
+
90
+ extension UIColor {
91
+ static func decode(_ it: Any?) -> Self? {
92
+ guard let it = it as? Int else { return nil }
93
+ let value = UInt32(truncatingIfNeeded: it)
94
+ guard value >= 0x100 else {
95
+ assertionFailure("Invalid color value: #\(String(value, radix: 16)). Expected #RGB, #ARGB, #RRGGBB, or #AARRGGBB")
96
+ return nil
97
+ }
98
+ // Preserve RGB/ARGB shorthand and RRGGBB/AARRGGBB formats.
99
+ let bits = value <= 0xFFFF ? 4 : 8
100
+ let mask: UInt32 = bits == 4 ? 0xF : 0xFF
101
+ func component(_ index: Int) -> CGFloat {
102
+ return CGFloat((value >> (index * bits)) & mask) / CGFloat(mask)
103
+ }
104
+ let alpha = value < (1 << (bits * 3)) ? 1 : component(3)
105
+ return Self(red: component(2), green: component(1), blue: component(0), alpha: alpha)
106
+ }
107
+ func encode() -> Int {
108
+ var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
109
+ if !getRed(&r, green: &g, blue: &b, alpha: &a) {
110
+ if let comps = cgColor.components {
111
+ if comps.count == 2 { r = comps[0]; g = comps[0]; b = comps[0]; a = comps[1] }
112
+ else if comps.count >= 4 { r = comps[0]; g = comps[1]; b = comps[2]; a = comps[3] }
113
+ }
114
+ }
115
+ let argb = [a, r, g, b].reduce(UInt32(0)) { ($0 << 8) | UInt32(lroundf(Float($1 * 255))) }
116
+ return Int(argb)
117
+ }
118
+ }
119
+
120
+ extension UIFont {
121
+ static func decode(_ it: Any?) -> Self? {
122
+ guard let it = it as? [String: Any] else { return nil }
123
+ return Self(name: it["name"] as! String,
124
+ size: it["size"] as! CGFloat)
125
+ }
126
+ func encode() -> [String: Any?] {
127
+ return ["name": self.fontName, "size": self.pointSize]
128
+ }
129
+ }
130
+
131
+ public extension CGRect {
132
+ static func decode(_ it: Any?) -> Self {
133
+ guard let it = it as? [String: Any] else { return .zero }
134
+ let left = it["left"] as? CGFloat ?? 0
135
+ let top = it["top"] as? CGFloat ?? 0
136
+ return Self(x: left, y: top,
137
+ width: (it["right"] as? CGFloat ?? 0) - left,
138
+ height: (it["bottom"] as? CGFloat ?? 0) - top)
139
+ }
140
+ func encode() -> [String: Any?]? {
141
+ if self.origin.x == 0 && self.origin.y == 0 { return nil }
142
+ return [
143
+ "top": self.origin.y,
144
+ "left": self.origin.x,
145
+ "bottom": self.origin.y + self.size.height,
146
+ "right": self.origin.x + self.size.width,
147
+ ]
148
+ }
149
+ }
150
+
151
+ public extension CGPoint {
152
+ static func decode(_ it: Any?) -> Self {
153
+ guard let it = it as? [String: Any] else { return .zero }
154
+ return Self(x: it["x"] as? CGFloat ?? 0,
155
+ y: it["y"] as? CGFloat ?? 0)
156
+ }
157
+ func encode() -> [String: Any?] {
158
+ return ["x": self.x, "y": self.y]
159
+ }
160
+ }
161
+
162
+ public extension Error {
163
+ func encode() -> [String: Any?] {
164
+ let it = self as NSError
165
+ return ["code": it.code, "message": it.localizedDescription]
166
+ }
167
+ }