-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
264 lines (247 loc) · 7.89 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import React, {useEffect, useState} from 'react';
import {
UNIVERSAL_FEED,
POST_DETAIL,
CREATE_POST,
CAROUSEL_SCREEN,
POST_LIKES_LIST,
LMOverlayProvider,
CarouselScreen,
LMFeedPollResult,
NOTIFICATION_FEED,
getNotification,
getRoute,
LMFeedCallbacks,
LMCarouselScreenCallbacks,
NAVIGATED_FROM_NOTIFICATION,
initMyClient,
LMSocialFeedPostDetailScreen,
LMCreatePollScreen,
LMLikesScreen,
LMNotificationScreen,
} from '@likeminds.community/feed-rn-core';
import LMSocialFeedCreateScreen from '@likeminds.community/feed-rn-core/wrappers/socialFeedCreateWrapper';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {
KeyboardAvoidingView,
Linking,
PermissionsAndroid,
Platform,
View,
} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {navigationRef} from './RootNavigation';
import FeedWrapper from './Wrappers/feedWrapper';
import messaging from '@react-native-firebase/messaging';
import notifee, {EventType} from '@notifee/react-native';
import {Credentials} from './login/credentials';
import {
CREATE_POLL_SCREEN,
POLL_RESULT,
} from '@likeminds.community/feed-rn-core/constants/screenNames';
import {pollStyle, postListStyles} from './styles';
import {GestureHandlerRootView} from 'react-native-gesture-handler';
import STYLES from '@likeminds.community/feed-rn-core/constants/Styles';
class CustomCallbacks implements LMFeedCallbacks, LMCarouselScreenCallbacks {
onEventTriggered(eventName: string, eventProperties?: Map<string, string>) {
// Override onEventTriggered with custom logic
}
onBackPressOnCarouselScreen() {
// Override onBackPressOnCarouselScreen with custom logic
navigationRef.goBack();
}
}
const lmFeedInterface = new CustomCallbacks();
const App = () => {
const Stack = createNativeStackNavigator();
const [users, setUsers] = useState<any>({
apiKey: '224beee7-4667-40e1-8fa3-592afdc9d37b',
userUniqueID: 'Jai Neww',
userName: 'Jai Neww',
});
const [apiKey, setApiKey] = useState(
Credentials?.apiKey?.length > 0 ? Credentials?.apiKey : users?.apiKey,
);
const [userUniqueID, setUserUniqueID] = useState(
Credentials?.userUniqueId?.length > 0
? Credentials.userUniqueId
: users?.userUniqueID,
);
const [userName, setUserName] = useState(
Credentials?.username?.length > 0 ? Credentials?.username : users?.userName,
);
const [myClient, setMyClient] = useState();
const [isTrue, setIsTrue] = useState(true);
useEffect(() => {
setUserName(
Credentials?.username?.length > 0
? Credentials?.username
: users?.userName,
);
setUserUniqueID(
Credentials?.userUniqueId?.length > 0
? Credentials.userUniqueId
: users?.userUniqueID,
);
setApiKey(
Credentials?.apiKey?.length > 0 ? Credentials?.apiKey : users?.apiKey,
);
}, [users, isTrue]);
useEffect(() => {
if (apiKey) {
const res: any = initMyClient();
setMyClient(res);
}
}, [isTrue, apiKey]);
// custom style of new post button
const regex = /post_id=([^&]+)/;
if (Platform.OS === 'android') {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
}
// notification listener on foreground state
useEffect(() => {
const unsubscribe = messaging().onMessage(async remoteMessage => {
const val = await getNotification(remoteMessage);
return remoteMessage;
});
return unsubscribe;
}, []);
// notification display on foreground state
useEffect(() => {
return notifee.onForegroundEvent(({type, detail}) => {
let routes = getRoute(detail?.notification?.data?.route);
switch (type) {
case EventType.DISMISSED:
console.log('User dismissed notification', detail.notification);
break;
case EventType.PRESS:
if (!!navigationRef) {
navigationRef.navigate(routes.route, routes.params);
}
break;
}
});
});
// deeplink listener for foreground state
useEffect(() => {
Linking.addEventListener('url', ({url}) => {
const match = url.match(regex);
// Extract the postId from the matched result
const postId = match ? match[1] : null;
if (navigationRef) {
navigationRef.navigate(POST_DETAIL, [
postId,
NAVIGATED_FROM_NOTIFICATION,
]);
}
});
}, []);
// deeplink listener for kill state
useEffect(() => {
let isMounted = true;
const getUrlAsync = async () => {
const initialUrl = await Linking.getInitialURL();
if (isMounted) {
if (initialUrl) {
// Execute the regex pattern on the URL
const match = initialUrl?.match(regex);
// Extract the postId from the matched result
const postId = match ? match[1] : null;
setTimeout(() => {
if (navigationRef) {
navigationRef.navigate(POST_DETAIL, [
postId,
NAVIGATED_FROM_NOTIFICATION,
]);
}
}, 2000);
}
}
};
getUrlAsync();
// Cleanup function to unsubscribe when component is unmounted
return () => {
isMounted = false;
};
}, []);
useEffect(() => {
STYLES.setTheme({
hue: 18,
isDarkTheme: true,
primaryColor: '#d26232',
fontTypes: {
fontFamilyLight: 'Montserrat-Light',
fontFamilyMedium: 'Montserrat-Medium',
fontFamilySemiBold: 'Montserrat-SemiBold',
fontFamilyBold: 'Montserrat-Bold',
fontFamilyBlack: 'Montserrat-Black',
},
});
STYLES.setPollStyle(pollStyle);
STYLES.setPostListStyles(postListStyles);
});
return (
<View style={{flex: 1}}>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={{
flex: 1,
backgroundColor: STYLES.$IS_DARK_THEME
? STYLES.$BACKGROUND_COLORS.DARK
: STYLES.$BACKGROUND_COLORS.LIGHT,
}}>
{userName && userUniqueID && apiKey && myClient ? (
<GestureHandlerRootView style={{flex: 1}}>
<LMOverlayProvider
myClient={myClient}
apiKey={apiKey}
userName={userName}
userUniqueId={userUniqueID}
lmFeedInterface={lmFeedInterface}>
<NavigationContainer ref={navigationRef} independent={true}>
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name={UNIVERSAL_FEED} component={FeedWrapper} />
<Stack.Screen
name={POST_DETAIL}
component={LMSocialFeedPostDetailScreen}
/>
<Stack.Screen
name={CREATE_POST}
component={LMSocialFeedCreateScreen}
/>
<Stack.Screen
name={POST_LIKES_LIST}
component={LMLikesScreen}
/>
<Stack.Screen
name={NOTIFICATION_FEED}
component={LMNotificationScreen}
/>
<Stack.Screen
options={{gestureEnabled: false}}
name={CAROUSEL_SCREEN}
component={CarouselScreen}
/>
<Stack.Screen
name={POLL_RESULT}
component={LMFeedPollResult}
options={{
gestureEnabled: false,
}}
/>
<Stack.Screen
name={CREATE_POLL_SCREEN}
component={LMCreatePollScreen}
/>
</Stack.Navigator>
</NavigationContainer>
</LMOverlayProvider>
</GestureHandlerRootView>
) : null}
</KeyboardAvoidingView>
</View>
);
};
export default App;