-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
199 lines (182 loc) · 5.79 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
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
import React, { useEffect, useState } from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import AsyncStorage from '@react-native-community/async-storage';
import Constants from 'expo-constants';
import * as Permissions from 'expo-permissions';
import Dashboard from './screens/Dashboard';
import FavoriteList from './screens/favoriteList';
import FavFocus from './screens/FavFocus';
import ButtonBar from './components/ButtonBar';
import RickRoll from './screens/RICKROLL';
import Quotalicious from './splashScreens/quotalicious';
import {
randomQuote,
randomPicture,
getKanye,
getTaylor,
getDonald,
pictureBW,
pictureBlur,
} from './ApiClientService';
const Stack = createStackNavigator();
const App = () => {
const [quote, setQuote] = useState({});
const [picture, setPicture] = useState('');
const [key, setKey] = useState('0');
const [favorites, setFavorites] = useState([]);
const [quoteType, setQuoteType] = useState('random');
const [pictureType, setPictureType] = useState('random');
const [displaySettings, setDisplaySettings] = useState(false);
const [displayForm, setDisplayForm] = useState(false);
const [isLiked, setIsLiked] = useState(false);
//const [displayQuotalicious, setDisplayQuotalicious] = useState(true);
const objToSave = {
quote: quote.quote,
author: quote.author,
picture: picture,
};
// DATA STORAGE
const getSavedFavorites = async () => {
try {
const gottenKeys = await AsyncStorage.getAllKeys();
const gottenFavorites = await AsyncStorage.multiGet(gottenKeys);
const favsToGet = gottenFavorites.map(item => item);
setFavorites(favsToGet);
} catch (error) {
console.error('Houston, we have a problem:', error);
}
};
const storeData = async () => {
try {
const jsonObj = JSON.stringify(objToSave);
await AsyncStorage.setItem(key, jsonObj);
setKey(key => (+key + 1).toString());
setFavorites(getSavedFavorites());
} catch (error) {
console.error('oh no something happened:', error);
}
};
const removeFavorite = async unlike => {
try {
await AsyncStorage.removeItem(unlike);
setIsLiked(false);
getSavedFavorites();
} catch (error) {
console.error('no I dunnae think so', error);
}
};
// REQUESTS
const getRandomQuote = () => {
randomQuote().then(res => {
setQuote({
quote: res.quote.quoteText,
author: res.quote.quoteAuthor,
});
});
};
const getRandomPicture = () => {
randomPicture().then(res => setPicture(res.url));
};
const getPictureBW = () => {
pictureBW().then(res => setPicture(res.url));
};
const getPictureBlur = () => {
pictureBlur().then(res => setPicture(res.url));
};
const getKanyeQuote = () => {
let quoteObj = {};
getKanye().then(res => {
quoteObj.quote = res.quote;
quoteObj.author = 'Kanye West';
setQuote(quoteObj);
});
};
const getTaylorQuote = () => {
let quoteObj = {};
getTaylor().then(res => {
quoteObj.quote = res.quote;
quoteObj.author = 'Taylor Swift';
setQuote(quoteObj);
});
};
const getDonaldQuote = () => {
let quoteObj = {};
getDonald().then(res => {
quoteObj.quote = res.value;
quoteObj.author = 'Donald Trump';
setQuote(quoteObj);
});
};
// DECIDE WHICH QUOTES/PICTURES
const whichQuotes = quoteType => {
if (quoteType === 'random') getRandomQuote();
else if (quoteType === 'kanye') getKanyeQuote();
else if (quoteType === 'taylor') getTaylorQuote();
else if (quoteType === 'trump') getDonaldQuote();
setIsLiked(false);
};
const whichPictures = pictureType => {
if (pictureType === 'random') getRandomPicture();
else if (pictureType === 'bw') getPictureBW();
else if (pictureType === 'blur') getPictureBlur();
setIsLiked(false);
};
// PERMISSIONS
const getPermissionIos = async () => {
if (Constants.platform.ios) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Cant save no pics without no permissions');
}
}
};
useEffect(() => {
whichQuotes('random');
getRandomPicture();
getSavedFavorites();
getPermissionIos();
}, []);
return (
<NavigationContainer>
<Stack.Navigator headerMode="none">
<Stack.Screen name="Quotalicious" component={Quotalicious} />
<Stack.Screen name="Dashboard">
{props => (
<Dashboard
whichQuotes={whichQuotes}
quote={quote}
setQuote={setQuote}
quoteType={quoteType}
setQuoteType={setQuoteType}
whichPictures={whichPictures}
picture={picture}
setPicture={setPicture}
pictureType={pictureType}
setPictureType={setPictureType}
displayForm={displayForm}
displaySettings={displaySettings}
setDisplaySettings={setDisplaySettings}
setDisplayForm={setDisplayForm}
storeData={storeData}
removeFavorite={removeFavorite}
isLiked={isLiked}
setIsLiked={setIsLiked}
myKey={key}
{...props}
/>
)}
</Stack.Screen>
<Stack.Screen name="SectionList" component={ButtonBar} />
<Stack.Screen name="FavoriteList">
{props => (
<FavoriteList favorites={favorites} removeFavorite={removeFavorite} {...props} />
)}
</Stack.Screen>
<Stack.Screen name="FavFocus" component={FavFocus} />
<Stack.Screen name="RickRoll" component={RickRoll} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;