Skip to content

Commit

Permalink
Ios refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.zhemerenko committed Dec 2, 2023
1 parent b8497fa commit 8035e1a
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
28 changes: 28 additions & 0 deletions ios/src/helpers/CallbacksHelpers.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
public typealias OnSuccess = () -> Void
public typealias OnSuccess1<T> = (_ value: T) -> Void
public typealias OnError = (_ cause: Error) -> Void

public class ContinuationCallback<TResult> {
private var continuation: CheckedContinuation<TResult, Error>? = nil

public init() {
}

public func setContinuation(_ continuation: CheckedContinuation<TResult, Error>) {
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
}
}
9 changes: 9 additions & 0 deletions ios/src/helpers/ThreadingHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public func runInMainThreadAsync<T>(_ action: @escaping () async throws -> T) async throws -> T {
return try await withCheckedThrowingContinuation { c in
DispatchQueue.main.async {
Task {
c.resume(returning: try await action())
}
}
}
}
30 changes: 30 additions & 0 deletions ios/src/helpers/UiControllerProvider.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 2 additions & 2 deletions ios/src/json/Helpers.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
public typealias MutableJsonObjectInit = (_ obj: MutableJsonObject) -> Void
public typealias MutableJsonArrayInit = (_ arr: MutableJsonArray) -> Void

public func mutableJsonObject(initFunc: MutableJsonObjectInit? = nil) -> MutableJsonObject {
public func mutableJsonObject(_ initFunc: MutableJsonObjectInit? = nil) -> MutableJsonObject {
let obj = MutableJsonObject()
if let initFunc = initFunc {
initFunc(obj)
}
return obj
}

public func mutableJsonArray(initFunc: MutableJsonArrayInit? = nil) -> MutableJsonArray {
public func mutableJsonArray(_ initFunc: MutableJsonArrayInit? = nil) -> MutableJsonArray {
let arr = MutableJsonArray()
if let initFunc = initFunc {
initFunc(arr)
Expand Down
6 changes: 5 additions & 1 deletion ios/src/json/JsonArray.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class JsonArray: JsonElement, IJsonArray {
public class JsonArray: JsonElement, IJsonArray, Sequence {
internal var arr: Array<JsonValueRaw?>

internal init(_ arr: Array<JsonValueRaw?>) {
Expand Down Expand Up @@ -78,4 +78,8 @@ public class JsonArray: JsonElement, IJsonArray {
public func toRaw() -> JsonArrayRaw {
return arr
}

public func makeIterator() -> JsonArrayIterator {
return JsonArrayIterator(self)
}
}
16 changes: 16 additions & 0 deletions ios/src/json/utils/JsonArrayIterator.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spryrocks/capacitor-ionic-core-plugin",
"version": "5.6.0-alpha.0",
"version": "5.7.0-alpha.0",
"description": "Ionic plugin core capacitor",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down

0 comments on commit 8035e1a

Please sign in to comment.