Skip to content

Commit

Permalink
feat: bump android runtime and integrate events listener
Browse files Browse the repository at this point in the history
  • Loading branch information
zplata committed Sep 20, 2023
1 parent 5969151 commit 4ff3033
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1'
implementation 'app.rive:rive-android:8.2.1'
implementation 'app.rive:rive-android:8.4.0'
implementation "androidx.startup:startup-runtime:1.1.1"
implementation 'com.android.volley:volley:1.2.0'
}
56 changes: 53 additions & 3 deletions android/src/main/java/com/rivereactnative/RiveReactNativeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.widget.FrameLayout
import app.rive.runtime.kotlin.PointerEvents
import app.rive.runtime.kotlin.RiveAnimationView
import app.rive.runtime.kotlin.controllers.RiveFileController
import app.rive.runtime.kotlin.RiveArtboardRenderer
import app.rive.runtime.kotlin.core.*
import app.rive.runtime.kotlin.core.errors.*
import com.android.volley.NetworkResponse
Expand All @@ -13,14 +12,14 @@ import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.HttpHeaderParser
import com.android.volley.toolbox.Volley
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.modules.core.ExceptionsManagerModule
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.RCTEventEmitter
import java.io.UnsupportedEncodingException
import kotlin.IllegalStateException


class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout(context) {
Expand All @@ -43,6 +42,7 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
STOP("onStop"),
LOOP_END("onLoopEnd"),
STATE_CHANGED("onStateChanged"),
RIVE_EVENT("onRiveEventReceived"),
ERROR("onError");

override fun toString(): String {
Expand Down Expand Up @@ -92,7 +92,18 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
}

}

val eventListener = object : RiveFileController.RiveEventListener {
override fun notifyEvent(event: RiveEvent) {
when (event) {
is RiveGeneralEvent -> onRiveEventReceived(event as RiveGeneralEvent)
is RiveOpenURLEvent -> onRiveEventReceived(event as RiveOpenURLEvent)
}
}
}

riveAnimationView.registerListener(listener)
riveAnimationView.addEventListener(eventListener)
autoplay = false
addView(riveAnimationView)
}
Expand Down Expand Up @@ -146,6 +157,45 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, Events.STATE_CHANGED.toString(), data)
}

private fun convertHashMapToWritableMap(hashMap: HashMap<String, Any>): WritableMap {
val writableMap = Arguments.createMap()

for ((key, value) in hashMap) {
when (value) {
is String -> writableMap.putString(key, value)
is Int -> writableMap.putInt(key, value)
is Float -> writableMap.putDouble(key, value.toDouble())
is Double -> writableMap.putDouble(key, value)
is Boolean -> writableMap.putBoolean(key, value)
}
}

return writableMap
}

fun onRiveEventReceived(event: RiveEvent) {
val reactContext = context as ReactContext
val topLevelDict = Arguments.createMap()

val eventProperties = Arguments.createMap().apply {
putString("name", event.name)
putDouble("delay", event.delay.toDouble())
putMap("properties", convertHashMapToWritableMap(event.properties))
}

if (event is RiveOpenURLEvent) {
eventProperties.putString("url", event.url)
eventProperties.putString("target", event.target)
}

topLevelDict.putMap(
"riveEvent",
eventProperties
)

reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, Events.RIVE_EVENT.toString(), topLevelDict)
}

fun play(animationName: String, rnLoopMode: RNLoopMode, rnDirection: RNDirection, isStateMachine: Boolean) {
val loop = RNLoopMode.mapToRiveLoop(rnLoopMode)
val direction = RNDirection.mapToRiveDirection(rnDirection)
Expand Down
Binary file added example/android/app/src/main/res/raw/rating.riv
Binary file not shown.

0 comments on commit 4ff3033

Please sign in to comment.