-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from gfanton/feat/add-android-libs
feat: add android build
- Loading branch information
Showing
13 changed files
with
222 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,4 @@ yarn-error.log | |
|
||
# gomobile | ||
ios/Frameworks | ||
android/libs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
android/src/main/java/expo/modules/weshnetexpo/PromiseBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package expo.modules.weshnetexpo | ||
|
||
// | ||
// expo.modules.weshnetexpo.PromiseBlock.kt | ||
// WeshnetExpo | ||
// | ||
// Created by Guilhem Fanton on 10/07/2023. | ||
// | ||
|
||
import expo.modules.kotlin.Promise | ||
import network.weshnet.core.PromiseBlock as IPromiseBlock | ||
|
||
class PromiseBlock(val promise: Promise): IPromiseBlock { | ||
// expo.modules.weshnetexpo.PromiseBlock aims to keep reference over promise object so go can play with | ||
// until the promise is resolved | ||
companion object { | ||
private var promises = mutableSetOf<PromiseBlock>() | ||
} | ||
|
||
init { | ||
store() | ||
} | ||
|
||
override fun callResolve(reply: String?) { | ||
this.promise.resolve(reply ?: "") | ||
this.remove() // cleanup the promise | ||
} | ||
|
||
override fun callReject(err: Exception?) { | ||
this.promise.reject(WeshnetCoreError(err)) | ||
this.remove() // cleanup the promise | ||
} | ||
|
||
private fun store() { | ||
promises.add(this) | ||
} | ||
|
||
private fun remove() { | ||
promises.remove(this) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
android/src/main/java/expo/modules/weshnetexpo/WeshnetError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package expo.modules.weshnetexpo | ||
|
||
import expo.modules.kotlin.exception.CodedException | ||
|
||
class WeshnetNotStartedError : CodedException("NotStarted", "Service hasn't started yet", null) | ||
class WeshnetCoreError(err: Exception?) : CodedException("CoreError", err) | ||
|
77 changes: 43 additions & 34 deletions
77
android/src/main/java/expo/modules/weshnetexpo/WeshnetExpoModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,56 @@ | ||
package expo.modules.weshnetexpo | ||
|
||
import expo.modules.kotlin.Promise | ||
import expo.modules.kotlin.exception.CodedException | ||
import expo.modules.kotlin.modules.Module | ||
import expo.modules.kotlin.modules.ModuleDefinition | ||
import network.weshnet.core.Service | ||
import network.weshnet.core.Core | ||
|
||
class WeshnetExpoModule : Module() { | ||
// Each module class must implement the definition function. The definition consists of components | ||
// that describes the module's functionality and behavior. | ||
// See https://docs.expo.dev/modules/module-api for more details about available components. | ||
override fun definition() = ModuleDefinition { | ||
// Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument. | ||
// Can be inferred from module's class name, but it's recommended to set it explicitly for clarity. | ||
// The module will be accessible from `requireNativeModule('WeshnetExpo')` in JavaScript. | ||
Name("WeshnetExpo") | ||
private var service: Service? = null | ||
|
||
// Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary. | ||
Constants( | ||
"PI" to Math.PI | ||
) | ||
// Each module class must implement the definition function. The definition consists of components | ||
// that describes the module's functionality and behavior. | ||
// See https://docs.expo.dev/modules/module-api for more details about available components. | ||
override fun definition() = ModuleDefinition { | ||
Name("WeshnetExpo") | ||
|
||
// Defines event names that the module can send to JavaScript. | ||
Events("onChange") | ||
AsyncFunction("init") { promise: Promise -> | ||
try { | ||
if (service == null) { | ||
service = initializeCoreService() | ||
} | ||
promise.resolve("") | ||
} catch (err: CodedException) { | ||
promise.reject(err) | ||
} | ||
} | ||
|
||
// Defines a JavaScript synchronous function that runs the native code on the JavaScript thread. | ||
Function("hello") { | ||
"Hello world! 👋" | ||
// Defines a JavaScript function that always returns a Promise and whose native code | ||
// is by default dispatched on a different thread than the JavaScript runtime runs on. | ||
AsyncFunction("invokeMethod") { method: String, b64message: String, promise: Promise -> | ||
try { | ||
service?.let { | ||
val block = PromiseBlock(promise) | ||
it.invokeBridgeMethodWithPromiseBlock(block, method, b64message) | ||
} ?: run { | ||
throw WeshnetNotStartedError() | ||
} | ||
} catch (err: CodedException) { | ||
promise.reject(err) | ||
} | ||
} | ||
} | ||
|
||
// Defines a JavaScript function that always returns a Promise and whose native code | ||
// is by default dispatched on the different thread than the JavaScript runtime runs on. | ||
AsyncFunction("setValueAsync") { value: String -> | ||
// Send an event to JavaScript. | ||
sendEvent("onChange", mapOf( | ||
"value" to value | ||
)) | ||
@Throws(CodedException::class) | ||
private fun initializeCoreService(): Service { | ||
// Add logic to create and return your service instance | ||
// If an error occurs, throw WeshnetError2.kt | ||
try { | ||
return Core.newService() | ||
} catch (err: Exception) { | ||
throw WeshnetCoreError(err) | ||
} | ||
} | ||
|
||
// Enables the module to be used as a native view. Definition components that are accepted as part of | ||
// the view definition: Prop, Events. | ||
View(WeshnetExpoView::class) { | ||
// Defines a setter for the `name` prop. | ||
Prop("name") { view: WeshnetExpoView, prop: String -> | ||
println(prop) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.