-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNestedViewModelExample.tsx
More file actions
241 lines (220 loc) · 5.61 KB
/
NestedViewModelExample.tsx
File metadata and controls
241 lines (220 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
View,
Text,
StyleSheet,
ActivityIndicator,
Button,
TextInput,
} from 'react-native';
import { useRef, useState } from 'react';
import {
Fit,
RiveView,
useRiveFile,
useRiveString,
useViewModelInstance,
type ViewModelInstance,
type RiveFile,
type RiveViewRef,
} from '@rive-app/react-native';
import { type Metadata } from '../shared/metadata';
export default function NestedViewModelExample() {
const { riveFile, isLoading, error } = useRiveFile(
require('../../assets/rive/viewmodelproperty.riv')
);
return (
<View style={styles.container}>
{isLoading ? (
<ActivityIndicator size="large" color="#0000ff" />
) : riveFile ? (
<WithViewModelSetup file={riveFile} />
) : (
<Text style={styles.errorText}>{error || 'Unexpected error'}</Text>
)}
</View>
);
}
function WithViewModelSetup({ file }: { file: RiveFile }) {
const instance = useViewModelInstance(file);
if (!instance) {
return <ActivityIndicator size="large" color="#0000ff" />;
}
return <ReplaceViewModelTest instance={instance} file={file} />;
}
function ReplaceViewModelTest({
instance,
file,
}: {
instance: ViewModelInstance;
file: RiveFile;
}) {
const riveRef = useRef<RiveViewRef>(null);
const [replaced, setReplaced] = useState(false);
const [log, setLog] = useState<string[]>([]);
const { value: vm1Name, setValue: setVm1Name } = useRiveString(
'vm1/name',
instance
);
const { value: vm2Name, setValue: setVm2Name } = useRiveString(
'vm2/name',
instance
);
const handleSetVm1Name = (newValue: string) => {
setVm1Name(newValue);
riveRef.current?.playIfNeeded();
};
const handleSetVm2Name = (newValue: string) => {
setVm2Name(newValue);
riveRef.current?.playIfNeeded();
};
const addLog = (msg: string) => setLog((prev) => [...prev, msg]);
const handleReplace = async () => {
const vm2Instance = await instance.viewModelAsync('vm2');
if (!vm2Instance) {
addLog('❌ viewModelAsync("vm2") returned undefined');
return;
}
addLog(`✅ Got vm2 instance: ${vm2Instance.instanceName}`);
// Replace vm1 with vm2's instance
try {
instance.replaceViewModel('vm1', vm2Instance);
addLog('✅ replaceViewModel("vm1", vm2Instance) succeeded');
// Call playIfNeeded to update the graphics
riveRef.current?.playIfNeeded();
addLog('✅ Called playIfNeeded() to refresh display');
addLog('→ Now vm1 and vm2 point to the same instance');
addLog('→ Changing vm2.name should also change vm1.name');
setReplaced(true);
} catch (error) {
addLog(`❌ replaceViewModel failed: ${error}`);
}
};
return (
<View style={styles.content}>
<RiveView
hybridRef={{ f: (ref: RiveViewRef | null) => (riveRef.current = ref) }}
style={styles.rive}
autoPlay={true}
dataBind={instance}
fit={Fit.Contain}
file={file}
/>
<View style={styles.info}>
<Text style={styles.title}>replaceViewModel() Test</Text>
<Text style={styles.description}>
Replace vm1 with vm2's instance. After replacement, changing vm2.name
should also change vm1.name since they share the same instance.
</Text>
<View style={styles.row}>
<Text style={styles.label}>vm1.name:</Text>
<TextInput
style={styles.input}
value={vm1Name ?? ''}
onChangeText={handleSetVm1Name}
placeholder="vm1 name"
/>
</View>
<View style={styles.row}>
<Text style={styles.label}>vm2.name:</Text>
<TextInput
style={styles.input}
value={vm2Name ?? ''}
onChangeText={handleSetVm2Name}
placeholder="vm2 name"
/>
</View>
{!replaced && (
<Button title="Replace vm1 with vm2" onPress={handleReplace} />
)}
{replaced && (
<Text style={styles.success}>
✅ Replaced! Try changing vm2.name - vm1.name should update too
</Text>
)}
{log.length > 0 && (
<View style={styles.logContainer}>
<Text style={styles.logTitle}>Log:</Text>
{log.map((entry, i) => (
<Text key={i} style={styles.logEntry}>
{entry}
</Text>
))}
</View>
)}
</View>
</View>
);
}
NestedViewModelExample.metadata = {
name: 'Nested ViewModel',
description:
'Tests viewModel() and replaceViewModel() for nested ViewModel instances',
} satisfies Metadata;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
content: {
flex: 1,
},
rive: {
flex: 1,
width: '100%',
},
info: {
padding: 16,
backgroundColor: '#fff',
gap: 12,
},
title: {
fontSize: 18,
fontWeight: 'bold',
},
description: {
fontSize: 14,
color: '#666',
},
row: {
flexDirection: 'row',
alignItems: 'center',
gap: 8,
},
label: {
fontSize: 14,
fontWeight: '600',
width: 80,
},
input: {
flex: 1,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
padding: 8,
fontSize: 14,
},
success: {
color: 'green',
fontWeight: '600',
},
logContainer: {
marginTop: 8,
padding: 8,
backgroundColor: '#f5f5f5',
borderRadius: 8,
},
logTitle: {
fontWeight: '600',
marginBottom: 4,
},
logEntry: {
fontSize: 12,
fontFamily: 'monospace',
color: '#333',
},
errorText: {
color: 'red',
textAlign: 'center',
padding: 20,
},
});