-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfileScreen.js
343 lines (266 loc) · 10 KB
/
ProfileScreen.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
341
342
343
/* eslint-disable prettier/prettier */
import React, { useState, useEffect, useContext } from 'react';
import { View, Text, StyleSheet, Image, TouchableOpacity } from 'react-native';
import { AuthContext } from './AuthContext';
import { useLocalStorage } from './useLocalStorage';
import { GlobalContext } from './GlobalContext';
import 'core-js/full/symbol/async-iterator';
// import { DataStore } from 'aws-amplify';
import { DataStore } from 'aws-amplify';
import { Statisticx } from './src/models';
function ProfileScreen() {
const { signOut } = React.useContext(AuthContext);
// const [answers, setAnswers] = useLocalStorage('answers', []);
const { answers, setAnswers } = useContext(GlobalContext);
const [savingValue, setSavingValue] = useLocalStorage('savingValue', 0);
const [totalValue, setTotalValue] = useLocalStorage('totalValue', 0);
const [savingValueStoredToCloud, setSavingValueStoredToCloud] = useLocalStorage('savingValueStoredToCloud', 0);
const [totalValueStoredToCloud, setTotalValueStoredToCloud] = useLocalStorage('totalValueStoredToCloud', 0);
const [currentSavingValue, setcurrentSavingValue] = useState(0);
const [currentTotalValue, setcurrentTotalValue] = useState(0);
const [currentSavingValueText, setcurrentSavingValueText] = useState('calculating...', '');
const [currentTotalValueText, setcurrentTotalValueText] = useState('calculating...', '');
const [username, setUsername] = useLocalStorage('username', '');
useEffect(() => {
const interval = setInterval(() => {
calculateValues();
}, 5000); // Change tip every 20 seconds
return () => {
clearInterval(interval); // Clear the interval when the component unmounts
};
}, [savingValue, totalValue, savingValueStoredToCloud, totalValueStoredToCloud, username, currentSavingValue, currentTotalValue, answers, calculateValues]);
const calculateValues = () => {
let currentTotalValue = 0;
let currentSavingValue = 0;
answers.forEach((answer) => {
currentTotalValue += answer.total;
currentSavingValue += answer.saving;
});
// console.log("currentTotalValue",currentTotalValue);
// console.log("currentSavingValue",currentSavingValue);
setcurrentTotalValue(currentTotalValue);
setcurrentSavingValue(currentSavingValue);
let comparisonText = '';
if (currentSavingValue > savingValue && savingValue > 0) {
let compSaving = (currentSavingValue - savingValue) / savingValue * 100;
comparisonText = 'You have increased saved water by !' + compSaving.toFixed(2) + "%";
setcurrentSavingValueText(comparisonText)
} else {
setcurrentSavingValueText('')
}
if (currentTotalValue < totalValue) {
let compTotal = (totalValue - currentTotalValue) / totalValue * 100;
comparisonText = 'You have decreased water footprint by !' + compTotal.toFixed(2) + "%";
setcurrentTotalValueText(comparisonText)
} else {
setcurrentTotalValueText('')
}
//console.log("no db",totalValue , totalValueStoredToCloud , savingValue, savingValueStoredToCloud,currentSavingValue, currentTotalValue)
if (currentTotalValue != totalValueStoredToCloud || currentSavingValue != savingValueStoredToCloud) {
console.log("go to db", totalValue, totalValueStoredToCloud, savingValue, savingValueStoredToCloud);
saveUserDataToAttributes();
} else {
console.log("no db", totalValue, totalValueStoredToCloud, savingValue, savingValueStoredToCloud, currentSavingValue, currentTotalValue)
}
};
const saveUserDataToAttributes = async () => {
try {
console.log("usernamex", username)
if (username === "" || username === "-" || username.trim() === "") {
console.log('user name not retrived');
return;
}
// console.log('Post saved successfully!',r);
const currentTime = new Date().toISOString();
const posts = await DataStore.query(Statisticx, (c) => c.username.eq(username));
console.log('Posts retrieved successfully!', JSON.stringify(posts, null, 2));
if (posts.length > 0) {
console.log("update called")
const updatedUserData = posts[0];
const updatedPost = await DataStore.save(
Statisticx.copyOf(updatedUserData, updated => {
//updated.savedvalue = savingValue;
//updated.totalvalue = totalValue;
updated.currerntsavedvalue = currentSavingValue;
updated.currentotalvalue = currentTotalValue;
updated.lastupdatetime = currentTime;
updated.visitcount = updatedUserData.visitcount + 1;
})
);
console.log('User data updated successfully!', updatedPost);
setTotalValueStoredToCloud(currentTotalValue);
setSavingValueStoredToCloud(currentSavingValue);
} else {
console.log("saved called")
if (username === "" || username === "-" || username.trim() === "") {
console.log('user name not retrived');
return;
}
const r = await DataStore.save(
new Statisticx({
"username": username,
"savedvalue": savingValue,
"totalvalue": totalValue,
"currerntsavedvalue": currentSavingValue,
"currentotalvalue": currentTotalValue,
"startdate": currentTime,
"visitcount": 1,
"lastupdatetime": currentTime
})
);
console.log("savedx", r);
}
setTotalValueStoredToCloud(currentTotalValue);
setSavingValueStoredToCloud(currentSavingValue);
} catch (error) {
console.log('Error :', error);
}
};
return (
<View style={styles.container}>
<View style={styles.card}>
<View style={styles.header}>
<Text style={styles.title}>Hi, Welcome</Text>
<Text style={styles.username}>{username}!</Text>
</View>
<View style={styles.waterDropContainer}>
<Image source={require('./images/category/drop.png')} style={styles.waterDropImage} />
</View>
<View style={styles.infoContainer}>
{currentSavingValueText !== '' ? (<>
<View style={styles.infoRow}>
<Text style={styles.infoText}>You saved</Text>
<Text style={styles.infoValue}>{currentSavingValue} L</Text>
<Text style={styles.infoText}>water!</Text>
</View>
</>) : (<>
{/* <View style={styles.infoRow}>
<Text style={styles.infoText}>You saved</Text>
<Text style={styles.infoValue}>{savingValue} L</Text>
<Text style={styles.infoText}>water!</Text>
</View> */}
<View style={styles.infoRow}>
<Text style={styles.infoText}>Your saved</Text>
<Text style={[styles.infoValue, { color: currentSavingValue >= 0 ? 'green' : 'red' }]}>
{currentSavingValue >= 0 ? currentSavingValue + ' L' : '0'}
</Text>
<Text style={styles.infoText}> water!</Text>
</View></>
)}
{currentTotalValueText !== '' ? (<>
<View style={styles.infoRow}>
<Text style={styles.infoText}>Your water footprint</Text>
<Text style={styles.infoValue}>{currentTotalValue} L!</Text>
</View>
</>) : (<>
<View style={styles.infoRow}>
<Text style={styles.infoText}>Your water footprint</Text>
<Text style={styles.infoValue}>{totalValue} L!</Text>
</View>
</>)}
{/* <View style={styles.infoRow}>
<Text style={styles.infoText}>Istanbul dam fill rate </Text>
<Text style={styles.infoValue}>95%</Text>
</View> */}
{currentSavingValueText !== '' ? (<><View style={styles.infoRow}>
<Text style={styles.infoText}>{currentSavingValueText} </Text>
</View>
</>) : (<></>)}
{currentTotalValueText !== '' ? (<>
<View style={styles.infoRow}>
<Text style={styles.infoText}>{currentTotalValueText} </Text>
</View></>) : (<></>)}
</View>
{/* {<TouchableOpacity style={styles.signOutButton} onPress={saveUserDataToAttributes}>
<Text style={styles.signOutButtonText}>Submit my data to dashboard</Text>
</TouchableOpacity>} */}
<Text style={styles.signOutButtonText}></Text>
<TouchableOpacity style={styles.signOutButton} onPress={signOut}>
<Text style={styles.signOutButtonText}>Sign Out</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
padding: 20,
},
card: {
backgroundColor: '#fff', // A light color for the card
borderRadius: 20, // Rounded corners
shadowColor: '#000', // Shadow for depth
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 3.84,
elevation: 5, // Elevation for Android
width: '100%', // Adjust as needed
maxWidth: 500, // Maximum width for larger devices
padding: 20, // Inner padding
},
header: {
marginBottom: 40,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#333',
},
username: {
fontSize: 24,
fontWeight: 'bold',
color: '#3498db',
},
waterDropContainer: {
alignItems: 'center',
marginBottom: 30,
},
waterDropImage: {
width: 120,
height: 120,
},
savingValue: {
fontSize: 24,
fontWeight: 'bold',
marginTop: 10,
color: '#333',
},
infoContainer: {
marginBottom: 30,
width: '100%',
},
infoRow: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 10,
},
infoText: {
fontSize: 18,
color: '#666',
marginRight: 10,
},
infoValue: {
fontSize: 18,
fontWeight: 'bold',
color: '#333',
},
signOutButton: {
backgroundColor: '#e74c3c',
paddingVertical: 15,
paddingHorizontal: 30,
borderRadius: 10,
},
signOutButtonText: {
fontSize: 14,
fontWeight: 'bold',
color: '#fff',
},
});
export default ProfileScreen;