-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add dataBind prop to RiveView #23
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
491ae77
feat: add viewModelInstance to ViewProps, so we don't need to wait fo…
mfazekas 248c82f
feat: getBoundViewModelInstance
mfazekas 6d4dbfb
dataBind 1st gen
mfazekas b164165
fix: apply data binding changes in-place without reload
mfazekas 1571d02
fix(ios): bind view model instance to both artboard and state machine
mfazekas 23c1d84
fix(ios): play after binding to refresh view
mfazekas ff931d6
fix(ios): play after none and auto binding modes
mfazekas 8baafac
fix(android): bind to artboard and play after binding changes
mfazekas ad702d1
fix(android): trigger full reload on binding mode changes
mfazekas ca726eb
refactor(android): remove manual afterUpdate call from dataBind setter
mfazekas aae4a7e
fix(android): play state machine after binding mode reload
mfazekas 719ebbe
fix(andorid): use ios like implementation
mfazekas 726d3d4
fix(andorid): use ios like implementation
mfazekas c46c6aa
removed unused files
mfazekas 9c39621
refator: simplify changes
mfazekas c49e386
fix: make dataBind optional and default to auto
mfazekas 6ba304e
review fixes
mfazekas 21ae834
renamed miklos_viewmodel to many_viewmodels
mfazekas 233cd12
review fixes
mfazekas 78599a8
fix: add logged to android
mfazekas b3a94ee
fix: dataBindingChanged
mfazekas 4b467f1
Apply suggestion from @mfazekas
mfazekas 0cf12b1
fix: always apply data binding on initial update
mfazekas 582345d
fix(andorid): always apply data binding on initial update
mfazekas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Binary file not shown.
This file contains hidden or 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,148 @@ | ||
| import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'; | ||
| import { useState, useMemo } from 'react'; | ||
| import type { Metadata } from '../helpers/metadata'; | ||
| import { | ||
| DataBindMode, | ||
| RiveView, | ||
| useRiveFile, | ||
| type ViewModelInstance, | ||
| } from 'react-native-rive'; | ||
|
|
||
| type BindModeOption = | ||
| | 'none' | ||
| | 'auto' | ||
| | 'red' | ||
| | 'green' | ||
| | 'blue' | ||
| | 'green-instance'; | ||
|
|
||
| function getDataBindValue( | ||
| mode: BindModeOption, | ||
| greenInstance: ViewModelInstance | null | ||
| ) { | ||
| if (mode === 'none') return DataBindMode.None; | ||
| if (mode === 'auto') return DataBindMode.Auto; | ||
| if (mode === 'green-instance' && greenInstance) return greenInstance; | ||
| return { byName: mode }; | ||
| } | ||
|
|
||
| export default function DataBindingMode() { | ||
| const { riveFile } = useRiveFile( | ||
| require('../../assets/rive/miklos_viewmodels.riv') | ||
| ); | ||
| const [bindMode, setBindMode] = useState<BindModeOption>('none'); | ||
|
|
||
| // Create a ViewModelInstance for "green" to demonstrate instance binding | ||
| const greenInstance = useMemo(() => { | ||
| if (!riveFile) return null; | ||
| try { | ||
| const viewModel = riveFile.defaultArtboardViewModel(); | ||
| if (!viewModel) return null; | ||
| return viewModel.createInstanceByName('green'); | ||
| } catch (e) { | ||
| console.error('Failed to create green instance:', e); | ||
| return null; | ||
| } | ||
| }, [riveFile]); | ||
|
|
||
| const dataBindValue = getDataBindValue(bindMode, greenInstance); | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <View style={styles.controlsContainer}> | ||
| <Text style={styles.label}>Binding Mode:</Text> | ||
| <View style={styles.buttonRow}> | ||
| {( | ||
| [ | ||
| 'none', | ||
| 'auto', | ||
| 'red', | ||
| 'green', | ||
| 'blue', | ||
| 'green-instance', | ||
| ] as BindModeOption[] | ||
| ).map((mode) => ( | ||
| <TouchableOpacity | ||
| key={mode} | ||
| style={[styles.button, bindMode === mode && styles.buttonActive]} | ||
| onPress={() => setBindMode(mode)} | ||
| > | ||
| <Text | ||
| style={[ | ||
| styles.buttonText, | ||
| bindMode === mode && styles.buttonTextActive, | ||
| ]} | ||
| > | ||
| {mode === 'green-instance' ? 'green (instance)' : mode} | ||
| </Text> | ||
| </TouchableOpacity> | ||
| ))} | ||
| </View> | ||
| </View> | ||
| {riveFile && ( | ||
| <RiveView | ||
| style={styles.rive} | ||
| file={riveFile} | ||
| dataBind={dataBindValue} | ||
| autoPlay={true} | ||
| /> | ||
| )} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| DataBindingMode.metadata = { | ||
| name: 'Miklos View Models', | ||
| description: | ||
| 'Interactive data binding mode selector (none, auto, byName, and instance)', | ||
| } satisfies Metadata; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| backgroundColor: '#fff', | ||
| }, | ||
| controlsContainer: { | ||
| padding: 16, | ||
| backgroundColor: '#f8f8f8', | ||
| borderBottomWidth: 1, | ||
| borderBottomColor: '#e0e0e0', | ||
| }, | ||
| label: { | ||
| fontSize: 14, | ||
| fontWeight: '600', | ||
| marginBottom: 8, | ||
| color: '#333', | ||
| }, | ||
| buttonRow: { | ||
| flexDirection: 'row', | ||
| gap: 8, | ||
| flexWrap: 'wrap', | ||
| }, | ||
| button: { | ||
| paddingHorizontal: 16, | ||
| paddingVertical: 8, | ||
| backgroundColor: '#fff', | ||
| borderRadius: 8, | ||
| borderWidth: 1, | ||
| borderColor: '#d0d0d0', | ||
| }, | ||
| buttonActive: { | ||
| backgroundColor: '#007AFF', | ||
| borderColor: '#007AFF', | ||
| }, | ||
| buttonText: { | ||
| fontSize: 14, | ||
| fontWeight: '500', | ||
| color: '#333', | ||
| textTransform: 'capitalize', | ||
| }, | ||
| buttonTextActive: { | ||
| color: '#fff', | ||
| }, | ||
| rive: { | ||
| flex: 1, | ||
| width: '100%', | ||
| height: '100%', | ||
| }, | ||
| }); |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.