-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
229 lines (194 loc) · 5.86 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
import React, { useState, useEffect, useCallback, useRef } from "react";
import { Provider, useDispatch, useSelector } from "react-redux";
import { store, fetchUserInfo, fetchUserAudios } from "./src/redux";
import {
Dimensions,
StyleSheet,
View,
Text,
TouchableOpacity,
ScrollView,
Keyboard
} from "react-native";
import { useFonts } from "expo-font";
import { StatusBar } from "expo-status-bar";
import AsyncStorage from "@react-native-async-storage/async-storage";
import FlipperAsyncStorage from "rn-flipper-async-storage-advanced";
import MainFlow from "./src/screens/";
import { NavigationContainer } from "@react-navigation/native";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import Alert from "./src/components/alert";
import config from "./config";
import io from "socket.io-client";
import { togglePlayer, toggleDrawer } from "./src/redux";
import Drawer from "./src/components/drawer";
const { height: SCREEN_HEIGHT } = Dimensions.get("window");
import Overlay from "./src/components/drawer/overlay";
import Notification from "./src/components/notification";
const App = () => {
const [showView, setShowView] = useState(false);
const [message, setMessage] = useState("");
const dispatch = useDispatch();
const storedUserInfo = useSelector((state) => state.user.userInfo);
const app = useSelector((state) => state.app);
const ref = useRef();
const fetchUserDetails = async () => {
const userIdFromStorage = await AsyncStorage.getItem("userId");
if (userIdFromStorage) {
dispatch(fetchUserInfo({ userId: userIdFromStorage }));
dispatch(fetchUserAudios({ userId: userIdFromStorage }));
}
};
const socketConnection = async (socket) => {
const userIdFromStorage = await AsyncStorage.getItem("userId");
socket.on("connect", () => {
socket.emit("setUserId", userIdFromStorage);
});
socket.on("connect_error", (error) => {
console.error("Socket Connection Error:", error);
});
socket.on("notification", (message) => {
setShowView(true);
setMessage(message.message);
setTimeout(() => {
setShowView(false);
}, 5000); // Hide the view after 5 seconds
});
};
// useEffect(() => {
// fetchUserDetails();
// const socket = io(config.apiURL);
// socketConnection(socket);
// return () => socket.disconnect();
// }, []);
useEffect(() => {
fetchUserDetails();
const socket = io(config.apiURL);
socketConnection(socket);
return () => socket.disconnect();
}, [storedUserInfo._id]);
useEffect(() => {
if (app.drawerOpen && ref && ref.current) {
const isActive = ref.current.isActive();
if (isActive) {
ref.current.scrollTo(0);
} else {
ref.current.scrollTo(-SCREEN_HEIGHT / 2);
}
let destination;
switch (app.drawerHeight) {
case "halfScreen":
destination = -SCREEN_HEIGHT / 2;
break;
case "fullScreen":
destination = -SCREEN_HEIGHT + 50;
break;
case "expanded":
destination = -SCREEN_HEIGHT / 1.222;
break;
default:
if (typeof app.drawerHeight === "number") {
destination = -app.drawerHeight;
}
break;
}
ref.current.scrollTo(destination);
} else {
if(ref && ref.current){
ref.current.scrollTo(0);
}
}
}, [app.drawerOpen]);
useEffect(() => {
// Set up keyboard show listener
const keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
() => {
console.log('Keyboard is shown');
if(ref && ref.current){
ref.current.scrollTo(-SCREEN_HEIGHT / 1.6, true);
}
// Your action here
}
);
// Clean up listeners when component unmounts
return () => {
keyboardDidShowListener.remove();
};
}, []);
const close = useCallback(() => {
dispatch(
toggleDrawer({ drawerOpen: false, drawerType: null, drawerData: null })
);
const isActive = ref.current.isActive();
if (isActive && app.drawerOpen && ref && ref.current) {
ref.current.scrollTo(0);
} else {
ref.current.scrollTo(-200);
}
}, [app.drawerOpen]);
return (
<NavigationContainer
linking={{
prefixes: ["pulse://"],
config: {
screens: {
Settings: "callback",
},
},
}}
screenOptions={{
cardStyle: { backgroundColor: "black" },
}}
>
{showView && <Alert message={message} callback={setShowView} />}
<GestureHandlerRootView style={{ flex: 1 }}>
<StatusBar
style="light"
barStyle="dark-content"
position="absolute"
top={0}
left={0}
right={0}
/>
{app.drawerOpen && (
<TouchableOpacity
style={{
flex: 1,
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
zIndex: 10,
}}
onPress={close}
/>
)}
{app.drawerOpen && <Overlay/> }
<Drawer ref={ref} />
<Notification />
<MainFlow />
</GestureHandlerRootView>
</NavigationContainer>
);
};
export default MainApp = () => {
const [fontsLoaded] = useFonts({
"aeonik-regular": require("./assets/fonts/Aeonik-Regular.ttf"),
"aeonik-medium": require("./assets/fonts/Aeonik-Medium.ttf"),
"aeonik-light": require("./assets/fonts/Aeonik-Light.ttf"),
london: require("./assets/fonts/London-Regular.ttf"),
});
if (!fontsLoaded) {
return <View style={{ flex: 1, backgroundColor: "black" }} />;
}
return (
<View style={{ flex: 1, backgroundColor: "black" }}>
<Provider store={store}>
{__DEV__ && <FlipperAsyncStorage />}
<App />
</Provider>
</View>
);
};