Skip to content

Commit eef1a40

Browse files
committed
refactor: return object instead json string
1 parent 16ecd85 commit eef1a40

4 files changed

Lines changed: 21 additions & 65 deletions

File tree

Sources/IONCameraLib/Extensions/IONCAMRFlowResultsDelegate+Default.swift

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@ extension IONCAMRFlowResultsHandler {
88
func didReturn(_ result: Result<any Encodable, IONCAMRError>) {
99
switch result {
1010
case .success(let value):
11-
do {
12-
if let mediaResult = value as? IONCAMRMediaResult {
13-
let mediaResultText = try self.treatSingle(mediaResult)
14-
self.responseDelegate?.callback(result: mediaResultText)
15-
} else if let mediaArray = value as? [IONCAMRMediaResult] {
16-
let mediaArrayText = try self.treatMultiple(mediaArray)
17-
self.responseDelegate?.callback(result: mediaArrayText)
18-
}
19-
} catch let error as IONCAMRError {
20-
self.responseDelegate?.callback(error: error)
21-
} catch {
22-
self.responseDelegate?.callback(error: .generalIssue)
11+
if let mediaResult = value as? IONCAMRMediaResult {
12+
self.responseDelegate?.callback(result: mediaResult)
13+
} else if let mediaArray = value as? [IONCAMRMediaResult] {
14+
self.responseDelegate?.callback(result: mediaArray)
2315
}
2416
case .failure(let error):
2517
self.responseDelegate?.callback(error: error)
@@ -29,23 +21,4 @@ extension IONCAMRFlowResultsHandler {
2921
func didCancel(_ error: IONCAMRError) {
3022
self.responseDelegate?.callback(error: error)
3123
}
32-
33-
private func encodeToStructure<T>(_ value: T) throws -> String where T: Encodable {
34-
let encoder = JSONEncoder()
35-
encoder.dateEncodingStrategy = .iso8601
36-
guard let mediaResultData = try? encoder.encode(value), let mediaResultText = String(data: mediaResultData, encoding: .utf8)
37-
else { throw IONCAMRError.invalidEncodeResultMedia }
38-
return mediaResultText
39-
}
40-
41-
private func treatSingle(_ value: IONCAMRMediaResult) throws -> String {
42-
if case .picture = value.type, value.uri.isEmpty {
43-
return value.thumbnail
44-
}
45-
return try self.encodeToStructure(value)
46-
}
47-
48-
private func treatMultiple(_ value: [IONCAMRMediaResult]) throws -> String {
49-
try self.encodeToStructure(value)
50-
}
5124
}

Sources/IONCameraLib/Models/IONCAMRMediaResult.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
struct IONCAMRMediaResult {
2-
let type: IONCAMRMediaType
3-
let uri: String
4-
let thumbnail: String
5-
let metadata: IONCAMRMetadata?
6-
let saved: Bool?
1+
public struct IONCAMRMediaResult {
2+
public let type: IONCAMRMediaType
3+
public let uri: String
4+
public let thumbnail: String
5+
public let metadata: IONCAMRMetadata?
6+
public let saved: Bool?
77

88
init(type: IONCAMRMediaType, uri: String, thumbnail: String, metadata: IONCAMRMetadata? = nil, saved: Bool? = nil ) {
99
self.type = type
@@ -33,7 +33,7 @@ extension IONCAMRMediaResult: Encodable {
3333
case type, uri, thumbnail, metadata, saved
3434
}
3535

36-
func encode(to encoder: Encoder) throws {
36+
public func encode(to encoder: Encoder) throws {
3737
var container = encoder.container(keyedBy: CodingKeys.self)
3838
try container.encode(self.type.enumerator.rawValue, forKey: .type)
3939
try container.encode(self.uri, forKey: .uri)

Sources/IONCameraLib/Models/IONCAMRMetadata.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Foundation
22

3-
struct IONCAMRMetadata {
4-
var size: UInt64
5-
var duration: Int?
6-
var format: String
7-
var resolution: String
8-
var creationDate: Date
3+
public struct IONCAMRMetadata {
4+
public var size: UInt64
5+
public var duration: Int?
6+
public var format: String
7+
public var resolution: String
8+
public var creationDate: Date
99

1010
init(size: UInt64, duration: Int? = nil, format: String, resolution: String, creationDate: Date) {
1111
self.size = size
@@ -21,7 +21,7 @@ extension IONCAMRMetadata: Encodable {
2121
case size, duration, format, resolution, creationDate
2222
}
2323

24-
func encode(to encoder: Encoder) throws {
24+
public func encode(to encoder: Encoder) throws {
2525
var container = encoder.container(keyedBy: CodingKeys.self)
2626
try container.encode(self.size, forKey: .size)
2727
try container.encodeIfPresent(self.duration, forKey: .duration)
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
/// Delegate for the callback return calls for the plugin
22
public protocol IONCAMRCallbackDelegate: AnyObject {
3-
/// Callback to be triggered.
4-
/// - Parameters:
5-
/// - result: The text to be returned in case of success.
6-
/// - error: The error to be returned in case of error.
7-
func callback(result: String?, error: IONCAMRError?)
8-
}
9-
10-
/// Provides accelerators for callbacks.
11-
extension IONCAMRCallbackDelegate {
12-
/// Triggers the callback when there's an error
13-
/// - Parameter error: Error to be thrown
14-
func callback(error: IONCAMRError) {
15-
self.callback(result: nil, error: error)
16-
}
17-
18-
/// Triggers the callback when there's a success text
19-
/// - Parameter result: Text to be returned
20-
func callback(result: String) {
21-
self.callback(result: result, error: nil)
22-
}
3+
func callback(error: IONCAMRError)
4+
func callback(result: IONCAMRMediaResult)
5+
func callback(result: [IONCAMRMediaResult])
236
}

0 commit comments

Comments
 (0)