forked from AppsFlyerSDK/appsflyer-expo-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
151 lines (120 loc) · 3.78 KB
/
App.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
import React, { useState, useEffect } from 'react';
import { StyleSheet, View } from 'react-native';
import { useFonts } from 'expo-font';
import { useCallback } from 'react';
import * as SplashScreen from 'expo-splash-screen';
import NavigationBar from './components/NavigationBar';
import Categories from './local_data/Categories.json'
import AppsFlyerHandler from './components/AppsFlyer/AppsFlyerHandler';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
requestTrackingPermissionsAsync,
getTrackingPermissionsAsync,
} from 'expo-tracking-transparency';
SplashScreen.preventAutoHideAsync();
export default function App() {
const [userInviteParams, setUserInvitesParams] = useState({});
const [currentGroceryList, setCurrentGroceryList] = useState([]);
// only for iOS - request ATT
useEffect(() => {
(async () => {
const { status } = await requestTrackingPermissionsAsync();
if (status === 'granted') {
console.log('Yay! I have user permission to track data');
}
})();
}, []);
useEffect(() => {
// retrieve the list from local storage
AsyncStorage.getItem('groceryList')
.then((listData) => {
if (listData != null) {
setCurrentGroceryList(JSON.parse(listData));
}
else {
// set to empty list
setCurrentGroceryList(Categories.categories.map(category => {
return {
"id": category.id,
"name": category.name,
"color": category.color,
"items": []
}
}))
}
});
}, [])
useEffect(() => {
// save the list to local storage
AsyncStorage.setItem('groceryList', JSON.stringify(currentGroceryList));
}, [currentGroceryList]);
// Load main font
const [fontsLoaded] = useFonts({
'Gilroy-Black': require('./assets/fonts/Gilroy-Font.otf'),
});
const onLayoutRootView = useCallback(async () => {
if (fontsLoaded) {
await SplashScreen.hideAsync();
}
}, [fontsLoaded]);
if (!fontsLoaded) {
return null;
}
const updateItemInCurrentGroceryList = (categoryName, itemObj) => {
const categoryIndex = currentGroceryList.findIndex(category => category.name === categoryName);
if (categoryIndex < 0) {
// Category not found, do nothing
return;
}
const category = currentGroceryList[categoryIndex];
const items = category.items.filter(item => item.name !== itemObj.name);
if ((itemObj.unitsMode && itemObj.units > 0) || (!itemObj.unitsMode && itemObj.kg > 0)) {
items.push(itemObj);
}
const updatedCategory = {
...category,
items,
};
const updatedGroceryList = [
...currentGroceryList.slice(0, categoryIndex),
updatedCategory,
...currentGroceryList.slice(categoryIndex + 1),
];
setCurrentGroceryList(updatedGroceryList);
}
return (
<View onLayout={onLayoutRootView} style={styles.container}>
<View style={styles.navigationBar}>
<AppsFlyerHandler
categoryList={Categories.categories}
currentGroceryList={currentGroceryList}
setCurrentGroceryList={setCurrentGroceryList}
userInviteParams={userInviteParams}
/>
<NavigationBar
categoryList={Categories.categories}
currentGroceryList={currentGroceryList}
updateItem={updateItemInCurrentGroceryList}
setUserInvitesParams={setUserInvitesParams}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
backgroundColor: '#6CC733',
},
backgroundImage: {
width: '100%',
height: '100%',
},
navigationBar: {
width: '100%',
height: '100%',
}
});