-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOutOfBandAssetsWithSuspense.tsx
More file actions
265 lines (242 loc) · 6.46 KB
/
OutOfBandAssetsWithSuspense.tsx
File metadata and controls
265 lines (242 loc) · 6.46 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import * as React from 'react';
import {
ActivityIndicator,
ScrollView,
StyleSheet,
Text,
View,
Button,
} from 'react-native';
import {
Fit,
useRiveFile,
RiveView,
RiveImages,
type RiveImage,
} from '@rive-app/react-native';
import { Picker } from '@react-native-picker/picker';
import { type Metadata } from '../shared/metadata';
class ErrorBoundary extends React.Component<
{
children: React.ReactNode;
fallback?: (error: Error, reset: () => void) => React.ReactNode;
},
{ hasError: boolean; error: Error | null }
> {
constructor(props: any) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error) {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
console.error('ErrorBoundary caught:', error, errorInfo);
}
reset = () => {
this.setState({ hasError: false, error: null });
};
render() {
if (this.state.hasError && this.state.error) {
if (this.props.fallback) {
return this.props.fallback(this.state.error, this.reset);
}
return (
<View style={styles.errorContainer}>
<Text style={styles.errorText}>Something went wrong:</Text>
<Text style={styles.errorMessage}>{this.state.error.message}</Text>
<Button title="Try Again" onPress={this.reset} />
</View>
);
}
return this.props.children;
}
}
const delay = 1000;
const ImageURL1 = `https://picsum.photos/id/372/500/500` as const;
const ImageURL2 = `https://picsum.photos/id/373/500/500` as const;
const ImageURLSlow =
`https://app.requestly.io/delay/${delay}/https://picsum.photos/id/374/500/500` as const;
const ImageInvalidURL = 'not-a-valid-url' as const;
type ImageURLS =
| typeof ImageURL1
| typeof ImageURL2
| typeof ImageURLSlow
| typeof ImageInvalidURL;
const imagePromises = new Map<string, Promise<RiveImage>>();
function getImagePromise(url: string): Promise<RiveImage> {
if (!imagePromises.has(url)) {
imagePromises.set(url, RiveImages.loadFromURLAsync(url));
}
return imagePromises.get(url)!;
}
function RiveContent({ imageUrl }: { imageUrl: ImageURLS }) {
const riveImage = React.use(getImagePromise(imageUrl));
const { riveFile, isLoading, error } = useRiveFile(
require('../../assets/rive/out_of_band.riv'),
{
referencedAssets: {
'Inter-594377': {
source: require('../../assets/fonts/Inter-594377.ttf'),
},
'referenced-image-2929282': riveImage,
'referenced_audio-2929340': {
source: require('../../assets/audio/referenced_audio-2929340.wav'),
},
},
}
);
if (isLoading) {
return <ActivityIndicator />;
} else if (error) {
return (
<View style={styles.safeAreaViewContainer}>
<Text>Error loading Rive file: {error.message}</Text>
</View>
);
}
return (
<RiveView
file={riveFile}
fit={Fit.Contain}
style={styles.rive}
stateMachineName="State Machine 1"
artboardName="Artboard"
/>
);
}
function ErrorFallback({ error, reset }: { error: Error; reset: () => void }) {
return (
<View style={styles.errorContainer}>
<Text style={styles.errorText}>Error loading image:</Text>
<Text style={styles.errorMessage}>{error.message}</Text>
<Button title="Try Again" onPress={reset} />
</View>
);
}
export default function OutOfBandAssetsWithSuspenseExample() {
const [uri, setUri] = React.useState<ImageURLS>(ImageURL1);
const [errorBoundaryKey, setErrorBoundaryKey] = React.useState(0);
const handleReset = () => {
setErrorBoundaryKey((k) => k + 1);
};
const renderErrorFallback = (error: Error, reset: () => void) => (
<ErrorFallback
error={error}
reset={() => {
reset();
handleReset();
}}
/>
);
return (
<View style={styles.safeAreaViewContainer}>
<ErrorBoundary key={errorBoundaryKey} fallback={renderErrorFallback}>
<React.Suspense
fallback={
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" />
<Text style={styles.loadingText}>Loading image...</Text>
</View>
}
>
<RiveContent imageUrl={uri} />
</React.Suspense>
</ErrorBoundary>
<ScrollView contentContainerStyle={styles.container}>
<View style={styles.pickersWrapper}>
<Text style={styles.description}>
This example uses React Suspense and RiveImages.loadFromURLAsync to
load images on demand.
</Text>
<View style={styles.pickerWrapper}>
<Picker
selectedValue={uri}
onValueChange={(value) => setUri(value)}
mode={'dropdown'}
style={styles.picker}
>
{[ImageURL1, ImageURL2, ImageURLSlow, ImageInvalidURL].map(
(url) => (
<Picker.Item key={url} value={url} label={url} />
)
)}
</Picker>
</View>
</View>
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
safeAreaViewContainer: {
flex: 1,
},
container: {
flexGrow: 1,
alignItems: 'center',
justifyContent: 'flex-start',
},
rive: {
width: '100%',
height: 400,
},
picker: {
flex: 1,
width: '100%',
},
pickerWrapper: {
borderWidth: 1,
borderColor: 'black',
borderRadius: 5,
alignItems: 'center',
margin: 16,
},
pickersWrapper: {
flex: 1,
padding: 16,
alignSelf: 'stretch',
},
loadingContainer: {
width: '100%',
height: 400,
justifyContent: 'center',
alignItems: 'center',
},
loadingText: {
marginTop: 16,
fontSize: 16,
color: '#666',
},
description: {
fontSize: 14,
color: '#666',
marginBottom: 8,
paddingHorizontal: 16,
},
errorContainer: {
width: '100%',
height: 400,
justifyContent: 'center',
alignItems: 'center',
padding: 16,
backgroundColor: '#ffebee',
},
errorText: {
fontSize: 18,
fontWeight: 'bold',
color: '#c62828',
marginBottom: 8,
},
errorMessage: {
fontSize: 14,
color: '#d32f2f',
marginBottom: 16,
textAlign: 'center',
},
});
OutOfBandAssetsWithSuspenseExample.metadata = {
name: 'Out-of-Band Assets (Suspense)',
description:
'Shows how to load images using RiveImages.loadFromURLAsync with React Suspense',
} satisfies Metadata;