Skip to content

Commit

Permalink
chore: ci fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elasoshi committed Sep 30, 2024
1 parent ef84cfb commit f9489df
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.dotlottiereactnative

import android.widget.FrameLayout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import com.dotlottie.dlplayer.Event
Expand Down Expand Up @@ -31,28 +32,6 @@ class DotlottieReactNativeView(context: ThemedReactContext) : FrameLayout(contex
private var playMode: Mode = Mode.FORWARD
var dotLottieController: DotLottieController = DotLottieController()

val stateListener =
object : StateMachineEventListener {
override fun onTransition(previousState: String, newState: String) {
val value =
Arguments.createMap().apply {
putString("previousState", previousState)
putString("newState", newState)
}
onReceiveNativeEvent("onTransition", value)
}

override fun onStateExit(leavingState: String) {
val value = Arguments.createMap().apply { putString("leavingState", leavingState) }
onReceiveNativeEvent("onStateExit", value)
}

override fun onStateEntered(enteringState: String) {
val value = Arguments.createMap().apply { putString("enteringState", enteringState) }
onReceiveNativeEvent("onStateEntered", value)
}
}

private val composeView: ComposeView =
ComposeView(context).apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
Expand All @@ -73,6 +52,33 @@ class DotlottieReactNativeView(context: ThemedReactContext) : FrameLayout(contex
fun DotLottieContent() {
dotLottieController = remember { DotLottieController() }

val stateListener = remember {
object : StateMachineEventListener {
override fun onTransition(previousState: String, newState: String) {
val value =
Arguments.createMap().apply {
putString("previousState", previousState)
putString("newState", newState)
}

onReceiveNativeEvent("onTransition", value)
}

override fun onStateExit(leavingState: String) {
val value = Arguments.createMap().apply { putString("leavingState", leavingState) }
onReceiveNativeEvent("onStateExit", value)
}

override fun onStateEntered(enteringState: String) {
val value = Arguments.createMap().apply { putString("enteringState", enteringState) }
println("enteringState")
onReceiveNativeEvent("onStateEntered", value)
}
}
}

LaunchedEffect(UInt) { dotLottieController.addStateMachineEventListener(stateListener) }

animationUrl?.let { url ->
DotLottieAnimation(
source = DotLottieSource.Url(url),
Expand Down Expand Up @@ -163,14 +169,6 @@ class DotlottieReactNativeView(context: ThemedReactContext) : FrameLayout(contex
composeView.setContent { DotLottieContent() }
}

fun addStateMachineEventListener() {
dotLottieController.addStateMachineEventListener(stateListener)
}

fun removeStateMachineEventListener() {
dotLottieController.removeStateMachineEventListener(stateListener)
}

fun postEvent(event: String) {
dotLottieController.postEvent(Event.String(event))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ class DotlottieReactNativeViewManager : SimpleViewManager<DotlottieReactNativeVi
}

if (commandId == COMMAND_START_STATE_MACHINE_ID) {
view.dotLottieController.startStateMachine()
val stateMachineId = args?.getString(0) ?: ""
val result = view.dotLottieController.loadStateMachine(stateMachineId)
if (result) {
view.dotLottieController.startStateMachine()
}
}

if (commandId == COMMAND_STOP_STATE_MACHINE_ID) {
Expand All @@ -116,15 +120,6 @@ class DotlottieReactNativeViewManager : SimpleViewManager<DotlottieReactNativeVi
val event = args?.getString(0) ?: ""
view.postEvent(event)
}

if (commandId == COMMAND_ADD_STATE_MACHINE_EVENT_LISTENER_ID) {
view.addStateMachineEventListener()
}

if (commandId == COMMAND_REMOVE_STATE_MACHINE_EVENT_LISTENER_ID) {
view.removeStateMachineEventListener()
}

if (commandId == COMMAND_RESIZE_ID) {
val width = args?.getDouble(0)?.toUInt() ?: 0u
val height = args?.getDouble(1)?.toUInt() ?: 0u
Expand Down
1 change: 1 addition & 0 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
classpath("com.android.tools.build:gradle")
Expand Down
Binary file added example/assets/exploding_pigeon.lottie
Binary file not shown.
8 changes: 4 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function App() {
}}
style={styles.box}
loop={false}
autoplay={false}
autoplay={true}
onPlay={(a) => console.log('onPlay', a.nativeEvent)}
onLoop={(a) => console.log('onLoop', a)}
onPause={() => console.log('onPause')}
Expand All @@ -37,12 +37,12 @@ export default function App() {
/>
<Button
title="START_STATE_MACHINE"
onPress={() => ref.current?.startStateMachine()}
onPress={() => ref.current?.startStateMachine('pigeon_fsm')}
/>

<Button
title="ADD_STATE_MACHINE"
onPress={() => ref.current?.addStateMachineEventListener()}
title="LOAD_STATE_MACHINE"
onPress={() => ref.current?.loadStateMachine('pigeon_fsm')}
/>
<Button
title="REMOVE_STATE_MACHINE"
Expand Down
27 changes: 15 additions & 12 deletions src/DotLottie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type Dotlottie = {
setFrame: (frame: number) => void;
freeze: () => void;
unfreeze: () => void;
startStateMachine: () => void;
startStateMachine: (stateMachineId: string) => void;
stopStateMachine: () => void;
loadStateMachine: (stateMachineId: string) => void;
setPostEvent: (event: string) => void;
Expand Down Expand Up @@ -225,17 +225,20 @@ export const DotLottie = forwardRef(
[]
);

const startStateMachineWithUIManager = useCallback(() => {
const command =
NativeViewManager.Commands[COMMAND_SET_START_STATE_MACHINE];
if (command) {
return UIManager.dispatchViewManagerCommand(
findNodeHandle(nativeRef.current),
command,
[]
);
}
}, []);
const startStateMachineWithUIManager = useCallback(
(stateMachineId: string) => {
const command =
NativeViewManager.Commands[COMMAND_SET_START_STATE_MACHINE];
if (command) {
return UIManager.dispatchViewManagerCommand(
findNodeHandle(nativeRef.current),
command,
[stateMachineId]
);
}
},
[]
);

const stopStateMachineWithUIManager = useCallback(() => {
const command =
Expand Down

0 comments on commit f9489df

Please sign in to comment.