-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.tsx
More file actions
146 lines (131 loc) · 3.46 KB
/
App.tsx
File metadata and controls
146 lines (131 loc) · 3.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
import { useEffect, useState } from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
ActionSheetIOS,
Platform,
Alert,
} from 'react-native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { PagesList, type PageItem } from './PagesList';
import { HomeMenu } from './shared/HomeMenu';
const LAST_OPENED_KEY = '@rive_example_last_opened';
type RootStackParamList = {
Home: undefined;
} & {
[K in (typeof PagesList)[number]['id']]: undefined;
};
const Stack = createStackNavigator<RootStackParamList>();
function invokeGC() {
if (typeof global.gc === 'function') {
global.gc();
Alert.alert('GC', 'Garbage collection invoked');
} else {
Alert.alert('GC', 'GC not available (Hermes debugger not attached)');
}
}
function HeaderMenuButton() {
const navigation = useNavigation<any>();
const openTests = () => {
navigation.navigate('TestsPage');
};
const showDevMenu = () => {
const options = ['Run Tests', 'Invoke GC', 'Cancel'];
const cancelButtonIndex = 2;
if (Platform.OS === 'ios') {
ActionSheetIOS.showActionSheetWithOptions(
{ options, cancelButtonIndex },
(buttonIndex) => {
if (buttonIndex === 0) {
openTests();
} else if (buttonIndex === 1) {
invokeGC();
}
}
);
} else {
Alert.alert('Dev Menu', undefined, [
{ text: 'Run Tests', onPress: openTests },
{ text: 'Invoke GC', onPress: invokeGC },
{ text: 'Cancel', style: 'cancel' },
]);
}
};
return (
<TouchableOpacity onPress={showDevMenu} style={styles.headerButton}>
<Text style={styles.headerButtonText}>🔧</Text>
</TouchableOpacity>
);
}
function HomeScreen({ navigation }: { navigation: any }) {
const [lastOpened, setLastOpened] = useState<PageItem | null>(null);
useEffect(() => {
AsyncStorage.getItem(LAST_OPENED_KEY).then((id) => {
if (id) {
const page = PagesList.find((p) => p.id === id);
if (page) setLastOpened(page);
}
});
}, []);
const handleNavigate = (id: string) => {
AsyncStorage.setItem(LAST_OPENED_KEY, id);
navigation.navigate(id);
};
return (
<View style={styles.container}>
<HomeMenu lastOpened={lastOpened} onNavigate={handleNavigate} />
</View>
);
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerStyle: {
backgroundColor: '#323232',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{
title: 'Rive Examples',
headerRight: HeaderMenuButton,
}}
/>
{PagesList.map(({ id, component, name }) => (
<Stack.Screen
key={id}
name={id}
component={component}
options={{ title: name }}
/>
))}
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
headerButton: {
marginRight: 16,
padding: 4,
},
headerButtonText: {
fontSize: 22,
color: '#fff',
},
});