-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.tsx
179 lines (163 loc) · 4.81 KB
/
App.tsx
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, { useEffect, useState } from "react";
import {
Platform,
UIManager,
StyleSheet,
LayoutAnimation,
View,
LogBox,
} from "react-native";
import SplashScreen from "react-native-splash-screen";
import TabSwitcher from "./components/TabSwitcher";
import Visualizer from "./components/Visualizer";
import SpeakButton from "./components/SpeakButton";
import Greeting from "./components/Greeting";
import Chats from "./components/Chats";
import Tabs from "./components/Tabs";
import Modal from "./components/Modal";
import { ChatType, TabsType } from "./types";
import { GlobalContext } from "./context/globalContext";
import { initStore } from "./stores/staticStore";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { sleep } from "./engine/utils";
LogBox.ignoreLogs(["new NativeEventEmitter"]);
if (Platform.OS === "android") {
if (UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
}
const App = () => {
const [listening, setListening] = useState(false);
const [activeTab, setActiveTab] = useState<TabsType>("home");
const [partialSpeechResults, setPartialSpeechResults] = useState("");
const [chats, setAllChats] = useState<ChatType[]>([]);
const [modalData, setModalData] = useState(null);
function addChat(chat: ChatType) {
console.log("Adding chat");
console.log(chat);
setAllChats((arr) => [...arr, chat]);
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
if (chat.extraData?.modalChild) {
console.log("chat.extraData");
console.log(chat);
setModalData(chat.extraData);
}
}
function _setPartialSpeechResults(text: string) {
setPartialSpeechResults(text);
}
function setIsListening(state: boolean) {
console.log(`listening: ${state}`);
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
if (activeTab === "home") {
setListening(state);
} else {
setActiveTab("home");
}
}
async function onboard() {
addChat({ text: "Hello 👋", variant: "basic text", extraData: {} });
await sleep(1500);
addChat({
text: "Am Gabby, a voice assistant",
variant: "basic text",
extraData: {},
});
await sleep(2000);
addChat({
text: "To see what I can do, click on Commands up there ☝️",
variant: "basic text",
extraData: {},
});
await sleep(2800);
addChat({
text: "If you are confused on the layout of the app, click on Tutorial also up there ☝️ for a walk through",
variant: "basic text",
extraData: {},
});
await sleep(3000);
addChat({
text: "Also, you might want to lower your volume as I reply by voice 🎤",
variant: "basic text",
extraData: {},
});
}
useEffect(() => {
async function intialize() {
await initStore();
SplashScreen.hide();
AsyncStorage.getItem("firstLaunch").then((value) => {
if (!value) {
onboard();
AsyncStorage.setItem("firstLaunch", "yes");
} else {
addChat({
text: "How can I help",
variant: "basic text",
extraData: {},
});
}
});
}
intialize();
}, []);
return (
<View style={styles.container}>
<GlobalContext.Provider
value={{ chats, addChat, isListening: listening, setIsListening }}>
{modalData && <Modal setModalData={setModalData} data={modalData} />}
<TabSwitcher
inListenMode={listening}
activeTab={activeTab}
switchTab={(tab: any) => {
LayoutAnimation.configureNext(
LayoutAnimation.Presets.easeInEaseOut
);
setActiveTab(tab);
}}
/>
<Visualizer isListening={listening} />
<View
style={{
flex: 1,
marginTop: -20,
width: "100%",
}}>
{activeTab == "home" ? (
<>
<Greeting isListening={listening} />
<Chats partialSpeechResults={partialSpeechResults} />
</>
) : (
<Tabs activeTab={activeTab} />
)}
</View>
<SpeakButton
isListening={listening}
activeTab={activeTab}
toggleListenMode={(state: boolean) => setIsListening(state)}
_setPartialSpeechResults={(text: string) =>
_setPartialSpeechResults(text)
}
/>
</GlobalContext.Provider>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "black",
alignItems: "center",
justifyContent: "center",
position: "relative",
},
});
export default App;