Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added reset #15

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions android/src/main/java/com/rivereactnative/RiveReactNativeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
resetRiveResource()
}

fun reset() {
url?.let {
if(resId == -1) {
riveAnimationView.drawable.reset()
}
} ?: run {
if(resId != -1) {
riveAnimationView.reset()
}
}
}

fun update() {
reloadIfNeeded()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
private enum class Commands {
PLAY,
PAUSE,
STOP
STOP,
RESET
}


Expand All @@ -31,7 +32,9 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
"pause",
Commands.PAUSE.ordinal,
"stop",
Commands.STOP.ordinal
Commands.STOP.ordinal,
"reset",
Commands.RESET.ordinal
)
}

Expand Down Expand Up @@ -61,6 +64,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
}
}
Commands.STOP.ordinal -> view.stop()
Commands.RESET.ordinal -> view.reset()
else -> {
}
}
Expand Down
14 changes: 10 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export default function App() {
setPlaying(false);
};

const resetAnimation = () => {
riveRef.current?.reset();
};

return (
<View style={styles.container}>
<Rive
Expand Down Expand Up @@ -70,12 +74,14 @@ export default function App() {
}}
style={styles.box}
fit={fit}
resourceName={
Platform.OS === 'android' ? 'artboard_animations' : 'bird'
}
// url={'https://cdn.rive.app/animations/juice_v7.riv'}
// resourceName={Platform.OS === 'android' ? 'flying_car' : 'bird'}
url={'https://cdn.rive.app/animations/juice_v7.riv'}
/>
<View style={styles.wrapper}>
<TouchableOpacity onPress={resetAnimation}>
<Text style={styles.button}>{'RESET'}</Text>
</TouchableOpacity>

<TouchableOpacity onPress={toggleAnimation}>
<Text style={styles.button}>{isPlaying ? 'PAUSE' : 'PLAY'}</Text>
</TouchableOpacity>
Expand Down
11 changes: 10 additions & 1 deletion src/Rive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,23 @@ const RiveContainer = React.forwardRef<RiveRef, Props>(
);
}, []);

const reset = useCallback(() => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(riveRef.current),
UIManager.getViewManagerConfig(VIEW_NAME).Commands.reset,
[]
);
}, []);

useImperativeHandle(
ref,
() => ({
play,
pause,
stop,
reset,
}),
[play, pause, stop]
[play, pause, stop, reset]
);

return (
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type RiveRef = {
areStateMachines?: boolean
) => void;
stop: () => void;
reset: () => void;
};

export enum Fit {
Expand Down