-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
340 lines (308 loc) · 13.5 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import React from 'react';
import { Text, View, StatusBar, TouchableOpacity, TouchableWithoutFeedback, ActivityIndicator, Image, Linking, SafeAreaView } from 'react-native';
import Navigator from './Navigator';
import Switch from './layouts/Switch';
import ProfileSetup from './layouts/ProfileSetup';
import EmailSignIn from './layouts/EmailSignIn';
import Browser from './layouts/Browser';
import ErrorBoundary from './ErrorBoundary';
import Svg, { Path, Line, Circle, Polyline, Rect } from 'react-native-svg';
import * as Font from 'expo-font';
import * as AppleAuthentication from 'expo-apple-authentication';
import * as GoogleSignIn from 'expo-google-sign-in';
import * as Localization from 'expo-localization';
import * as ScreenOrientation from 'expo-screen-orientation';
import API from './api';
import makeid from './js/makeid';
TouchableOpacity.defaultProps = TouchableOpacity.defaultProps || {};
TouchableOpacity.defaultProps.delayPressIn = 0;
TouchableWithoutFeedback.defaultProps = TouchableWithoutFeedback.defaultProps || {};
TouchableWithoutFeedback.defaultProps.delayPressIn = 0;
export default class App extends React.Component {
constructor(props){
super(props);
this.state = {
screen: "loading",
moreSignin: false,
activity: false,
premium: API.premium
}
API.event.on("premium", () => {
console.log("$$$$$", API.premium)
this.setState({premium: API.premium});
})
}
async componentDidMount(){
setTimeout(() => {
if(this.state.screen == "loading"){
this.setState({screen: "login"});
}
}, 5000);
this.checkIdentifier();
ScreenOrientation.unlockAsync();
API.event.on("refresh", (type) => {
if(type == "signout"){
this.setState({screen: "login"});
}
})
}
async checkIdentifier(providedIdentifier){
let identifier = providedIdentifier;
if(!identifier){
identifier = await API.getIdentifier();
}
if(identifier != ""){
let user = await API.signIn(identifier);
console.log("Already exists: ", user.language);
if(user.language){
this.setState({screen: "logged"});
}else{
this.setState({screen: "login"});
}
}else{
this.setState({screen: "login"});
}
}
async signInWithApple(){
try {
this.setState({activity: true});
const credential = await AppleAuthentication.signInAsync({
requestedScopes: [
AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
AppleAuthentication.AppleAuthenticationScope.EMAIL,
],
});
let user = await API.signIn(credential.user, "apple", credential);
API.setData("identifier", credential.user);
this.setState({screen: "logged", activity: false});
// signed in
} catch (e) {
if (e.code === 'ERR_CANCELED') {
// handle that the user canceled the sign-in flow
this.setState({activity: false});
} else {
// handle other errors
alert('Make sure to have internet connection and try again later:' + JSON.stringify(e));
this.setState({activity: false});
}
}
}
async signInWithGoogle(){
try {
this.setState({activity: true});
let asdas = await GoogleSignIn.initAsync({
// You may ommit the clientId when the firebase `googleServicesFile` is configured
clientId: '494587339451-56jbil93dcoif248evnj52sff1p4b3ca.apps.googleusercontent.com',
});
console.log("asdasd",asdas);
await GoogleSignIn.askForPlayServicesAsync();
const { type, user } = await GoogleSignIn.signInAsync();
if (type === 'success') {
const credential = await GoogleSignIn.signInSilentlyAsync();
let user = await API.signIn(credential.uid, "google", credential);
API.setData("identifier", credential.uid);
this.setState({screen: "logged", activity: false});
}
} catch ({ message }) {
console.log(message);
alert('Make sure to have internet connection and try again later:' + message);
this.setState({activity: false});
}
}
signInWithEmail(){
this.setState({screen: "email"});
API.event.on("authIdentifier", (identifier) => {
this.checkIdentifier(identifier);
API.setData("identifier", identifier);
});
}
setCurrentProfile(id){
if(id){
API.setCurrentProfile(id);
this.forceUpdate();
}else{
API.getCurrentProfile().then(profile => {
if(profile){
API.setCurrentProfile(profile.id);
}else{
API.setCurrentProfile(API.user.profiles[0].id);
}
this.forceUpdate();
})
}
}
async getStarted(){
this.setState({screen: "loading"});
let pass = makeid(10);
let email = pass + "@leeloo.com";
let identifier = await API.getAuthIdentifier(email, pass);
await API.signIn(identifier, "email", {email: email});
this.checkIdentifier(identifier);
API.setData("identifier", identifier);
}
signInScreen(){
return (
<>
<SafeAreaView style={{justifyContent: "center", alignItems: "center", flex: 1, backgroundColor: "#6989FF"}}>
<View style={{ justifyContent: "center", alignItems: "center", flexDirection: "column", padding: 30, paddingBottom: 0, marginTop: 20}}>
<Text style={[API.styles.h1, {color: "#fff", marginTop: 0, marginHorizontal: 0, fontSize: 28, textAlign: "center"}]}>{API.t("setup_welcome_title1")}</Text>
<Text style={[API.styles.h1, {color: "#fff", marginTop: 0, marginHorizontal: 0, fontSize: 42, textAlign: "center", marginBottom: 15}]}>{API.t("setup_welcome_title2")}</Text>
<Text style={[API.styles.pHome, {marginBottom: 0, marginHorizontal: 0, textAlign: "center"}]}>{API.t("setup_welcome_description")}</Text>
</View>
<Image source={require("./assets/mascot.png")} style={{width: 150, height: 150, flex: 1}} resizeMode={"contain"} />
{this.renderSignInButtons()}
<TouchableOpacity onPress={() => this.setState({screen: "policy"})} style={{marginTop: 15, marginBottom: 30}}>
<Text style={[API.styles.pHome, {textAlign: "center"}]}>
By signing in you accept our <Text style={{fontWeight: "600"}}>Terms of Use</Text> and <Text style={{fontWeight: "600"}}>Privacy Policy</Text>.
</Text>
</TouchableOpacity>
</SafeAreaView>
{this.state.activity &&
<View style={{backgroundColor: "rgba(0,0,0,0.3)", width: "100%", height: "100%", position: "absolute", top: 0, left: 0, justifyContent: "center", alignItems: "center"}}>
<View style={{width: 60, height: 60, backgroundColor: "#fff", alignItems: "center", justifyContent: "center", borderRadius: 30}}>
<ActivityIndicator color={"#6989FF"}/>
</View>
<TouchableOpacity style={{marginTop: 30, position: "absolute", bottom: 30}} onPress={() => this.setState({activity: false})}>
<Text style={{color: "#fff", fontWeight: "bold", fontSize: 18}}>{API.t("alert_cancel")}</Text>
</TouchableOpacity>
</View>
}
</>
)
}
renderSignInButtons(){
if(this.state.moreSignin){
if(Platform.OS == "android"){
return (
<View style={{justifyContent: "center", alignItems: "center", backgroundColor: "#6989FF"}}>
<AppleAuthentication.AppleAuthenticationButton
buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN}
buttonStyle={AppleAuthentication.AppleAuthenticationButtonStyle.WHITE}
cornerRadius={25}
style={{ width: 240, height: 50, borderRadius: 25 }}
onPress={this.signInWithApple.bind(this)}
/>
<View style={{height: 10}}></View>
<TouchableOpacity
style={{ width: 240, height: 46, alignItems: "center", borderRadius: 25, backgroundColor: "#fff", justifyContent: "center", flexDirection: "row"}}
onPress={this.signInWithGoogle.bind(this)}>
<Image source={{uri: "https://developers.google.com/identity/images/g-logo.png"}} style={{width: 18, height: 18, marginRight: 5}}/>
<Text style={{fontSize: 19, fontWeight: "500"}}>Sign in with Google</Text>
</TouchableOpacity>
</View>
);
}else if(Platform.OS == "ios"){
return (
<View style={{justifyContent: "center", alignItems: "center", backgroundColor: "#6989FF"}}>
<AppleAuthentication.AppleAuthenticationButton
buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN}
buttonStyle={AppleAuthentication.AppleAuthenticationButtonStyle.WHITE}
cornerRadius={25}
style={{ width: 240, height: 50, borderRadius: 25 }}
onPress={this.signInWithApple.bind(this)}
/>
<View style={{height: 10}}></View>
<TouchableOpacity
style={{ width: 240, height: 46, alignItems: "center", borderRadius: 25, backgroundColor: "#fff", justifyContent: "center", flexDirection: "row"}}
onPress={this.signInWithGoogle.bind(this)}>
<Image source={{uri: "https://developers.google.com/identity/images/g-logo.png"}} style={{width: 18, height: 18, marginRight: 5}}/>
<Text style={{fontSize: 19, fontWeight: "500"}}>Sign in with Google</Text>
</TouchableOpacity>
<View style={{height: 10}}></View>
<TouchableOpacity
style={{ width: 240, height: 46, alignItems: "center", borderRadius: 25, backgroundColor: "#fff", justifyContent: "center", flexDirection: "row"}}
onPress={this.signInWithEmail.bind(this)}>
<Svg height={18} width={18} viewBox="0 0 24 24" style={{marginRight: 5}} strokeWidth="2" stroke="#333" fill="none" strokeLinecap="round" strokeLinejoin="round">
<Path d="M0 0h24v24H0z" stroke="none"/>
<Rect height="14" width="18" rx="2" x="3" y="5"/>
<Polyline points="3 7 12 13 21 7"/>
</Svg>
<Text style={{fontSize: 19, fontWeight: "500"}}>Sign in with Email</Text>
</TouchableOpacity>
</View>
);
}
}else{
if(Platform.OS == "ios"){
return (
<>
<AppleAuthentication.AppleAuthenticationButton
buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN}
buttonStyle={AppleAuthentication.AppleAuthenticationButtonStyle.WHITE}
cornerRadius={25}
style={{ width: 240, height: 50, borderRadius: 25 }}
onPress={this.signInWithApple.bind(this)}
/>
<TouchableOpacity onPress={() => this.setState({moreSignin: true})}><Text style={{color: "rgba(255,255,255,0.9)", marginTop: 18.5, marginBottom: 20}}>{API.t("setup_other_options")}</Text></TouchableOpacity>
</>
)
}else{
return(
<>
<TouchableOpacity
style={{ width: 240, height: 46, alignItems: "center", borderRadius: 25, backgroundColor: "#fff", justifyContent: "center", flexDirection: "row", marginTop: 10}}
onPress={this.getStarted.bind(this)}>
<Svg height={18} width={18} viewBox="0 0 24 24" style={{marginRight: 5}} strokeWidth="2" stroke="#333" fill="none" strokeLinecap="round" strokeLinejoin="round">
<Path fill={"transparent"} d="M12 17.75l-6.172 3.245 1.179-6.873-4.993-4.867 6.9-1.002L12 2l3.086 6.253 6.9 1.002-4.993 4.867 1.179 6.873z"/>
</Svg>
<Text style={{fontSize: 19, fontWeight: "500", color: "#333"}}>{API.t("get_started")}</Text>
</TouchableOpacity>
</>
);
}
}
}
renderLoading(type){
if(type == "premium"){
setTimeout(() => {
this.setState({premium: "none"});
}, 5000);
}
return (
<View style={{flex: 1, backgroundColor: "#6989FF", justifyContent: "center", alignItems: "center"}}>
<StatusBar backgroundColor="#6989FF" barStyle={"light-content"} />
<View style={{width: 60, height: 60, backgroundColor: "#fff", alignItems: "center", justifyContent: "center", borderRadius: 30}}>
<ActivityIndicator color={"#6989FF"}/>
</View>
</View>
)
}
renderAll() {
let screen = this.state.screen;
if(screen == "login"){
return this.signInScreen();
}else if(screen == "policy"){
return (<Browser link={"https://dreamoriented.org/privacypolicy_textonly"} back={() => this.setState({screen: "login"})}/>);
}else if(screen == "email"){
return (<EmailSignIn back={() => this.setState({screen: "login"})}/>);
}else if(screen == "logged"){
if(API.user.active_profile == "noprofile"){
return (<ProfileSetup done={this.setCurrentProfile.bind(this)}/>);
}else if(API.user.active_profile == "multiple"){
return (<Switch onChoose={this.setCurrentProfile.bind(this)}/>);
}else if(API.user.active_profile){
if(this.state.premium == "determining"){
return this.renderLoading("premium");
}else{
return (
<View style={{flex: 1}}>
<StatusBar backgroundColor="#ffffff" barStyle={"dark-content"} />
<Navigator/>
</View>
);
}
}else{
return (<ProfileSetup done={this.setCurrentProfile.bind(this)}/>);
}
}else if(screen == "loading"){
return this.renderLoading();
}
}
render(){
return (
<ErrorBoundary>
{this.renderAll()}
</ErrorBoundary>
)
}
}