Commit 417899a7 authored by norhalimah's avatar norhalimah

.......

parent ca5914ae
...@@ -23,19 +23,19 @@ class LogBook extends React.PureComponent { ...@@ -23,19 +23,19 @@ class LogBook extends React.PureComponent {
}; };
} }
displayData = async () => { displayData = async () => {
try { try {
const value = await AsyncStorage.getItem('@storage_key'); const value = await AsyncStorage.getItem('@storage_key');
// console.log(JSON.parse(value));
if (value !== null) { if (value !== null) {
// We have data!! // We have data!!
// console.log(JSON.parse(value)); // console.log(JSON.parse(value));
this.setState({ logbook: JSON.parse(value) }); this.setState({ logbook: JSON.parse(value)});
console.log(this.state.logbook.length); console.log(this.state.logbook);
} }
} catch (error) { } catch (error) {
// Error retrieving data // Error retrieving data
} }
console.log(this.state.logbook);
}; };
componentDidMount() { componentDidMount() {
...@@ -58,30 +58,33 @@ console.log(this.state.logbook); ...@@ -58,30 +58,33 @@ console.log(this.state.logbook);
style={{ style={{
marginTop: 50, marginTop: 50,
}}> }}>
{this.state.logbook && this.state.logbook.map((item, id) => (
<View <View
key ={id}
style={{ style={{
flexDirection: 'row', flexDirection: 'row',
padding: 10, padding: 10,
backgroundColor: 'white', backgroundColor: 'white',
borderBottomWidth:1, borderBottomWidth:1,
}}> }}>
<View style={{alignItems: 'center', paddingLeft:15, paddingRight:15}}> <View style={{alignItems: 'center'}}>
<View style={{marginRight:5, marginBottom:5}}> <View style={{marginRight:10, marginBottom:10}}>
<Text style={{color:'black', fontSize: 40}}>{(this.state.logbook.date !== undefined) ? this.state.logbook.date.split(' ')[0].trim() : ''}</Text> <Text style={{color:'black', fontSize: 40}}>{(item.date !== undefined) ? item.date.split(' ')[0].trim() : ''}</Text>
<Text style={{color:'black', fontSize: 15}}>{(this.state.logbook.date !== undefined) ? this.state.logbook.date.split(' ')[1].trim() : ''}</Text> <Text style={{color:'black', fontSize: 15, marginLeft:11}}>{(item.date !== undefined) ? item.date.split(' ')[1].trim() : ''}</Text>
</View> </View>
<View style={{marginRight:5}}> <View style={{marginRight:5}}>
<Text style={{color:'black', fontSize: 15}}>{this.state.logbook.day}</Text> <Text style={{color:'black', fontSize: 15}}>{item.day}</Text>
</View> </View>
</View> </View>
<View style={{borderWidth:1, borderColor : 'blue'}}/> <View style={{borderWidth:1, borderColor : 'blue'}}/>
<View style={{flexDirection: 'column',marginLeft:5,marginTop:5}}> <View style={{flexDirection: 'column',marginLeft:5,marginTop:5}}>
<Text style={{color:'green', fontSize: 15}}>Correct : {this.state.logbook.correct}</Text> <Text style={{color:'green', fontSize: 15}}>Correct : {item.correct}</Text>
<Text style={{color:'red', fontSize: 15}}>Wrong : {this.state.logbook.wrong}</Text> <Text style={{color:'red', fontSize: 15}}>Wrong : {item.wrong}</Text>
<Text style={{color:'black', fontSize: 15}}>Timer : {this.state.logbook.timer}</Text> <Text style={{color:'black', fontSize: 15}}>Timer : {item.timer}</Text>
<Text style={{color:'black', fontSize: 15}}>Time : {this.state.logbook.time}</Text> <Text style={{color:'black', fontSize: 15}}>Time : {item.time}</Text>
</View> </View>
</View> </View>
)).reverse()}
</ScrollView> </ScrollView>
</SafeAreaView> </SafeAreaView>
</ImageBackground> </ImageBackground>
......
This diff is collapsed.
/* eslint-disable prettier/prettier */ /* eslint-disable prettier/prettier */
/*This is an Example of Calling Other Class Function in React Native*/
import { Component } from 'react'; import { Component } from 'react';
import moment from 'moment'; import moment from 'moment';
import AsyncStorage from '@react-native-async-storage/async-storage'; import AsyncStorage from '@react-native-async-storage/async-storage';
const date = moment().utcOffset('+08:00').format('D MMM'); const date = moment().utcOffset('+08:00').format('D MMM');
const time = moment().utcOffset('+08:00').format('h:mm a'); const time = moment().utcOffset('+08:00').format('h:mm a');
const day = moment().utcOffset('+08:00').format('ddd'); const day = moment().utcOffset('+08:00').format('ddd');
var records = []; //var records = [];
class OtherClass extends Component { class OtherClass extends Component {
saveRecord = async (correct, wrong, stime) => { saveRecord = async (correct, wrong, stime) => {
...@@ -23,34 +22,15 @@ class OtherClass extends Component { ...@@ -23,34 +22,15 @@ class OtherClass extends Component {
const value = await AsyncStorage.getItem('@storage_key'); const value = await AsyncStorage.getItem('@storage_key');
if (value !== null) { if (value !== null) {
// We have data!! // We have data!!
console.log(JSON.parse(value)); // console.log(JSON.parse(value));
records.push(JSON.parse(value)); this.setState({ logbook: JSON.parse(value) });
records.push(JSON.parse(obj)); console.log(this.state.logbook.length);
//console.log(records);
AsyncStorage.setItem('@storage_key', JSON.stringify(records), (err)=> {
if (err){
console.log('an error');
throw err;
}
// console.log('success');
}).catch((err)=> {
console.log('error is: ' + err);
});
}
else {
AsyncStorage.setItem('@storage_key', JSON.stringify(obj), (err)=> {
if (err){
console.log('an error');
throw err;
}
// console.log('success');
}).catch((err)=> {
console.log('error is: ' + err);
});
} }
} catch (error) { } catch (error) {
// Error retrieving data // Error retrieving data
} }
}; };
} }
......
...@@ -17,8 +17,9 @@ import Orientation from 'react-native-orientation'; ...@@ -17,8 +17,9 @@ import Orientation from 'react-native-orientation';
import Notes from './data'; import Notes from './data';
import * as RNFS from 'react-native-fs'; import * as RNFS from 'react-native-fs';
import NumericInput from 'react-native-numeric-input'; import NumericInput from 'react-native-numeric-input';
import AsyncStorage from '@react-native-async-storage/async-storage';
var path = RNFS.DocumentDirectoryPath + '/settings.txt'; //var path = RNFS.DocumentDirectoryPath + '/settings.txt';
var path1 = RNFS.DocumentDirectoryPath + '/counter.txt'; var path1 = RNFS.DocumentDirectoryPath + '/counter.txt';
...@@ -76,31 +77,56 @@ export default class Settings extends React.Component { ...@@ -76,31 +77,56 @@ export default class Settings extends React.Component {
list.push(keyword); list.push(keyword);
} }
this.setState({listN: list}); this.setState({listN: list});
if (this.state.listN !== null){
this.setState({listN: list});
this.saveData(this.state.listN); this.saveData(this.state.listN);
this.displayData(); }
else {
this.setState({listN: notes});
this.saveData2(this.state.listN);
}
this._retrieveData();
// console.log(notes); // console.log(notes);
}; };
saveData = async () => { saveData = async () => {
RNFS.writeFile(path,JSON.stringify(this.state.listN),'utf8',) AsyncStorage.setItem('@notes_key', JSON.stringify(this.state.listN), (err)=> {
.then((success) => { if (err){
console.log(JSON.stringify(this.state.listN)); console.log('an error');
}) throw err;
.catch((err) => { }
console.log(err.message); console.log('success'); //console.log(JSON.stringify(this.state.listN));
}).catch((err)=> {
console.log('error is: ' + err);
}); });
}; };
displayData = async () => { saveData = async () => {
RNFS.readFile(path, 'ascii') AsyncStorage.setItem('@notes_key', JSON.stringify(this.state.listN), (err)=> {
.then((res) => { if (err){
// console.log(JSON.stringify(res)); console.log('an error');
}) throw err;
.catch((err) => { }
console.log(err.message, err.code); console.log('success'); //console.log(JSON.stringify(this.state.listN));
}).catch((err)=> {
console.log('error is: ' + err);
}); });
}; };
_retrieveData = async () => {
try {
const value = await AsyncStorage.getItem('@notes_key');
if (value !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
};
saveData1 = async () => { saveData1 = async () => {
RNFS.writeFile(path1,JSON.stringify(counter),'utf8',) RNFS.writeFile(path1,JSON.stringify(counter),'utf8',)
.then((success) => { .then((success) => {
...@@ -127,13 +153,6 @@ export default class Settings extends React.Component { ...@@ -127,13 +153,6 @@ export default class Settings extends React.Component {
Orientation.lockToPortrait(); Orientation.lockToPortrait();
// console.log(notes.length); // console.log(notes.length);
counter = 0; counter = 0;
RNFS.unlink(path).then(res => {
console.log('delete');
RNFS.scanFile(path);
})
.catch(err => {
console.log(err.message, err.code);
});
RNFS.unlink(path1).then(res => { RNFS.unlink(path1).then(res => {
console.log('delete'); console.log('delete');
RNFS.scanFile(path1); RNFS.scanFile(path1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment