-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRiveFileLoadingExample.tsx
More file actions
194 lines (177 loc) · 4.47 KB
/
RiveFileLoadingExample.tsx
File metadata and controls
194 lines (177 loc) · 4.47 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
import {
Text,
View,
StyleSheet,
ActivityIndicator,
TouchableOpacity,
} from 'react-native';
import {
Fit,
RiveView,
useRive,
useRiveFile,
type RiveFileInput,
} from '@rive-app/react-native';
import { useState, useEffect } from 'react';
import { downloadFileAsArrayBuffer } from '../shared/fileHelpers';
import { type Metadata } from '../shared/metadata';
const LOADING_METHODS = {
SOURCE: 'Source',
URL: 'URL',
RESOURCE: 'Resource',
ARRAY_BUFFER: 'ArrayBuffer',
} as const;
type LoadingMethod = (typeof LOADING_METHODS)[keyof typeof LOADING_METHODS];
interface CustomRiveViewProps {
loadingMethod: LoadingMethod;
title: string;
}
const networkGraphicURL = 'https://cdn.rive.app/animations/vehicles.riv';
const getInputForMethod = async (
method: LoadingMethod
): Promise<RiveFileInput | undefined> => {
switch (method) {
case LOADING_METHODS.URL:
return networkGraphicURL;
case LOADING_METHODS.RESOURCE:
return 'rewards';
case LOADING_METHODS.ARRAY_BUFFER:
const arrayBuffer = await downloadFileAsArrayBuffer(networkGraphicURL);
return arrayBuffer || undefined;
case LOADING_METHODS.SOURCE:
return require('../../assets/rive/rating.riv');
default:
return undefined;
}
};
const CustomRiveView = ({ loadingMethod, title }: CustomRiveViewProps) => {
const { setHybridRef } = useRive();
const [input, setInput] = useState<RiveFileInput | undefined>(undefined);
useEffect(() => {
const loadInput = async () => {
setInput(undefined);
try {
const inputForMethod = await getInputForMethod(loadingMethod);
if (inputForMethod) setInput(inputForMethod);
} catch (err) {
console.error('Error loading input:', err);
}
};
loadInput();
}, [loadingMethod]);
const { riveFile, isLoading, error } = useRiveFile(input);
return (
<View style={styles.container}>
<Text style={styles.title}>{title}</Text>
{!input || isLoading ? (
<ActivityIndicator style={styles.rive} size="large" color="#0000ff" />
) : error ? (
<Text style={styles.errorText}>{error.message}</Text>
) : riveFile ? (
<RiveView
style={styles.rive}
autoPlay={true}
fit={Fit.Contain}
file={riveFile}
hybridRef={setHybridRef}
/>
) : null}
</View>
);
};
const TabButton = ({
method,
activeTab,
onPress,
}: {
method: LoadingMethod;
activeTab: LoadingMethod;
onPress: () => void;
}) => (
<TouchableOpacity
style={[styles.tab, activeTab === method && styles.activeTab]}
onPress={onPress}
>
<Text
style={[styles.tabText, activeTab === method && styles.activeTabText]}
>
{method}
</Text>
</TouchableOpacity>
);
export default function RiveFileLoadingExample() {
const [activeTab, setActiveTab] = useState<LoadingMethod>(
LOADING_METHODS.SOURCE
);
const titles = {
[LOADING_METHODS.SOURCE]: 'Loading from Source',
[LOADING_METHODS.URL]: 'Loading from URL',
[LOADING_METHODS.RESOURCE]: 'Loading from Resource',
[LOADING_METHODS.ARRAY_BUFFER]: 'Loading from ArrayBuffer',
};
return (
<View style={styles.container}>
<CustomRiveView loadingMethod={activeTab} title={titles[activeTab]} />
<View style={styles.tabBar}>
{Object.values(LOADING_METHODS).map((method) => (
<TabButton
key={method}
method={method}
activeTab={activeTab}
onPress={() => setActiveTab(method)}
/>
))}
</View>
</View>
);
}
RiveFileLoadingExample.metadata = {
name: 'File Loading',
description:
'Demonstrates different methods to load Rive files: from source, URL, resource, and ArrayBuffer',
} satisfies Metadata;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
title: {
fontSize: 20,
fontWeight: 'bold',
textAlign: 'center',
marginTop: 20,
marginBottom: 10,
},
rive: {
flex: 1,
},
tabBar: {
flexDirection: 'row',
height: 60,
borderTopWidth: 1,
borderTopColor: '#ccc',
backgroundColor: '#fff',
},
tab: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
activeTab: {
borderTopWidth: 2,
borderTopColor: '#007AFF',
},
tabText: {
fontSize: 14,
color: '#666',
},
activeTabText: {
color: '#007AFF',
fontWeight: 'bold',
},
errorText: {
color: 'red',
textAlign: 'center',
padding: 20,
},
});