diff --git a/example/src/pages/DataBindingListExample.tsx b/example/src/pages/DataBindingListExample.tsx index 4dc2b599..8fd1c955 100644 --- a/example/src/pages/DataBindingListExample.tsx +++ b/example/src/pages/DataBindingListExample.tsx @@ -5,7 +5,7 @@ import { ActivityIndicator, TouchableOpacity, } from 'react-native'; -import { useMemo, useState, useCallback, useRef } from 'react'; +import { useMemo, useState, useRef } from 'react'; import { Fit, RiveView, @@ -13,6 +13,7 @@ import { type RiveFile, type RiveViewRef, useRiveFile, + useRiveList, } from '@rive-app/react-native'; import { type Metadata } from '../helpers/metadata'; @@ -65,18 +66,10 @@ function ListExample({ }) { const riveRef = useRef(null); const [isPlaying, setIsPlaying] = useState(true); - const listProperty = useMemo( - () => instance.listProperty('ListItemVM'), - [instance] - ); - const [listLength, setListLength] = useState(listProperty?.length ?? 0); - - const refreshLength = useCallback(() => { - setListLength(listProperty?.length ?? 0); - }, [listProperty]); + const { length, addInstance, removeInstanceAt, swap, getInstanceAt, error } = + useRiveList('ListItemVM', instance); - const handleAddItem = useCallback(() => { - if (!listProperty) return; + const handleAddItem = () => { const buttonVM = file.viewModelByName('button VM'); if (!buttonVM) { console.error('button VM view model not found'); @@ -91,54 +84,47 @@ function ListExample({ if (stringProp) { stringProp.value = 'new btn'; } - listProperty.addInstance(newInstance); + addInstance(newInstance); riveRef.current?.playIfNeeded(); - refreshLength(); - }, [listProperty, file, refreshLength]); + }; - const handleRemoveFirst = useCallback(() => { - if (!listProperty || listProperty.length === 0) return; - listProperty.removeInstanceAt(0); + const handleRemoveFirst = () => { + if (length === 0) return; + removeInstanceAt(0); riveRef.current?.playIfNeeded(); - refreshLength(); - }, [listProperty, refreshLength]); + }; - const handleRemoveLast = useCallback(() => { - if (!listProperty || listProperty.length === 0) return; - listProperty.removeInstanceAt(listProperty.length - 1); + const handleRemoveLast = () => { + if (length === 0) return; + removeInstanceAt(length - 1); riveRef.current?.playIfNeeded(); - refreshLength(); - }, [listProperty, refreshLength]); + }; - const handleSwapFirstTwo = useCallback(() => { - if (!listProperty || listProperty.length < 2) return; - listProperty.swap(0, 1); + const handleSwapFirstTwo = () => { + if (length < 2) return; + swap(0, 1); riveRef.current?.playIfNeeded(); - refreshLength(); - }, [listProperty, refreshLength]); - - const logListItems = useCallback(() => { - if (!listProperty) return; - console.log(`List has ${listProperty.length} items:`); - for (let i = 0; i < listProperty.length; i++) { - const item = listProperty.getInstanceAt(i); + }; + + const logListItems = () => { + console.log(`List has ${length} items:`); + for (let i = 0; i < length; i++) { + const item = getInstanceAt(i); console.log(` [${i}]: ${item?.instanceName ?? 'undefined'}`); } - }, [listProperty]); + }; - const handlePlayPause = useCallback(() => { + const handlePlayPause = () => { if (isPlaying) { riveRef.current?.pause(); } else { riveRef.current?.play(); } setIsPlaying(!isPlaying); - }, [isPlaying]); + }; - if (!listProperty) { - return ( - ListItemVM list property not found - ); + if (error) { + return {error.message}; } return ( @@ -156,7 +142,7 @@ function ListExample({ file={file} /> - List length: {listLength} + List length: {length} Add Item diff --git a/example/src/pages/ResponsiveLayouts.tsx b/example/src/pages/ResponsiveLayouts.tsx index e1372e7d..0cdcde83 100644 --- a/example/src/pages/ResponsiveLayouts.tsx +++ b/example/src/pages/ResponsiveLayouts.tsx @@ -1,5 +1,18 @@ -import { View, Text, StyleSheet, ActivityIndicator } from 'react-native'; -import { Fit, RiveView, useRiveFile } from '@rive-app/react-native'; +import { useState, useRef, useEffect } from 'react'; +import { + View, + Text, + StyleSheet, + Button, + ActivityIndicator, + useWindowDimensions, +} from 'react-native'; +import { + Fit, + RiveView, + useRiveFile, + type RiveViewRef, +} from '@rive-app/react-native'; import { type Metadata } from '../helpers/metadata'; /** @@ -11,6 +24,22 @@ export default function ResponsiveLayoutsExample() { const { riveFile, isLoading, error } = useRiveFile( require('../../assets/rive/layouts_demo.riv') ); + const [scaleFactor, setScaleFactor] = useState(4.0); + const riveRef = useRef(null); + const { width, height } = useWindowDimensions(); + + useEffect(() => { + riveRef.current?.playIfNeeded(); + }, [width, height]); + + const increaseScale = () => { + setScaleFactor((prev) => prev + 0.5); + riveRef.current?.playIfNeeded(); + }; + const decreaseScale = () => { + setScaleFactor((prev) => Math.max(0.5, prev - 0.5)); + riveRef.current?.playIfNeeded(); + }; return ( @@ -20,36 +49,64 @@ export default function ResponsiveLayoutsExample() { {error} ) : riveFile ? ( (riveRef.current = ref) }} file={riveFile} fit={Fit.Layout} - layoutScaleFactor={1} // adjust the layout scale factor to control the layout scale + layoutScaleFactor={scaleFactor} style={styles.rive} + autoPlay={true} /> ) : null} + + Layout Scale Factor + +