-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
415 lines (393 loc) · 12.6 KB
/
App.js
File metadata and controls
415 lines (393 loc) · 12.6 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* eslint-disable react-native/no-inline-styles */
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState} from 'react';
import type {Node} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
useColorScheme,
View,
ActivityIndicator,
FlatList,
Text,
Image,
TouchableOpacity,
Modal,
Pressable,
StyleSheet,
TextInput,
} from 'react-native';
import {
GoogleSignin,
GoogleSigninButton,
NativeModuleError,
statusCodes,
} from '@react-native-google-signin/google-signin';
import {Colors, Header} from 'react-native/Libraries/NewAppScreen';
import {SelectList} from 'react-native-dropdown-select-list';
const App: () => Node = () => {
const isDarkMode = useColorScheme() === 'dark';
const [modalVisible, setModalVisible] = useState(false);
const [title, onChangeTitle] = React.useState();
const [description, onChangeDesc] = React.useState();
const [privacy, onChangePrivacy] = React.useState('public');
const privacyList = [
{key: '1', value: 'public'},
{key: '2', value: 'unlisted'},
{key: '3', value: 'private'},
];
const webClientId =
'GCM WEB CLIENT ID';
const iosClientId =
'IOS CLIENT ID';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
const KEY_API = 'APP API KEY';
const [authToken, setAuthToken] = useState();
const [isLoading, setLoading] = useState(true);
const [liveBroadcast, setLiveBroadcast] = useState([]);
GoogleSignin.configure({
scopes: [
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.readonly',
], // what API you want to access on behalf of the user, default is email and profile
webClientId: webClientId,
offlineAccess: true, // if you want to access Google API on behalf of the user FROM YOUR SERVER
hostedDomain: '', // specifies a hosted domain restriction
forceCodeForRefreshToken: true, // [Android] related to `serverAuthCode`, read the docs link below *.
accountName: '', // [Android] specifies an account name on the device that should be used
iosClientId: iosClientId, // [iOS] if you want to specify the client ID of type iOS (otherwise, it is taken from GoogleService-Info.plist)
googleServicePlistPath: '', // [iOS] if you renamed your GoogleService-Info file, new name here, e.g. GoogleService-Info-Staging
openIdRealm: '', // [iOS] The OpenID2 realm of the home web server. This allows Google to include the user's OpenID Identifier in the OpenID Connect ID token.
profileImageSize: 120, // [iOS] The desired height (and width) of the profile image. Defaults to 120px
});
const _signIn = async () => {
try {
//ML 1: Google OAuth 2
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
console.log('User Info ::', userInfo);
const {accessToken} = await GoogleSignin.getTokens();
console.log('accessToken ::', accessToken);
setAuthToken(accessToken);
//ML 2: Live Broadcast Schedules List Request
getLiveBroadcastSchedules(accessToken);
//ML 3: - [ ] Create a newly scheduled event for YouTube streaming
} catch (error) {
console.log('ERROR ::', error);
}
};
const getLiveBroadcastSchedules = async accessToken => {
try {
const response = await fetch(
'https://youtube.googleapis.com/youtube/v3/liveBroadcasts?part=snippet,contentDetails,status&mine=true&broadcastType=all&key=' +
KEY_API,
{
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
},
);
const json = await response.json();
console.log('JSON DATA', JSON.stringify(json));
setLiveBroadcast(json.items);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
const posteBroadcastSchedules = async () => {
try {
const response = await fetch(
'https://youtube.googleapis.com/youtube/v3/liveBroadcasts?part=snippet,contentDetails,status&mine=true&broadcastType=all&key=' +
KEY_API,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
},
},
);
const json = await response.json();
console.log('JSON DATA', JSON.stringify(json));
setLiveBroadcast(json.items);
setModalVisible(!modalVisible)
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundStyle.backgroundColor}
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<Header />
<View
style={{
backgroundColor: isDarkMode ? Colors.black : Colors.white,
}}>
<View
style={{
justifyContent: 'space-between',
flexDirection: 'row',
padding: 15,
}}>
<GoogleSigninButton
style={{width: 192, height: 48, alignSelf: 'center'}}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Dark}
onPress={_signIn}
/>
{!authToken && (
<TouchableOpacity
style={{
flexDirection: 'row',
alignItems: 'center',
backgroundColor: '#485a96',
borderWidth: 0.5,
borderColor: '#fff',
height: 40,
width: 180,
borderRadius: 4,
margin: 5,
}}
onPress={() => setModalVisible(true)}
activeOpacity={0.5}>
<Image
source={require('./assets/ic_youtube.png')}
style={{
margin: 5,
height: 25,
width: 25,
}}
/>
<View style={{backgroundColor: '#fff', width: 1, height: 40}} />
<Text
style={{
alignSelf: 'center',
color: '#fff',
marginLeft: 10,
fontWeight: '600',
}}>
Create Streams
</Text>
</TouchableOpacity>
)}
</View>
{isLoading && authToken?.length > 0 ? (
<ActivityIndicator />
) : (
<View style={{flex: 1, marginTop: 20}}>
{liveBroadcast?.length > 0 && (
<View style={{flexDirection: 'row', justifyContent: 'center'}}>
<Image
style={{
width: 30,
height: 30,
marginRight: 10,
}}
source={require('./assets/ic_youtube.png')}
/>
<Text
style={{
fontSize: 18,
fontWeight: '800',
alignSelf: 'center',
}}>
Upcoming Youtube streams
</Text>
</View>
)}
<FlatList
style={{marginTop: 30}}
data={liveBroadcast}
ListEmptyComponent={() => {
return authToken?.length > 0 &&
liveBroadcast?.length === 0 ? (
<View
style={{
backgroundColor: 'red',
justifyContent: 'center',
margin: 20,
height: 150,
}}>
<Text
style={{
fontFamily: 'Cochin',
fontWeight: '500',
alignSelf: 'center',
color: 'white',
fontSize: 22,
}}
numberOfLines={2}>
{'The user is not enabled for live streaming'}
</Text>
</View>
) : null;
}}
keyExtractor={({id}, index) => id}
renderItem={({item}) => (
<View
style={{
paddingVertical: 5,
marginHorizontal: 10,
flexDirection: 'row',
justifyContent: 'flex-start',
}}>
<Image
style={{
width: 50,
height: 50,
marginRight: 10,
borderWidth: 1,
borderColor: 'red',
}}
source={{uri: item?.snippet?.thumbnails?.default?.url}}
/>
<Text
style={{
fontFamily: 'Cochin',
fontWeight: '500',
alignSelf: 'center',
}}
numberOfLines={2}>
{item?.snippet?.title}
</Text>
</View>
)}
/>
</View>
)}
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<View style={{flexDirection: 'row', justifyContent: 'center'}}>
<Image
style={{
width: 30,
height: 30,
marginRight: 10,
}}
source={require('./assets/ic_youtube.png')}
/>
<Text
style={{
fontSize: 18,
fontWeight: '800',
alignSelf: 'center',
}}>
Create new stream
</Text>
</View>
<TextInput
style={styles.input}
onChangeText={text => onChangeTitle(text)}
value={title}
placeholder="Enter Title"
/>
<TextInput
style={styles.input}
onChangeText={text => onChangeTitle(text)}
value={title}
placeholder="Enter Description"
/>
<SelectList
setSelected={val => onChangePrivacy(val)}
data={privacyList}
save="value"
boxStyles={{
borderRadius: 90,
height: 45,
width: 280,
marginVertical: 10,
}}
dropdownStyles={{
height: 120,
}}
/>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={() => posteBroadcastSchedules}>
<Text style={styles.textStyle}>Create</Text>
</Pressable>
</View>
</View>
</Modal>
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
centeredView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
marginTop: 22,
},
modalView: {
margin: 20,
backgroundColor: 'white',
borderRadius: 20,
padding: 35,
alignItems: 'center',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
button: {
borderRadius: 20,
padding: 10,
elevation: 2,
marginTop: 10,
width: 150,
},
buttonClose: {
backgroundColor: '#2196F3',
},
textStyle: {
color: 'white',
fontWeight: 'bold',
textAlign: 'center',
},
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
borderRadius: 20,
width: 280,
},
});
export default App;