Skip to content

Commit dbbdac9

Browse files
committed
History
1 parent 34195c0 commit dbbdac9

File tree

8 files changed

+145
-16
lines changed

8 files changed

+145
-16
lines changed

Assets/listning.gif

136 KB
Loading

Assets/mic.png

10.4 KB
Loading

Components/Homepage/EachHistory.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ import {Image, Text, View} from "react-native";
22
import {useContext} from "react";
33
import Context from "../../Context/Context";
44

5-
export const EachHistory = () => {
5+
export const EachHistory = ({text}) => {
66
const {Style1}=useContext(Context)
7+
function UpdatedText(text1){
8+
if(text1.length>60){
9+
return text1.slice(0,61)+" ..."
10+
}else{
11+
return text1
12+
}
13+
}
714
return (
815
<View style={{
916
paddingHorizontal:5,
@@ -16,19 +23,20 @@ export const EachHistory = () => {
1623
backgroundColor:Style1.color5,
1724
padding:10,
1825
borderRadius:10,
19-
paddingVertical:15
26+
paddingVertical:15,
27+
2028
}}>
2129
<Image source={require("../../Assets/points.png")} style={{
2230
height:20,
2331
width:20
2432
}}/>
2533
<Text style={{
2634
color:Style1.color1,
27-
paddingRight:30,
2835
marginLeft:10,
36+
paddingRight:10,
2937
fontSize:17,
3038
fontWeight:"400"
31-
}}>How to develop a law firm website in a way that convey trust and authority</Text>
39+
}}>{UpdatedText(text)}</Text>
3240
</View>
3341
</View>
3442
)

Context/ContextState.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ const ContextState=(props)=>{
3434
})
3535
}
3636
}, [darkMode]);
37-
return <Context.Provider value={{Style1,darkMode,setDarkmode}}>
37+
const [History,setHistory] = useState([])
38+
useEffect(()=>{
39+
console.log(History)
40+
},[History])
41+
return <Context.Provider value={{Style1,darkMode,setDarkmode,History,setHistory}}>
3842
{props.children}
3943
</Context.Provider>
4044
}

Screens/ChatPage.js

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Image, ScrollView, Text, TextInput, TouchableOpacity, View} from "react-native";
1+
import {Image, PermissionsAndroid, ScrollView, Text, TextInput, TouchableOpacity, View} from "react-native";
22
import {useContext, useEffect, useRef, useState} from "react";
33
import Context from "../Context/Context";
44
import {Person} from "../Components/ChatPage/Person";
@@ -7,14 +7,15 @@ import {Ai} from "../Components/ChatPage/Ai";
77
import {useToast} from "react-native-toast-notifications";
88
import axios from "axios";
99
import {AiThinking} from "../Components/ChatPage/AiThinking";
10+
import Voice from '@react-native-community/voice';
1011

