-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathApp.tsx
More file actions
135 lines (118 loc) · 3.41 KB
/
Copy pathApp.tsx
File metadata and controls
135 lines (118 loc) · 3.41 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
import { useStore } from '@callstack/brownie';
import { useEffect } from 'react';
import './BrownfieldStore.brownie';
import { StyleSheet, Text, View, Button, TextInput } from 'react-native';
import {
createNativeStackNavigator,
type NativeStackScreenProps,
} from '@react-navigation/native-stack';
import ReactNativeBrownfield from '@callstack/react-native-brownfield';
import { NavigationContainer } from '@react-navigation/native';
const getRandomValue = () => Math.round(Math.random() * 255);
const getRandomTheme = () => {
const primary = [getRandomValue(), getRandomValue(), getRandomValue()];
const secondary = [
255 - (primary?.[0] || 0),
255 - (primary?.[1] || 0),
255 - (primary?.[2] || 0),
];
return {
primary: `rgb(${primary[0]}, ${primary[1]}, ${primary[2]})`,
secondary: `rgb(${secondary[0]}, ${secondary[1]}, ${secondary[2]})`,
};
};
type RootStackParamList = {
Home: { theme: { primary: string; secondary: string } };
};
type HomeScreenProps = NativeStackScreenProps<RootStackParamList, 'Home'>;
const theme = getRandomTheme();
function HomeScreen({ navigation, route }: HomeScreenProps) {
const colors = route.params?.theme || theme;
const [counter, setState] = useStore('BrownfieldStore', (s) => s.counter);
const [user] = useStore('BrownfieldStore', (s) => s.user);
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
const isFirstRoute = !navigation.canGoBack();
ReactNativeBrownfield.setNativeBackGestureAndButtonEnabled(isFirstRoute);
});
return unsubscribe;
}, [navigation]);
return (
<View style={[styles.container, { backgroundColor: colors.primary }]}>
<Text style={[styles.title, { color: colors.secondary }]}>
React Native Screen
</Text>
<Text style={[styles.text, { color: colors.secondary }]}>
Count: {counter}
</Text>
<TextInput
style={styles.input}
value={user.name}
onChangeText={(text) =>
setState((prev) => ({ user: { ...prev.user, name: text } }))
}
placeholder="User name"
/>
<Button
onPress={() => setState((prev) => ({ counter: prev.counter + 1 }))}
color={colors.secondary}
title="Increment"
/>
<Button
onPress={() => {
navigation.push('Home', {
theme: getRandomTheme(),
});
}}
color={colors.secondary}
title="Push next screen"
/>
<Button
onPress={() => {
if (navigation.canGoBack()) {
navigation.goBack();
} else {
ReactNativeBrownfield.popToNative(true);
}
}}
color={colors.secondary}
title="Go back"
/>
</View>
);
}
const Stack = createNativeStackNavigator<RootStackParamList>();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
title: {
fontSize: 30,
fontWeight: 'bold',
margin: 10,
},
text: {
fontSize: 16,
margin: 5,
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
padding: 10,
width: 200,
marginVertical: 10,
backgroundColor: '#fff',
},
});