Skip to content

Commit

Permalink
Merge pull request #101 from SpryRocks/merge-cap2
Browse files Browse the repository at this point in the history
Merge cap2
  • Loading branch information
Maxim Zhemerenko authored Dec 8, 2023
2 parents 992ae3d + ff2c603 commit d801b65
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 214 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/gradle-publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
36 changes: 14 additions & 22 deletions ios/src/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,57 @@ open class CorePlugin<TDelegate : CoreDelegate, TMappers: CoreMappers>: 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<TDelegate, TMappers>) {
(event as! Context).initialize(callback: self, delegate: delegate, mappers: mappers)
if let event = event as? Context<TDelegate, TMappers> {
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<TDelegate, TMappers>(action: action, tag: tag, level: level, message: message, params: params))
}

public func call(_ actionType: CoreBaseAction<TDelegate, TMappers>.Type, _ call: CAPPluginCall) {
let context = CallContext(call: call, mappers: mappers)
do {
let action = try actionType.init(args: context.asObject())
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)
}
Expand Down
37 changes: 20 additions & 17 deletions ios/src/actions/BaseAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ open class CoreBaseAction<TDelegate, TMappers>: ContextWithCall<TDelegate, TMapp
required public init(args: IJsonObjectProperties) throws {
super.init()
}

open func onExecute() throws {}

public func executeAsync(operation: @escaping () throws -> Void) {
DispatchQueue.main.async {
do {
Expand All @@ -17,7 +17,7 @@ open class CoreBaseAction<TDelegate, TMappers>: ContextWithCall<TDelegate, TMapp
}
}
}

public func executeTask(operation: @Sendable @escaping () async throws -> Void) {
Task.init {
do {
Expand All @@ -27,35 +27,38 @@ open class CoreBaseAction<TDelegate, TMappers>: ContextWithCall<TDelegate, TMapp
}
}
}
public func success(_ data: PluginCallResultData? = nil, finish: Bool = true) {
callback.reportSuccess(data, call: call, finish: finish)

public func success(_ data: JsonObject? = nil, finish: Bool = true) {
mappers.reportSuccess(data, call: call, finish: finish)
}

public func success(_ data: Encodable, finish: Bool = true, serialize: Bool) {
let encoded = try! JSONEncoder().encode(data)
let dict = try! JSONSerialization.jsonObject(with: encoded) as! [String: Any]
success(dict, finish: finish)
do {
let data = try JsonObject.fromObject(data)
success(data, finish: finish)
} catch {
self.error(error)
}
}

public func error(_ error: Error? = nil, finish: Bool = true) {
callback.reportError(error, call: call, finish: finish)
mappers.reportError(error, call: call, finish: finish)
}

public func logger(tag: String?) -> 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)
}
Expand Down
70 changes: 35 additions & 35 deletions ios/src/actions/CallContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading

0 comments on commit d801b65

Please sign in to comment.