1112
export const ChatPage = ({navigation}) => {
13+
const {History,setHistory} = useContext(Context)
1214
const {Style1}=useContext(Context)
1315
const [scrollEnabled, setScrollEnabled] = useState(true)
1416
const Toast = useToast()
15-
const [menu,setMenu]=useState(false)
1617
const [loading,setloading]=useState(false)
17-
const {getData}=useContext(Context)
18+
const [VoiceRecording,setVoiceRecording]=useState(false)
1819
const [requestBody,setRequestBody]=useState([
1920
])
2021
const [value,setvalue]=useState("")
@@ -85,6 +86,72 @@ export const ChatPage = ({navigation}) => {
8586
})
8687
}
8788
},[requestBody])
89+
useEffect(() => {
90+
Voice.onSpeechStart = onSpeechStartHandler;
91+
Voice.onSpeechEnd = onSpeechEndHandler;
92+
Voice.onSpeechResults = onSpeechResultsHandler;
93+
94+
return () => {
95+
Voice.destroy().then(Voice.removeAllListeners);
96+
}
97+
}, [])
98+
//History
99+
useEffect(() => {
100+
if(chat.length===2){
101+
const Prev=[...History]
102+
Prev.push([])
103+
Prev[Prev.length-1]=[...chat]
104+
setHistory(Prev)
105+
// console.log(Prev)
106+
}
107+
if(chat.length>2){
108+
const Prev=[...History]
109+
Prev[Prev.length-1]=[...chat]
110+
setHistory(Prev)
111+
}
112+
}, [chat]);
113+
const onSpeechStartHandler = (e) => {
114+
console.log("start handler==>>>", e)
115+
}
116+
const onSpeechEndHandler = (e) => {
117+
setVoiceRecording(false)
118+
console.log("stop handler", e)
119+
}
120+
121+
const onSpeechResultsHandler = (e) => {
122+
let text = e.value[0]
123+
setvalue(text)
124+
console.log("speech result handler", e)
125+
}
126+
127+
const startRecording = async () => {
128+
setVoiceRecording(true)
129+
const audio=await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO)
130+
if(audio===false){
131+
Toast.show("No Audio Permission 😟",{
132+
type: "danger",
133+
placement: "top",
134+
duration: 3000,
135+
offset: 30,
136+
animationType: "zoom-in",
137+
})
138+
setVoiceRecording(false)
139+
}
140+
141+
try {
142+
await Voice.start('en-Us')
143+
} catch (error) {
144+
console.log("error raised", error)
145+
}
146+
}
147+
148+
const stopRecording = async () => {
149+
try {
150+
await Voice.stop()
151+
} catch (error) {
152+
console.log("error raised", error)
153+
}
154+
}
88155
return (
89156
<View style={{
90157
flex:1,
@@ -120,6 +187,31 @@ export const ChatPage = ({navigation}) => {
120187
alignItems:"center",
121188
justifyContent:"center"
122189
}}>
190+
<TouchableOpacity onPress={()=>{
191+
if(!VoiceRecording){
192+
console.log(Voice.isAvailable())
193+
startRecording()
194+
setVoiceRecording(true)
195+
}else {
196+
setVoiceRecording(false)
197+
stopRecording()
198+
}
199+
200+
}} style={{
201+
height:40,
202+
width:40,
203+
borderRadius:100000,
204+
overflow:"hidden",
205+
marginRight:5,
206+
justifyContent:"center",
207+
alignItems:"center"
208+
}}>
209+
<Image source={(VoiceRecording)?require("../Assets/listning.gif"):require("../Assets/mic.png")} style={{
210+
height:VoiceRecording?"100%":"70%",
211+
width:VoiceRecording?"100%":"70%",
212+
borderRadius:100000
213+
}}/>
214+
</TouchableOpacity>
123215
<TextInput autoFocus={true} value={value} onChangeText={(text)=>{
124216
setvalue(text)
125217
}} style={{
@@ -163,7 +255,6 @@ export const ChatPage = ({navigation}) => {
163255
}}/>
164256
</View>}
165257
</View>
166-
167258
</View>
168259
)
169260
}

Screens/HomePage.js

Lines changed: 20 additions & 7 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}=useContext(Context)
11+
const {Style1,setDarkmode,darkMode,History}=useContext(Context)
1212
return (
1313
<ImageBackground source={require("../Assets/artem-bryzgalov-r2CAjGQ0gSI-unsplash.jpg")} style={{flex:1,height:"100%",width:"100"}}>
1414
<View style={{
@@ -48,9 +48,9 @@ export const HomePage = ({navigation}) => {
4848
<EachFeaturesCard navigation={navigation} image={require("../Assets/christopher-gower-m_HRfLhgABo-unsplash.jpg")} name={"Generate Code"} navigate={"CodePage"}/>
4949
</ScrollView>
5050
<Heading title={"Chat history"}/>
51-
<EachHistory/>
52-
<EachHistory/>
53-
<EachHistory/>
51+
{History.map((e,i)=>{
52+
return <EachHistory text={e[1].message} key={i}/>
53+
})}
5454
</ScrollView>
5555
<TouchableOpacity onPress={()=>{
5656
navigation.navigate("ChatPage")
@@ -62,6 +62,20 @@ export const HomePage = ({navigation}) => {
6262
alignItems:"center",
6363
justifyContent:"center"
6464
}}>
65+
<View style={{
66+
height:40,
67+
width:40,
68+
borderRadius:100000,
69+
overflow:"hidden",
70+
marginRight:5,
71+
justifyContent:"center",
72+
alignItems:"center"
73+
}}>
74+
<Image source={require("../Assets/mic.png")} style={{
75+
height:"70%",
76+
width:"70%",
77+
borderRadius:100000
78+
}}/></View>
6579
<TouchableOpacity onPress={()=>{
6680
navigation.navigate("ChatPage")
6781
}} style={{
@@ -75,8 +89,7 @@ export const HomePage = ({navigation}) => {
7589
paddingLeft:14
7690
}}>Ask anything...</Text>
7791
</TouchableOpacity>
78-
79-
<TouchableOpacity style={{
92+
<View style={{
8093
height:"100%",
8194
alignItems:"center",
8295
justifyContent:"center"
@@ -86,7 +99,7 @@ export const HomePage = ({navigation}) => {
8699
width:30,
87100
objectFit:"contain"
88101
}}/>
89-
</TouchableOpacity>
102+
</View>
90103
</TouchableOpacity>
91104
</View>
92105
</View>

package-lock.json

Lines changed: 12 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-community/voice": "^1.1.9",
1819
"@react-navigation/native": "^6.1.9",
1920
"@react-navigation/native-stack": "^6.9.16",
2021
"axios": "^1.6.0",

0 commit comments

Comments
 (0)