Skip to content

Commit

Permalink
Merge pull request #105 from SpryRocks/v8/main-capacitor2
Browse files Browse the repository at this point in the history
V8/main capacitor2
  • Loading branch information
maximzhemerenko authored May 20, 2024
2 parents 992ae3d + 556f8b5 commit 804bb65
Show file tree
Hide file tree
Showing 23 changed files with 496 additions and 260 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/gradle-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish package
on:
push
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '21.x'
registry-url: 'https://registry.npmjs.org'
- run: yarn && yarn build
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
21
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package com.ionic.plugin.android.capacitor.core
import android.app.Activity
import android.content.Intent
import com.getcapacitor.PluginCall
import com.getcapacitor.PluginMethod
import com.ionic.plugin.android.capacitor.core.actions.CallContext
import com.ionic.plugin.android.core.utils.ActivityResultObserver
import com.ionic.plugin.android.core.utils.IActivityResultObserver
import com.ionic.plugin.core.actions.BaseAction
import com.ionic.plugin.core.actions.Delegate
import com.ionic.plugin.core.actions.IActionCreator
import com.ionic.plugin.core.actions.Mappers
import com.ionic.plugin.core.actions.SetLogLevelsAction
import com.spryrocks.kson.JsonObject
import com.spryrocks.kson.mutableJsonObject

abstract class CapacitorPlugin<TActionKey, TDelegate : Delegate<TMappers>, TMappers: Mappers> : com.getcapacitor.Plugin() {
abstract class CapacitorPlugin<TActionKey, TDelegate : Delegate<TMappers>, TMappers : Mappers> :
com.getcapacitor.Plugin() {
private val plugin: com.ionic.plugin.core.Plugin<TActionKey, TDelegate, TMappers>
private val wrapperDelegate = WrapperDelegateImpl(this)
private var activityResultObserver_ = ActivityResultObserver()
Expand All @@ -29,14 +33,34 @@ abstract class CapacitorPlugin<TActionKey, TDelegate : Delegate<TMappers>, TMapp
}

protected fun call(action: TActionKey, call: PluginCall) {
plugin.call(action, CallContext(call, wrapperDelegate, plugin.mappers))
plugin.call(action, call.toContext())
}

protected fun callAction(
actionCreator: (call: CallContext) -> BaseAction<TDelegate, TMappers>,
call: PluginCall
) {
val context = call.toContext()
val baseAction = actionCreator(context)
plugin.callAction(baseAction, context)
}

private fun PluginCall.toContext(): CallContext {
return CallContext(this, wrapperDelegate, plugin.mappers)
}

override fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
activityResultObserver_.onActivityResult(requestCode, resultCode, data)
}

class WrapperDelegateImpl<TActionKey, TDelegate : Delegate<TMappers>, TMappers: Mappers>(private val wrapper: CapacitorPlugin<TActionKey, TDelegate, TMappers>) :
@PluginMethod
fun setLogLevels(call: PluginCall) {
callAction({ SetLogLevelsAction(it.asObject()) }, call)
}

class WrapperDelegateImpl<TActionKey, TDelegate : Delegate<TMappers>, TMappers : Mappers>(
private val wrapper: CapacitorPlugin<TActionKey, TDelegate, TMappers>
) :
WrapperDelegate {
override val activity: Activity
get() = wrapper.activity
Expand Down
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
Loading

0 comments on commit 804bb65

Please sign in to comment.