-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
393 lines (337 loc) · 13.4 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
//123
import React, { useState, useRef, useEffect } from 'react';
import { View, ScrollView, DrawerLayoutAndroid } from 'react-native'
import {
Appbar,
Button,
TextInput,
Title, Subheading, Text,
IconButton,
Surface,
Colors,
Drawer,
Dialog, Portal
} from 'react-native-paper'
import AsyncStorage from '@react-native-community/async-storage';
import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import * as Animatable from 'react-native-animatable';
import SplashScreen from 'react-native-splash-screen';
import { Picker } from '@react-native-picker/picker'
const ItemCard = ({ i, e, tocalc, cred_txtchange, sub_txtchange, delItem, pnt_txtchange }) => {
console.log("itemcard" + i);
const thisref = useRef(null);
return (
<Animatable.View ref={thisref}>
<Surface style={{ borderRadius: 20, margin: 10, elevation: 5, padding: 20, backgroundColor: 'lightyellow' }}>
<TextInput editable={tocalc == 'gpa'} dense="true" style={{ marginBottom: 10, backgroundColor: 'transparent', fontWeight:'bold' }}
placeholder={(tocalc == 'gpa' ? 'SUBJECT ' : 'SEMESTER ') + (i + 1)}
onChangeText={(text) => { sub_txtchange(text, i) }} value={e.sub} />
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-evenly' }}>
<TextInput dense="true" mode="outlined"
onChangeText={(text) => cred_txtchange(text, i)}
style={{ width: 100, marginRight: 10, backgroundColor: Colors.white }}
label="CREDITS" value={e.cred.toString()} keyboardType="decimal-pad" />
<TextInput dense="true" mode="outlined"
onChangeText={(text) => pnt_txtchange(text, i)}
style={{ width: 100, backgroundColor: Colors.white }}
label={tocalc == 'cgpa' ? 'GPA' : 'POINTS'} value={e.pnt.toString()} keyboardType="number-pad" />
<IconButton color={Colors.red500} icon="delete" size={25} onPress={() => { thisref.current.bounceOut(800).then(() => delItem(i)) }} />
</View>
</Surface>
</Animatable.View>
)
}
const CalcCont = ({ tocalc, list, storefunc, navigation }) => {
console.log(tocalc + "render");
const [st, setst] = useState(list);
const [saveVis, setsaveVis] = useState(false);
//const [resVis, setresVis] = useState(false);
const [resTxt, setresTxt] = useState("");
const [nametxt, setnametxt] = useState("");
const [autofillVis, setAutofillVis] = useState(false);
const [colPickState, setColPickState] = useState("");
const [deptPickState, setDeptPickState] = useState("");
const [semPickState, setSemPickState] = useState("sem4");
const key_val = useRef(0);
const autofillData = useRef({ data: {} });
const addsub = () => {
//alert(1)
setst([...st, { sub: '', cred: '', pnt: '' }])
setTimeout(() => scrollViewRef.current.scrollToEnd({ animated: true }), 0);
}
const cred_txtchange = (text, i) => {
let cur = [...st];
cur[i].cred = parseInt(text) || 0;
setst(cur)
}
const pnt_txtchange = (text, i) => {
let cur = [...st];
cur[i].pnt = text;
setst(cur)
}
const sub_txtchange = (text, i) => {
let cur = [...st];
cur[i].sub = text;
setst(cur)
}
const showAutofill = async () => {
fetch("https://github.com/agilan2001/Grader/raw/master/data/gpa_data.json")
.then((res) => res.json())
.then((data) => {
autofillData.current = data.data;
var temp = Object.keys(data.data)[0];
if (!colPickState) setColPickState(temp);
if (!deptPickState) setDeptPickState(Object.keys(data.data[temp])[0]);
setAutofillVis(true);
});
}
const autofill = () => {
var data = autofillData.current[colPickState][deptPickState];
var new_st = [];
if (tocalc == 'gpa') {
Object.keys(data[semPickState]).forEach((e, i) => {
new_st.push({
sub: e,
cred: data[semPickState][e],
pnt: ""
})
});
} else {
for (var i = 1; i <= parseInt(semPickState.replace(/\D/g,'')); i++) {
var tot = 0;
Object.keys(data["sem" + i]).forEach((e, k) => {
tot += parseInt(data["sem" + i][e]);
});
new_st.push({
sub: "",
cred: tot.toString(),
pnt: ""
})
}
}
setst(new_st);
}
const calc = () => {
var sop = 0, sc = 0;
st.forEach((e, i) => {
sop += (parseInt(e.cred) || 0) * (parseFloat(e.pnt) || 0);
sc += parseInt(e.cred) || 0;
});
var res_gpa = ((sop / sc) || 0).toFixed(2);
setresTxt(res_gpa);
// Alert.alert("WoW !", `Your ${tocalc == 'cgpa' ? 'C' : ''}GPA is ${res_gpa}`,
// [{
// text: "SAVE",
// onPress: () => { setnametxt((tocalc == 'gpa' ? 'GPA (' : 'CGPA (') + new Date().toLocaleDateString("en-US") + ')'); setsaveVis(true) }
// },
// { text: "OK" }],
// { cancelable: true }
// )
}
const delItem = (p) => {
console.log(p)
let cur = [...st];
console.log(cur)
cur.splice(p, 1);
console.log(cur)
key_val.current++;
setst(cur);
}
useEffect(() => {
setst(list);
console.log(tocalc)
if (list[0].cred) setTimeout(() => navigation.navigate(tocalc), 0)
}, [list, tocalc])
const scrollViewRef = useRef();
return (
<View style={{backgroundColor:tocalc=='gpa'?'lightgreen':'lightblue', flex:1}}>
<View style={{ flexDirection: 'row', justifyContent: 'space-evenly', marginTop: 20, marginBottom: 10 }}>
<Button icon="flash-auto" style={{ width: 80, backgroundColor:'blue' }} mode="contained" onPress={showAutofill}>AUTO</Button>
<Button icon="plus" style={{ width: 120, backgroundColor:'orange' }} mode="contained" onPress={addsub}>{tocalc == 'cgpa' ? 'SEMESTER' : 'SUBJECT'}</Button>
<Button icon='calculator' style={{ width: 120 }} mode="contained" onPress={calc}>CALCULATE</Button>
</View>
<ScrollView
ref={scrollViewRef} style={{ marginBottom: 20 }}
>
{st.map((e, i) => (
<ItemCard key={key_val.current + i.toString()} i={i} e={e} tocalc={tocalc} cred_txtchange={cred_txtchange} sub_txtchange={sub_txtchange} delItem={delItem} pnt_txtchange={pnt_txtchange} />
))}
</ScrollView>
<Portal>
<Dialog style={{ backgroundColor: 'yellow' }} visible={autofillVis} onDismiss={() => { setAutofillVis(false); }}>
<Dialog.Content>
<Title style={{ textAlign: 'center' }}>AUTOFILL</Title>
<Picker selectedValue={colPickState} onValueChange={(e, i) => setColPickState(e)}>
{Object.keys(autofillData.current).map((e, i) => (<Picker.Item key={key_val.current + "clg" + i} label={e} value={e} />))}
</Picker>
<Picker selectedValue={deptPickState} onValueChange={(e, i) => setDeptPickState(e)}>
{colPickState && Object.keys(autofillData.current[colPickState]).map((e, i) => (<Picker.Item key={key_val.current + "dept" + i} label={e} value={e} />))}
</Picker>
<Picker selectedValue={semPickState} onValueChange={(e, i) => setSemPickState(e)}>
{colPickState && deptPickState && [...Array(8).keys()].map((e, i) => (<Picker.Item key={key_val.current + "sem" + i} label={"SEMESTER " + (e + 1)} value={"sem" + (e + 1)} />))}
</Picker>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={() => { setAutofillVis(false); }}>CANCEL</Button>
<Button onPress={() => { autofill(); setAutofillVis(false); }}>OK</Button>
</Dialog.Actions>
</Dialog>
<Dialog style={{ backgroundColor: 'lightblue' }} visible={resTxt} onDismiss={() => { setresTxt(""); }}>
<Dialog.Content>
<Title style={{ textAlign: 'center' }}>{tocalc == 'cgpa' ? 'C' : ''}GPA</Title>
<Text style={{ textAlign: 'center', fontSize: 50, fontWeight: 'bold', color: 'red', }}>{resTxt}</Text>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={() => { setnametxt((tocalc == 'gpa' ? 'GPA (' : 'CGPA (') + new Date().toLocaleDateString("en-US") + ')'); setsaveVis(true); setresTxt("") }}>SAVE</Button>
<Button onPress={() => { setresTxt(""); }}>OK</Button>
</Dialog.Actions>
</Dialog>
<Dialog style={{ backgroundColor: 'orange' }} visible={saveVis} onDismiss={() => { setsaveVis(false); }}>
<Dialog.Content>
<Subheading>Enter Reference Name : </Subheading>
<TextInput defaultValue={nametxt} selectTextOnFocus={true} dense={true} style={{ backgroundColor: Colors.white }}
onChangeText={(text) => setnametxt(text)} ></TextInput>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={() => { setsaveVis(false); }}>CANCEL</Button>
<Button onPress={() => { storefunc({ name: nametxt, data: st }, tocalc); setsaveVis(false); }}>SAVE</Button>
</Dialog.Actions>
</Dialog>
</Portal>
</View>
)
}
const Tab = createMaterialTopTabNavigator();
const TabNav = ({ storefunc, gpaList, cgpaList }) => {
console.log("TabNav render")
return (
<NavigationContainer>
<Tab.Navigator
tabBarOptions={{
activeTintColor: 'green',
showIcon: true,
// scrollEnabled:true,
}}
>
<Tab.Screen
name="gpa"
options={{
tabBarLabel: 'GPA',
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="star-box" color={color} size={26} />
),
}}
>
{(props) => <CalcCont {...props} storefunc={storefunc} tocalc="gpa" list={gpaList} />}
</Tab.Screen>
<Tab.Screen
name="cgpa"
options={{
tabBarLabel: 'CGPA',
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="star-box-multiple" color={color} size={26} />
),
}}
>
{(props) => <CalcCont {...props} storefunc={storefunc} tocalc="cgpa" list={cgpaList} />}
</Tab.Screen>
</Tab.Navigator>
</NavigationContainer>
);
}
const App = () => {
console.log("App render")
const menu = useRef();
const [hist, sethist] = useState({
gpa: [],
cgpa: []
});
console.log('hist at app' + JSON.stringify(hist))
const [gpaList, setgpaList] = useState(Array.apply(null, Array(5)).map(_ => ({ sub: "", cred: "", pnt: '' })));
const [cgpaList, setcgpaList] = useState(Array.apply(null, Array(2)).map(_ => ({ sub: "", cred: "", pnt: '' })));
const storeData = async (sdata, toudp) => {
try {
console.log('hist' + JSON.stringify(hist))
var temp = { ...hist };
temp[toudp].push(JSON.parse(JSON.stringify(sdata)));
console.log('temp :' + JSON.stringify(temp))
sethist(temp);
console.log('hist after :' + JSON.stringify(hist))
const jsonValue = JSON.stringify(temp)
await AsyncStorage.setItem('@storage_Key', jsonValue)
} catch (e) {
// saving error
}
}
//console.log(hist);
useEffect(() => {
console.log('fetch storage')
const getData = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@storage_Key')
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
// error reading value
}
}
getData().then((val) => {
if (val) sethist(val);
SplashScreen.hide();
})
}, [])
const navigationView = (
<View style={{ flexDirection: 'column', flex: 1 }}>
<View style={{ backgroundColor: 'green' }}>
<Text style={{ fontSize: 30, fontWeight: 'bold', color: 'yellow', padding: 10 }}>GRADER</Text>
<Text style={{ padding: 10, paddingTop: 0, color: 'white', fontWeight: 'bold', fontSize: 20 }}>Grade Points Calculator</Text>
</View>
<Drawer.Section title="Saved GPA" style={{ flex: 1 }}>
<ScrollView>
{hist.gpa.length ?
JSON.parse(JSON.stringify(hist)).gpa.map((e, i) => (<Drawer.Item key={i}
label={e.name}
onPress={() => { setgpaList(e.data); menu.current.closeDrawer() }}
/>))
: <Drawer.Item label={'[Empty]'} />}
</ScrollView>
</Drawer.Section>
<Drawer.Section title="Saved CGPA" style={{ flex: 1 }}>
<ScrollView>
{hist.cgpa.length ?
JSON.parse(JSON.stringify(hist)).cgpa.map((e, i) => (<Drawer.Item key={i}
label={e.name}
onPress={() => { setcgpaList(e.data); menu.current.closeDrawer() }}
/>))
: <Drawer.Item label={'[Empty]'} />}
</ScrollView>
</Drawer.Section>
<View>
<Text style={{ fontSize: 20, fontWeight: 'bold', color: 'red', padding: 10, textAlign: 'center' }}>[ AgiApps ]</Text>
</View>
</View>
);
return (
<DrawerLayoutAndroid
ref={menu}
drawerWidth={300}
drawerPosition="left"
renderNavigationView={() => navigationView}
>
<Appbar.Header>
<Appbar.Action icon="menu" onPress={() => { menu.current.openDrawer() }} />
<Appbar.Content title="GRADER" subtitle={'AgiApps'} />
</Appbar.Header>
<TabNav storefunc={storeData} gpaList={gpaList} cgpaList={cgpaList} />
</DrawerLayoutAndroid>
);
};
export default App;