diff --git a/.github/workflows/gradle-publish.yml b/.github/workflows/gradle-publish.yml new file mode 100644 index 0000000..cc0cd4b --- /dev/null +++ b/.github/workflows/gradle-publish.yml @@ -0,0 +1,20 @@ +name: Publish package +on: + push +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + # Setup .npmrc file to publish to npm + - uses: actions/setup-node@v3 + with: + node-version: '20.x' + registry-url: 'https://registry.npmjs.org' + - run: yarn && yarn build + - run: yarn publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/ios/src/Plugin.swift b/ios/src/Plugin.swift index 57f66c8..54d28d3 100644 --- a/ios/src/Plugin.swift +++ b/ios/src/Plugin.swift @@ -9,34 +9,34 @@ open class CorePlugin: IPluginL private struct Session { let wrapperDelegate: CapacitorPluginDelegate } - + private let delegate: TDelegate private let mappers: TMappers - + public init(delegate: TDelegate, mappers: TMappers) { self.delegate = delegate self.mappers = mappers } - + private var session_: Session? = nil private var session: Session { get { return session_! } } public var wrapperDelegate: CapacitorPluginDelegate { get { return session.wrapperDelegate } } - + public func initialize(wrapperDelegate: CapacitorPluginDelegate) { session_ = Session(wrapperDelegate: wrapperDelegate) } - + public func sendEvent(_ event: ICoreEvent) { - if (event is Context) { - (event as! Context).initialize(callback: self, delegate: delegate, mappers: mappers) + if let event = event as? Context { + event.initialize(callback: self, delegate: delegate, mappers: mappers) } wrapperDelegate.sendEvent(event.name, event.getData()) } - + public func sendLog(_ action: String?, _ tag: String?, _ level: LogLevel, _ message: String, _ params: LogParams) { sendEvent(LogEvent(action: action, tag: tag, level: level, message: message, params: params)) } - + public func call(_ actionType: CoreBaseAction.Type, _ call: CAPPluginCall) { let context = CallContext(call: call, mappers: mappers) do { @@ -44,30 +44,22 @@ open class CorePlugin: IPluginL action.initialize(callback: self, delegate: delegate, mappers: mappers, call: context) try action.onExecute() } catch { - reportError(error, call: context, finish: true) + mappers.reportError(error, call: context, finish: true) } } - - public func reportSuccess(_ data: PluginCallResultData?, call: CallContext, finish: Bool) { - mappers.reportSuccess(data, call: call, finish: finish) - } - - public func reportError(_ error: Error?, call: CallContext, finish: Bool) { - mappers.reportError(error, call: call, finish: finish) - } - + public func logger(tag: String?) -> ILogger { return Logger(action: nil, tag: tag, params: nil, pluginLogger: self) } - + public func logger() -> ILogger { return logger(tag: nil) } - + public func loggerLegacy(tag: String?) -> ILoggerLegacy { return LoggerLegacy(action: nil, tag: tag, params: nil, pluginLogger: self) } - + public func loggerLegacy() -> ILoggerLegacy { return loggerLegacy(tag: nil) } diff --git a/ios/src/actions/BaseAction.swift b/ios/src/actions/BaseAction.swift index a403988..572f155 100644 --- a/ios/src/actions/BaseAction.swift +++ b/ios/src/actions/BaseAction.swift @@ -5,9 +5,9 @@ open class CoreBaseAction: ContextWithCall Void) { DispatchQueue.main.async { do { @@ -17,7 +17,7 @@ open class CoreBaseAction: ContextWithCall Void) { Task.init { do { @@ -27,35 +27,38 @@ open class CoreBaseAction: ContextWithCall ILogger { return Logger(action: getClassName(), tag: tag, params: nil, pluginLogger: callback) } - + public func logger() -> ILogger { return logger(tag: nil) } - + private func getClassName() -> String { let fullName = String(describing: self) let parts = fullName.split(separator: ".") - return parts.last!.description + return parts.last?.description ?? "Unknown class name" } - + public func sendEvent(_ event: ICoreEvent) { callback.sendEvent(event) } diff --git a/ios/src/actions/CallContext.swift b/ios/src/actions/CallContext.swift index 3ab76b6..7d3c0cb 100644 --- a/ios/src/actions/CallContext.swift +++ b/ios/src/actions/CallContext.swift @@ -3,130 +3,130 @@ import Capacitor public class CallContext { private let call: CAPPluginCall private let mappers: CoreMappers - + init(call: CAPPluginCall, mappers: CoreMappers) { self.call = call self.mappers = mappers } - + public func asObject() -> CallContextAsJsonObject { return CallContextAsJsonObject(call: call) } - - public func success(_ data: PluginCallResultData?, finish: Bool) { + + public func success(_ data: JsonObject?, finish: Bool) { call.keepAlive = !finish - if (data == nil) { + if let data = data { + call.resolve(data.toRaw() as PluginCallResultData) + } else { call.resolve() - return } - - call.resolve(data!) } - + public func error(_ error: Error?, finish: Bool) { call.keepAlive = !finish - + let errorData = prepareErrorData(error) - + let message = error?.localizedDescription ?? "Unknown error" - if (errorData == nil) { + if let errorData = errorData { + call.reject(message, nil, nil, errorData.toRaw() as PluginCallResultData) + } else { call.reject(message) - return } - - call.reject(message, nil, nil, errorData!) } - - private func prepareErrorData(_ error: Error?) -> PluginCallResultData? { - if (error == nil) { return nil } - let pluginError = mappers.errorMapper.map(error!) - return mappers.errorMapper.mapToJson(pluginError)?.toRaw() + + private func prepareErrorData(_ error: Error?) -> JsonObject? { + guard let error = error else { + return nil + } + let pluginError = mappers.errorMapper.map(error) + return mappers.errorMapper.mapToJson(pluginError) } } public class CallContextAsJsonObject: IJsonObjectProperties { private let call: CAPPluginCall - + init(call: CAPPluginCall) { self.call = call } - + public func opt(_ name: String) -> JsonValue? { return nil } - + public func optString(_ name: String) -> String? { return call.getString(name) } - + public func optInt(_ name: String) -> Int? { return call.getInt(name) } - + public func optDouble(_ name: String) -> Double? { return call.getDouble(name) } - + public func optBool(_ name: String) -> Bool? { return call.getBool(name) } - + public func optObject(_ name: String) -> JsonObject? { guard let raw = call.getObject(name) else { return nil } return JsonObject.fromRaw(raw) } - + public func optArray(_ name: String) -> JsonArray? { guard let raw = call.getArray(name) else { return nil } return JsonArray.fromRaw(raw) } - + public func get(_ name: String) throws -> JsonValue { guard let result = opt(name) else { throw PluginError(message: "Value for required '\(name)' is nil") } return result } - + public func getString(_ name: String) throws -> String { guard let result = optString(name) else { throw PluginError(message: "Value for required string '\(name)' is nil") } return result } - + public func getInt(_ name: String) throws -> Int { guard let result = optInt(name) else { throw PluginError(message: "Value for required int '\(name)' is nil") } return result } - + public func getDouble(_ name: String) throws -> Double { guard let result = optDouble(name) else { throw PluginError(message: "Value for required double '\(name)' is nil") } return result } - + public func getBool(_ name: String) throws -> Bool { guard let result = optBool(name) else { throw PluginError(message: "Value for required bool '\(name)' is nil") } return result } - + public func getObject(_ name: String) throws -> JsonObject { guard let result = optObject(name) else { throw PluginError(message: "Value for required object '\(name)' is nil") } return result } - + public func getArray(_ name: String) throws -> JsonArray { guard let result = optArray(name) else { throw PluginError(message: "Value for required array '\(name)' is nil") diff --git a/ios/src/actions/Mappers.swift b/ios/src/actions/Mappers.swift index a34adbf..896fa88 100644 --- a/ios/src/actions/Mappers.swift +++ b/ios/src/actions/Mappers.swift @@ -2,27 +2,27 @@ import Capacitor open class CoreMappers { public init() {} - - func reportSuccess(_ data: PluginCallResultData?, call: CallContext, finish: Bool) { + + func reportSuccess(_ data: JsonObject?, call: CallContext, finish: Bool) { call.success(data, finish: finish) } - + func reportError(_ error: Error?, call: CallContext, finish: Bool) { errorMapper.reportError(error, call: call, finish: finish) } - + open func createErrorMapper() -> CoreErrorMapper { return CoreErrorMapper() } - + lazy var errorMapper: CoreErrorMapper = { createErrorMapper() }() - + open func createLogMapper() -> CoreLogMapper { return CoreLogMapper() } - + lazy var logMapper: CoreLogMapper = { createLogMapper() }() @@ -30,33 +30,32 @@ open class CoreMappers { open class CoreErrorMapper { public init() {} - + open func map(_ error: Error) -> PluginError { - if (error is PluginError) { - return error as! PluginError + if let error = error as? PluginError { + return error } return PluginError(message: error.localizedDescription, cause: error) } - - open func mapToJson(_ error_: Error) -> JsonObject? { - if (!(error_ is PluginError)) { + + open func mapToJson(_ error: Error) -> JsonObject? { + guard let error = error as? PluginError else { return nil } - let error = error_ as! PluginError let data = mutableJsonObject() - + if let errorMessage = error.message { data.put("message", errorMessage) } - + if let errorCode = error.code { data.put("code", errorCode) } - + return data } - + func reportError(_ error: Error?, call: CallContext, finish: Bool) { call.error(error, finish: finish) } @@ -66,17 +65,20 @@ public typealias LogMapperObjectFormatter = (_ obj: T) throws -> JsonObject open class CoreLogMapper { private var formatters: Array<(type: Any.Type, formatter: LogMapperObjectFormatter)> = [] - + public init() { } - + public func register(type: T.Type, formatter: @escaping LogMapperObjectFormatter) { let f: LogMapperObjectFormatter = { c in - return try formatter(c as! T) + guard let c = c as? T else { + throw PluginError(message: "Object formatting error, cannot cast to required type") + } + return try formatter(c) } formatters.append((type, f)) } - + private func mapLogObject(_ obj: Any) -> JsonObject? { let type = type(of: obj) if let formatter = formatters.first(where: { it in it.type == type })?.formatter { @@ -85,7 +87,7 @@ open class CoreLogMapper { return nil } } - + internal func getLogTypeValue(_ type: LogLevel) -> String { switch(type) { case .Warning: @@ -100,7 +102,7 @@ open class CoreLogMapper { return "Trace" } } - + internal func formatParams(_ params: LogParams) -> JsonObject { let result = mutableJsonObject() for param in params { @@ -112,11 +114,11 @@ open class CoreLogMapper { } return result } - + private func formatValue(_ value: Any?) -> JsonValue? { if let value = value { - if (value is Array) { - return formatArray(value as! Array) + if let value = value as? Array { + return formatArray(value) } else if ( value is String || value is Int || @@ -128,10 +130,10 @@ open class CoreLogMapper { return formatObject(value) } } - + return nil } - + private func formatArray(_ array: Array) -> JsonArray { let result = mutableJsonArray() array.forEach { it in @@ -143,13 +145,13 @@ open class CoreLogMapper { } return result } - + private func formatObject(_ obj: Any) -> JsonValue? { if let formatted = mapLogObject(obj) { return formatted } - if (obj is AnyClass) { - return (obj as! AnyClass).description() + if let obj = obj as? AnyClass { + return obj.description() } return nil } diff --git a/ios/src/actions/PluginCallbackInternal.swift b/ios/src/actions/PluginCallbackInternal.swift index 07bd76a..79e0684 100644 --- a/ios/src/actions/PluginCallbackInternal.swift +++ b/ios/src/actions/PluginCallbackInternal.swift @@ -1,6 +1,4 @@ import Capacitor public protocol PluginCallbackInternal: IPluginLogger, IEventSender { - func reportSuccess(_ data: PluginCallResultData?, call: CallContext, finish: Bool) - func reportError(_ error: Error?, call: CallContext, finish: Bool) } diff --git a/ios/src/helpers/CallbacksHelpers.swift b/ios/src/helpers/CallbacksHelpers.swift new file mode 100644 index 0000000..c726b97 --- /dev/null +++ b/ios/src/helpers/CallbacksHelpers.swift @@ -0,0 +1,31 @@ +public typealias OnSuccess = () -> Void +public typealias OnSuccess1 = (_ value: T) -> Void +public typealias OnError = (_ cause: Error) -> Void + +public class ContinuationCallback { + private var continuation: CheckedContinuation? = nil + + public init() { + } + + public func setContinuation(_ continuation: CheckedContinuation) { + if let continuation = self.continuation { + continuation.resume(throwing: PluginError(message: "Cancelled because another callback was set")) + } + self.continuation = continuation + } + + public func onSuccess(_ result: TResult) { + if let continuation = continuation { + continuation.resume(returning: result) + } + continuation = nil + } + + public func onError(_ error: Error) { + if let continuation = continuation { + continuation.resume(throwing: error) + } + continuation = nil + } +} diff --git a/ios/src/helpers/DictionaryHelpers.swift b/ios/src/helpers/DictionaryHelpers.swift new file mode 100644 index 0000000..a965ab7 --- /dev/null +++ b/ios/src/helpers/DictionaryHelpers.swift @@ -0,0 +1,40 @@ +public typealias HashableDictionary = Dictionary + +public func cannotGet(_ name: String, type: String, from: String) -> Error { + return PluginError(message: "Cannot get \"\(name)\" \(type) from \(from)") +} + +public func getDictionaryFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> HashableDictionary { + guard let result = dict[name] as? HashableDictionary else { + throw cannotGet(name, type: "dictionary", from: "dictionary") + } + return result +} + +public func optStringFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> String? { + guard let result = dict[name] as? String? else { + throw cannotGet(name, type: "optional string", from: "dictionary") + } + return result +} + +public func getStringFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> String { + guard let result = dict[name] as? String else { + throw cannotGet(name, type: "string", from: "dictionary") + } + return result +} + +public func getDoubleFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> Double { + guard let result = dict[name] as? Double else { + throw cannotGet(name, type: "double", from: "dictionary") + } + return result +} + +public func getIntFromDictionary(_ dict: HashableDictionary, _ name: String) throws -> Int { + guard let result = dict[name] as? Int else { + throw cannotGet(name, type: "int", from: "dictionary") + } + return result +} diff --git a/ios/src/helpers/ThreadingHelpers.swift b/ios/src/helpers/ThreadingHelpers.swift new file mode 100644 index 0000000..b48eda0 --- /dev/null +++ b/ios/src/helpers/ThreadingHelpers.swift @@ -0,0 +1,9 @@ +public func runInMainThreadAsync(_ action: @escaping () async throws -> T) async throws -> T { + return try await withCheckedThrowingContinuation { c in + DispatchQueue.main.async { + Task { + c.resume(returning: try await action()) + } + } + } +} diff --git a/ios/src/helpers/UiControllerProvider.swift b/ios/src/helpers/UiControllerProvider.swift new file mode 100644 index 0000000..c2214f2 --- /dev/null +++ b/ios/src/helpers/UiControllerProvider.swift @@ -0,0 +1,30 @@ +import Capacitor + +public protocol WithUiViewControllerProvider { + func setUiViewControllerProvider(viewControllerProvider: IUiViewControllerProvider) +} + +public extension IUiViewControllerProvider? { + func requireViewController() throws -> UIViewController { + guard let viewController = self?.getViewController() else { + throw PluginError(message: "viewControllerProvider is nil") + } + return viewController + } +} + +public protocol IUiViewControllerProvider { + func getViewController() -> UIViewController? +} + +public class UiViewControllerProvider: IUiViewControllerProvider { + private let plugin: CAPPlugin + + public init(plugin: CAPPlugin) { + self.plugin = plugin + } + + public func getViewController() -> UIViewController? { + return plugin.bridge?.viewController + } +} diff --git a/ios/src/json/Helpers.swift b/ios/src/json/Helpers.swift index 224e860..4c0938b 100644 --- a/ios/src/json/Helpers.swift +++ b/ios/src/json/Helpers.swift @@ -1,9 +1,20 @@ -public func mutableJsonObject() -> MutableJsonObject { - return MutableJsonObject() +public typealias MutableJsonObjectInit = (_ obj: MutableJsonObject) -> Void +public typealias MutableJsonArrayInit = (_ arr: MutableJsonArray) -> Void + +public func mutableJsonObject(_ initFunc: MutableJsonObjectInit? = nil) -> MutableJsonObject { + let obj = MutableJsonObject() + if let initFunc = initFunc { + initFunc(obj) + } + return obj } -public func mutableJsonArray() -> MutableJsonArray { - return MutableJsonArray() +public func mutableJsonArray(_ initFunc: MutableJsonArrayInit? = nil) -> MutableJsonArray { + let arr = MutableJsonArray() + if let initFunc = initFunc { + initFunc(arr) + } + return arr } func convertFromRawJsonElement(raw: JsonValueRaw?) -> JsonValue? { @@ -16,10 +27,10 @@ func convertFromRawJsonElement(raw: JsonValueRaw?) -> JsonValue? { ) { return raw } - else if (raw is Array) { - return JsonArray(raw as! Array) - } else if (raw is Dictionary) { - return JsonObject(raw as! Dictionary) + else if let raw = raw as? Array { + return JsonArray(raw) + } else if let raw = raw as? Dictionary { + return JsonObject(raw) } } return nil diff --git a/ios/src/json/JsonArray.swift b/ios/src/json/JsonArray.swift index 02fc15f..08ff437 100644 --- a/ios/src/json/JsonArray.swift +++ b/ios/src/json/JsonArray.swift @@ -1,81 +1,85 @@ -public class JsonArray: JsonElement, IJsonArray { +public class JsonArray: JsonElement, IJsonArray, Sequence { internal var arr: Array - + internal init(_ arr: Array) { self.arr = arr } - + public static func fromRaw(_ arr: JsonArrayRaw) -> JsonArray { return JsonArray(arr) } - + public var size: Int { get { return arr.count } } - + public func opt(_ index: Int) -> JsonValue? { return convertFromRawJsonElement(raw: arr[index] as JsonValueRaw?) } - + public func optString(_ index: Int) -> String? { - return opt(index) as! String? + return opt(index) as? String? ?? nil } - + public func optInt(_ index: Int) -> Int? { - return opt(index) as! Int? + return opt(index) as? Int? ?? nil } - + public func optDouble(_ index: Int) -> Double? { - return opt(index) as! Double? + return opt(index) as? Double? ?? nil } - + public func optBool(_ index: Int) -> Bool? { - return opt(index) as! Bool? + return opt(index) as? Bool? ?? nil } - + public func optObject(_ index: Int) -> JsonObject? { - return opt(index) as! JsonObject? + return opt(index) as? JsonObject? ?? nil } - + public func optArray(_ index: Int) -> JsonArray? { - return opt(index) as! JsonArray? + return opt(index) as? JsonArray? ?? nil } - + public func get(_ index: Int) throws -> JsonValue { return try require(index, opt) } - + public func getString(_ index: Int) throws -> String { return try require(index, optString) } - + public func getInt(_ index: Int) throws -> Int { return try require(index, optInt) } - + public func getDouble(_ index: Int) throws -> Double { return try require(index, optDouble) } - + public func getBool(_ index: Int) throws -> Bool { return try require(index, optBool) } - + public func getObject(_ index: Int) throws -> JsonObject { return try require(index, optObject) } - + public func getArray(_ index: Int) throws -> JsonArray { return try require(index, optArray) } - + public func mutate() -> MutableJsonArray { return MutableJsonArray(arr) } - + public func toRaw() -> JsonArrayRaw { return arr } + + public func makeIterator() -> JsonArrayIterator { + return JsonArrayIterator(self) + } } diff --git a/ios/src/json/JsonObject.swift b/ios/src/json/JsonObject.swift index 40e7c7b..ee6689b 100644 --- a/ios/src/json/JsonObject.swift +++ b/ios/src/json/JsonObject.swift @@ -1,83 +1,83 @@ public class JsonObject: JsonElement, IJsonObject { internal var dict: Dictionary - + internal init(_ dict: Dictionary) { self.dict = dict } - + public static func fromRaw(_ dict: JsonObjectRaw) -> JsonObject { return JsonObject(dict) } - + public static func fromObject(_ obj: T) throws -> JsonObject where T: Encodable { let data = try JSONEncoder().encode(obj) let result = try JSONSerialization.jsonObject(with: data) - if (result is Dictionary) { - return JsonObject(result as! Dictionary) + if let result = result as? Dictionary { + return JsonObject(result) } throw PluginError(message: "Cannot create json object") } - + public func opt(_ name: String) -> JsonValue? { return convertFromRawJsonElement(raw: dict[name] as JsonValueRaw?) } - + public func optString(_ name: String) -> String? { - return opt(name) as! String? + return opt(name) as? String? ?? nil } - + public func optInt(_ name: String) -> Int? { - return opt(name) as! Int? + return opt(name) as? Int? ?? nil } - + public func optDouble(_ name: String) -> Double? { - return opt(name) as! Double? + return opt(name) as? Double? ?? nil } - + public func optBool(_ name: String) -> Bool? { - return opt(name) as! Bool? + return opt(name) as? Bool? ?? nil } - + public func optObject(_ name: String) -> JsonObject? { - return opt(name) as! JsonObject? + return opt(name) as? JsonObject? ?? nil } - + public func optArray(_ name: String) -> JsonArray? { - return opt(name) as! JsonArray? + return opt(name) as? JsonArray? ?? nil } - + public func get(_ name: String) throws -> JsonValue { return try require(name, opt) } - + public func getString(_ name: String) throws -> String { return try require(name, optString) } - + public func getInt(_ name: String) throws -> Int { return try require(name, optInt) } - + public func getDouble(_ name: String) throws -> Double { return try require(name, optDouble) } - + public func getBool(_ name: String) throws -> Bool { return try require(name, optBool) } - + public func getObject(_ name: String) throws -> JsonObject { return try require(name, optObject) } - + public func getArray(_ name: String) throws -> JsonArray { return try require(name, optArray) } - + public func mutate() -> MutableJsonObject { return MutableJsonObject(dict) } - + public func toRaw() -> JsonObjectRaw { return dict } diff --git a/ios/src/json/MutableJsonArray.swift b/ios/src/json/MutableJsonArray.swift index c14ac71..83f2ba5 100644 --- a/ios/src/json/MutableJsonArray.swift +++ b/ios/src/json/MutableJsonArray.swift @@ -2,55 +2,55 @@ public class MutableJsonArray: JsonArray, IMutableJsonArray { override init(_ arr: Array) { super.init(arr) } - + internal convenience init() { self.init([]) } - + public func put(_ value: String, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: Int, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: Double, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: Bool, index: Int? = nil) { putInternal(value, index) } - + public func put(_ value: JsonObject, index: Int? = nil) { putInternal(value.dict, index) } - + public func put(_ value: JsonArray, index: Int? = nil) { putInternal(value.arr, index) } - + public func putNull(index: Int? = nil) { putInternal(nil, index) } - + public func put(_ value: JsonValue, index: Int? = nil) { - if (value is String) { - put(value as! String, index: index) - } else if (value is Int) { - put(value as! Int, index: index) - } else if (value is Double) { - put(value as! Double, index: index) - } else if (value is Bool) { - put(value as! Bool, index: index) - } else if (value is JsonObject) { - put(value as! JsonObject, index: index) - } else if (value is JsonArray) { - put(value as! JsonArray, index: index) + if let value = value as? String { + put(value, index: index) + } else if let value = value as? Int { + put(value, index: index) + } else if let value = value as? Double { + put(value, index: index) + } else if let value = value as? Bool { + put(value, index: index) + } else if let value = value as? JsonObject { + put(value, index: index) + } else if let value = value as? JsonArray { + put(value, index: index) } } - + private func putInternal(_ value: JsonValueRaw?, _ index: Int?) { arr.append(value) } diff --git a/ios/src/json/MutableJsonObject.swift b/ios/src/json/MutableJsonObject.swift index 6eb84c2..012c126 100644 --- a/ios/src/json/MutableJsonObject.swift +++ b/ios/src/json/MutableJsonObject.swift @@ -2,55 +2,55 @@ public class MutableJsonObject: JsonObject, IMutableJsonObject { override init(_ dict: Dictionary) { super.init(dict) } - + internal convenience init() { self.init([:]) } - + public func put(_ name: String, _ value: String) { putInternal(name, value) } - + public func put(_ name: String, _ value: Int) { putInternal(name, value) } - + public func put(_ name: String, _ value: Double) { putInternal(name, value) } - + public func put(_ name: String, _ value: Bool) { putInternal(name, value) } - + public func put(_ name: String, _ value: JsonObject) { putInternal(name, value.dict) } - + public func put(_ name: String, _ value: JsonArray) { putInternal(name, value.arr) } - + public func putNull(_ name: String) { putInternal(name, nil) } - + public func put(_ name: String, _ value: JsonValue) { - if (value is String) { - put(name, value as! String) - } else if (value is Int) { - put(name, value as! Int) - } else if (value is Double) { - put(name, value as! Double) - } else if (value is Bool) { - put(name, value as! Bool) - } else if (value is JsonObject) { - put(name, value as! JsonObject) - } else if (value is JsonArray) { - put(name, value as! JsonArray) + if let value = value as? String { + put(name, value) + } else if let value = value as? Int { + put(name, value) + } else if let value = value as? Double { + put(name, value) + } else if let value = value as? Bool { + put(name, value) + } else if let value = value as? JsonObject { + put(name, value) + } else if let value = value as? JsonArray { + put(name, value) } } - + private func putInternal(_ name: String, _ value: JsonValueRaw?) { dict[name] = value } diff --git a/ios/src/json/utils/JsonArrayIterator.swift b/ios/src/json/utils/JsonArrayIterator.swift new file mode 100644 index 0000000..6140d25 --- /dev/null +++ b/ios/src/json/utils/JsonArrayIterator.swift @@ -0,0 +1,16 @@ +public class JsonArrayIterator: IteratorProtocol { + public typealias Element = JsonValue + + private let jsonArray: JsonArray + + private var index = -1 + + init(_ jsonArray: JsonArray) { + self.jsonArray = jsonArray + } + + public func next() -> JsonValue? { + index += 1; + return jsonArray.opt(index) + } +} diff --git a/package.json b/package.json index 92a3536..098404c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spryrocks/capacitor-ionic-core-plugin", - "version": "2.5.6-alpha.0", + "version": "2.7.0-alpha.0", "description": "Ionic plugin core capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js",