-
Notifications
You must be signed in to change notification settings - Fork 22
/
ApplicationRoot.js
281 lines (266 loc) · 10.3 KB
/
ApplicationRoot.js
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import React, { useState, useCallback, useEffect } from 'react';
import { StyleSheet, View, TouchableOpacity, Linking, Platform, Animated } from 'react-native';
import AboutUsScreen from './screens/AboutUsScreen';
import HomeSearchScreen from './screens/HomeSearchScreen.js';
import ChatSessionHistoriesScreen from './screens/ChatSessionHistoriesScreen';
import MyPalettesScreen from './screens/MyPalettesScreen.js';
import Colors from './constants/Styles';
import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import ColorDetailsScreen from './screens/ColorDetailScreen';
import PalettesScreen from './screens/PalettesScreen';
import SavePaletteScreen from './screens/SavePaletteScreen';
import ChatSessionScreen from './screens/ChatSessionScreen';
import ColorListScreen from './screens/ColorListScreen';
import PaletteViewScreen from './screens/PaletteViewScreen.js';
import PaletteEditScreen from './screens/PaletteEditScreen.js';
import ProVersionScreen from './screens/ProVersionScreen';
import CommonPalettesScreen from './screens/CommonPalettesScreen';
import PaletteLibraryScreen from './screens/PaletteLibraryScreen';
import HamburgerMenu from './components/HamburgerMenu';
import SideMenu from 'react-native-side-menu';
import { HEADER_HEIGHT } from './constants/Layout';
import Entypo from 'react-native-vector-icons/Entypo';
import { t } from 'i18next';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import useIAPConnection from './hooks/useIAPConnection';
import { ROUTE_NAMES } from './libs/constants.js';
import AppAuthProvider from './components/AppAuthProvider.js';
import UserProfile from './screens/UserProfileScreen.js';
import useApplicationStore from './hooks/useApplicationStore.js';
import ExplorePaletteScreen from './screens/ExplorePaletteScreen.js';
import { notifyMessage } from './libs/Helpers.js';
import FlashMessage from 'react-native-flash-message';
import ShareMenu from 'react-native-share-menu';
import Color from 'pigment/full';
import { logEvent } from './libs/Helpers.js';
const Stack = createNativeStackNavigator();
/*import { LogBox } from 'react-native'; // enabled for recording demos
LogBox.ignoreAllLogs();//Ignore all log notifications*/
export default function App() {
const applicationState = useApplicationStore();
const { loadInitPaletteFromStore } = applicationState;
const [isMenuOpen, setMenu] = useState(false);
const navigationRef = useNavigationContainerRef();
useIAPConnection(function (error) {
if (error) {
// TODO: figure out a better way to handle this error and show user a way to retry, ask for help or continue.
notifyMessage('Error during purchase initialization. Purchase might not work. Please retry');
}
loadInitPaletteFromStore(); // Still load the palettes. Specially simulator will always face this issue.
});
const hamburgerMenuIcon = () => (
<TouchableOpacity onPress={() => setMenu(!isMenuOpen)}>
<Entypo name="menu" style={styles.sideMenuIcon} />
</TouchableOpacity>
);
const handleDeepLink = useCallback(
(url) => {
try {
const urlParts = url.split('?');
const path = urlParts[0].split('/');
const colorsPart = path[path.length - 1];
// Split the colors part by `-` to get the color codes
const colors = colorsPart.split('-');
// Parse the query parameters
const queryParamsString = urlParts[1] || '';
const queryParams = {};
queryParamsString.split('&').forEach((param) => {
const [key, value] = param.split('=');
queryParams[key] = decodeURIComponent(value);
});
const suggestedName = queryParams['name'];
navigationRef.navigate('SavePalette', {
colors: colors.map((color) => {
return { color: '#' + color };
}),
suggestedName: suggestedName
});
} catch (error) {
notifyMessage('Error parsing url: ' + error.message);
navigationRef.navigate(ROUTE_NAMES.HOME_SEARCH);
}
},
[navigationRef]
);
useEffect(() => {
if (Platform.OS === 'android' || Platform.OS === 'ios') {
// Handle deep link when the app is launched
Linking.getInitialURL().then((url) => {
if (url) {
logEvent('deep_linking_open_link');
handleDeepLink(url);
}
});
// Handle deep link when the app is already running
const linkingListener = Linking.addEventListener('url', (event) => {
if (event.url) {
logEvent('deep_linking_open_link');
handleDeepLink(event.url);
}
});
// Cleanup listener on unmount
return () => {
linkingListener.remove();
};
}
}, [handleDeepLink, navigationRef]);
const handleShare = useCallback(
(item) => {
if (item && item.mimeType === 'text/plain' && item.data) {
const text = item.data;
notifyMessage('Shared text: ' + text);
const colors = Color.parse(text);
logEvent('get_shared_text', { length: colors.length });
for (let i = 0, l = colors.length; i < l; i++) {
colors[i] = { color: colors[i].tohex().toLowerCase() };
}
navigationRef.navigate('SavePalette', { colors });
}
},
[navigationRef]
);
useEffect(() => {
ShareMenu.getInitialShare(handleShare);
const listener = ShareMenu.addNewShareListener(handleShare);
return () => {
listener.remove();
};
}, [handleShare]);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<SideMenu
animationFunction={(prop, value) =>
Animated.spring(prop, {
toValue: value,
friction: 8,
useNativeDriver: true
})
}
menu={
<HamburgerMenu navigation={navigationRef} toggleSideMenu={() => setMenu(!isMenuOpen)} />
}
isOpen={isMenuOpen}
onChange={(isOpen) => setMenu(isOpen)}>
<View style={styles.container}>
<NavigationContainer ref={navigationRef}>
<AppAuthProvider>
<Stack.Navigator
screenOptions={{
...screenOptions,
headerStyle: {
...screenOptions.headerStyle,
height: HEADER_HEIGHT
}
}}>
<Stack.Screen
name={ROUTE_NAMES.HOME_SEARCH}
options={{
headerLeft: hamburgerMenuIcon,
headerTitleContainerStyle: { left: 40 },
title: t('HueHive AI')
}}
component={HomeSearchScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.CHAT_SESSION}
options={{
headerLeft: hamburgerMenuIcon,
headerTitleContainerStyle: { left: 40 },
title: t('HueHive AI chat')
}}
component={ChatSessionScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.MY_PALETTES}
options={() => {
return {
headerLeft: hamburgerMenuIcon,
headerTitleContainerStyle: { left: 40 },
title: t('My Palettes')
};
}}
component={MyPalettesScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.ABOUT_US}
options={{ title: t('About us') }}
component={AboutUsScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.CHAT_SESSION_HISTORIES}
options={{ title: t('Your chats') }}
component={ChatSessionHistoriesScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.COLOR_DETAILS}
options={{ title: t('Color details') }}
component={ColorDetailsScreen}
/>
<Stack.Screen name={ROUTE_NAMES.PALETTES} component={PalettesScreen} />
<Stack.Screen
name={ROUTE_NAMES.SAVE_PALETTE}
options={{ title: t('Save palette') }}
component={SavePaletteScreen}
/>
<Stack.Screen name="Palette" component={PaletteViewScreen} />
<Stack.Screen name="PaletteEdit" component={PaletteEditScreen} />
<Stack.Screen
name={ROUTE_NAMES.PRO_VERSION}
options={{ title: t('Pro benefits') }}
component={ProVersionScreen}
/>
<Stack.Screen name="CommonPalettes" component={CommonPalettesScreen} />
<Stack.Screen
name={ROUTE_NAMES.PALETTE_LIBRARY}
options={{ title: t('Palette library') }}
component={PaletteLibraryScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.COLOR_LIST}
options={{ title: t('New palette') }}
component={ColorListScreen}
/>
<Stack.Screen
name={ROUTE_NAMES.USER_PROFILE}
options={{ title: t('Profile') }}
component={UserProfile}
/>
<Stack.Screen
name={ROUTE_NAMES.EXPLORE_PALETTE}
options={{ title: t('Explore palette') }}
component={ExplorePaletteScreen}
/>
</Stack.Navigator>
</AppAuthProvider>
</NavigationContainer>
</View>
<FlashMessage floating={true} position={{ bottom: 100 }} />
</SideMenu>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
flexDirection: 'row'
},
sideMenuIcon: {
fontSize: 25,
height: 25,
color: 'white',
paddingRight: 15
}
});
const screenOptions = {
headerStyle: {
backgroundColor: Colors.primary,
shadowOpacity: 0.3,
shadowRadius: 4,
shadowColor: 'black',
shadowOffset: { height: 3, width: 0 },
borderBottomWidth: 0
},
headerTintColor: Colors.white
};