Skip to content

Commit b2bac2f

Browse files
committed
Storing data
1 parent e9e9158 commit b2bac2f

File tree

8 files changed

+105
-9
lines changed

8 files changed

+105
-9
lines changed

Assets/mic.png

11 KB
Loading

Components/Homepage/EachFeaturesCard.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export const EachFeaturesCard = ({image,navigation,name,navigate}) => {
88
<TouchableOpacity onPress={()=>{
99
navigation.navigate(navigate)
1010
}} style={{
11-
height:200,
12-
width:190,
11+
height:160,
12+
width:200,
1313
backgroundColor:"black",
1414
margin:10,
1515
borderRadius:10,

Context/ContextState.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Context from "./Context";
22
import {useEffect, useState} from "react";
3+
import AsyncStorage from "@react-native-async-storage/async-storage";
34

45

56

@@ -35,10 +36,63 @@ const ContextState=(props)=>{
3536
}
3637
}, [darkMode]);
3738
const [History,setHistory] = useState([])
39+
async function SaveDarkMode(value){
40+
try {
41+
await AsyncStorage.setItem('dark', value);
42+
} catch (e) {
43+
// saving error
44+
}
45+
}
46+
const GetDarkMode = async () => {
47+
try {
48+
const jsonValue = await AsyncStorage.getItem('dark');
49+
if (jsonValue===null){
50+
setDarkmode(false)
51+
}else{
52+
if(jsonValue==='D'){
53+
setDarkmode(true)
54+
}else{
55+
setDarkmode(false)
56+
}
57+
58+
}
59+
} catch (e) {
60+
// error reading value
61+
}
62+
};
63+
async function SaveData(){
64+
try {
65+
const jsonValue = JSON.stringify(History);
66+
await AsyncStorage.setItem('history', jsonValue);
67+
} catch (e) {
68+
// saving error
69+
}
70+
}
71+
const getData = async () => {
72+
try {
73+
const jsonValue = await AsyncStorage.getItem('history');
74+
jsonValue != null ? JSON.parse(jsonValue) : null;
75+
if (jsonValue===null){
76+
setHistory([])
77+
}else{
78+
setHistory(JSON.parse(jsonValue))
79+
}
80+
} catch (e) {
81+
// error reading value
82+
}
83+
};
3884
useEffect(()=>{
39-
// console.log(History)
85+
if(History.length!==0){
86+
SaveData()
87+
}
88+
// const History1=History
89+
// setHistory(JSON.parse(History1))
4090
},[History])
41-
return <Context.Provider value={{Style1,darkMode,setDarkmode,History,setHistory}}>
91+
useEffect( () => {
92+
getData()
93+
GetDarkMode()
94+
}, []);
95+
return <Context.Provider value={{Style1,darkMode,setDarkmode,History,setHistory,SaveData,getData,SaveDarkMode,GetDarkMode}}>
4296
{props.children}
4397
</Context.Provider>
4498
}

Screens/ChatPage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {AiThinking} from "../Components/ChatPage/AiThinking";
1010
import Voice from '@react-native-community/voice';
1111

1212
export const ChatPage = ({navigation}) => {
13-
const {History,setHistory} = useContext(Context)
13+
const {History,setHistory,SaveData} = useContext(Context)
1414
const {Style1}=useContext(Context)
1515
const [scrollEnabled, setScrollEnabled] = useState(true)
1616
const Toast = useToast()
@@ -102,12 +102,14 @@ export const ChatPage = ({navigation}) => {
102102
Prev.push([])
103103
Prev[Prev.length-1]=[...chat]
104104
setHistory(Prev)
105+
SaveData()
105106
// console.log(Prev)
106107
}
107108
if(chat.length>2){
108109
const Prev=[...History]
109110
Prev[Prev.length-1]=[...chat]
110111
setHistory(Prev)
112+
SaveData()
111113
}
112114
}, [chat]);
113115
const onSpeechStartHandler = (e) => {

Screens/HistoryChatPage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {AiThinking} from "../Components/ChatPage/AiThinking";
1010
import Voice from '@react-native-community/voice';
1111

1212
export const HistoryChatPage = ({navigation,route}) => {
13-
const {History,setHistory} = useContext(Context)
13+
const {History,setHistory,SaveData} = useContext(Context)
1414
const {Style1}=useContext(Context)
1515
const [scrollEnabled, setScrollEnabled] = useState(true)
1616
const Toast = useToast()
@@ -111,6 +111,7 @@ export const HistoryChatPage = ({navigation,route}) => {
111111
const Prev=[...History]
112112
Prev[route.params.index]=[...chat]
113113
setHistory(Prev)
114+
SaveData()
114115
}, [chat]);
115116
const onSpeechStartHandler = (e) => {
116117
console.log("start handler==>>>", e)

Screens/HomePage.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {EachHistory} from "../Components/Homepage/EachHistory";
88
import ToggleSwitch from "toggle-switch-react-native";
99

1010
export const HomePage = ({navigation}) => {
11-
const {Style1,setDarkmode,darkMode,History}=useContext(Context)
11+
const {Style1,setDarkmode,darkMode,History,SaveDarkMode}=useContext(Context)
1212
return (
1313
<ImageBackground source={require("../Assets/artem-bryzgalov-r2CAjGQ0gSI-unsplash.jpg")} style={{flex:1,height:"100%",width:"100"}}>
1414
<View style={{
@@ -28,7 +28,14 @@ export const HomePage = ({navigation}) => {
2828
label={"Dark Mode"}
2929
labelStyle={{ color: "white", fontWeight: "500" }}
3030
size="medium"
31-
onToggle={isOn => setDarkmode(isOn)}
31+
onToggle={isOn => {
32+
if(isOn===false){
33+
SaveDarkMode('L')
34+
}else {
35+
SaveDarkMode("D")
36+
}
37+
setDarkmode(isOn)
38+
}}
3239
/>
3340
</View>
3441

@@ -48,7 +55,7 @@ export const HomePage = ({navigation}) => {
4855
<EachFeaturesCard navigation={navigation} image={require("../Assets/christopher-gower-m_HRfLhgABo-unsplash.jpg")} name={"Generate Code"} navigate={"CodePage"}/>
4956
</ScrollView>
5057
<Heading title={"Chat history"}/>
51-
{History.map((e,i)=>{
58+
{History!=undefined&&History?.map((e,i)=>{
5259
return <EachHistory text={e[1].message} key={i} data={e} navigation={navigation} index={i}/>
5360
})}
5461
</ScrollView>

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@fortawesome/free-regular-svg-icons": "^6.4.2",
1616
"@fortawesome/free-solid-svg-icons": "^6.4.2",
1717
"@fortawesome/react-native-fontawesome": "^0.3.0",
18+
"@react-native-async-storage/async-storage": "^1.19.3",
1819
"@react-native-community/voice": "^1.1.9",
1920
"@react-navigation/native": "^6.1.9",
2021
"@react-navigation/native-stack": "^6.9.16",

0 commit comments

Comments
 (0)