-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRiveSuspenseExample.tsx
More file actions
184 lines (173 loc) · 4 KB
/
RiveSuspenseExample.tsx
File metadata and controls
184 lines (173 loc) · 4 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
import {
Text,
View,
StyleSheet,
ActivityIndicator,
ScrollView,
} from 'react-native';
import { Fit, RiveView, RiveSuspense } from 'react-native-rive';
import { Suspense, Component, type ReactNode } from 'react';
class ErrorBoundary extends Component<
{ children: ReactNode; fallback: ReactNode },
{ hasError: boolean; error?: Error }
> {
constructor(props: { children: ReactNode; fallback: ReactNode }) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: Error) {
return { hasError: true, error };
}
render() {
if (this.state.hasError) {
return this.props.fallback;
}
return this.props.children;
}
}
const RiveCard = ({ title, input }: { title: string; input: any }) => {
const riveFile = RiveSuspense.useRiveFile(input);
return (
<View style={styles.card}>
<Text style={styles.cardTitle}>{title}</Text>
<RiveView
style={styles.riveView}
autoBind={false}
autoPlay={true}
fit={Fit.Contain}
file={riveFile}
/>
</View>
);
};
const SuspenseContent = () => {
return (
<ScrollView style={styles.scrollView}>
<Text style={styles.description}>
This example demonstrates the Suspense API. All three cards below use
the same Rive file, which is cached and shared efficiently.
</Text>
<RiveCard
title="Card 1"
input={require('../../assets/rive/rating.riv')}
/>
<RiveCard
title="Card 2"
input={require('../../assets/rive/rating.riv')}
/>
<RiveCard
title="Card 3"
input={require('../../assets/rive/rating.riv')}
/>
<Text style={styles.note}>
Note: All three cards share the same cached RiveFile instance, loaded
only once!
</Text>
</ScrollView>
);
};
export default function RiveSuspenseExample() {
return (
<View style={styles.container}>
<Text style={styles.title}>RiveSuspense Example</Text>
<RiveSuspense.Provider>
<ErrorBoundary
fallback={
<View style={styles.errorContainer}>
<Text style={styles.errorText}>
Failed to load Rive animation
</Text>
</View>
}
>
<Suspense
fallback={
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" color="#007AFF" />
<Text style={styles.loadingText}>Loading animations...</Text>
</View>
}
>
<SuspenseContent />
</Suspense>
</ErrorBoundary>
</RiveSuspense.Provider>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
title: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginTop: 20,
marginBottom: 10,
color: '#333',
},
description: {
fontSize: 14,
textAlign: 'center',
marginHorizontal: 20,
marginBottom: 20,
color: '#666',
lineHeight: 20,
},
scrollView: {
flex: 1,
},
card: {
margin: 20,
padding: 15,
backgroundColor: '#f5f5f5',
borderRadius: 10,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
cardTitle: {
fontSize: 18,
fontWeight: '600',
marginBottom: 10,
color: '#333',
},
riveView: {
height: 200,
backgroundColor: '#fff',
borderRadius: 8,
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
loadingText: {
marginTop: 10,
fontSize: 16,
color: '#007AFF',
},
errorContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
errorText: {
color: 'red',
fontSize: 16,
textAlign: 'center',
padding: 20,
},
note: {
fontSize: 12,
fontStyle: 'italic',
textAlign: 'center',
marginHorizontal: 20,
marginBottom: 40,
color: '#007AFF',
},
});