-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
462 lines (401 loc) · 18.6 KB
/
App.js
File metadata and controls
462 lines (401 loc) · 18.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import { registerRootComponent } from 'expo';
import React from 'react';
import { StyleSheet, Text, View, ScrollView , FlatList , Image, Button , Alert} from 'react-native';
import { Card, ListItem, Icon, BottomSheet } from 'react-native-elements';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux'
import { ScreenWidth, ScreenHeight } from 'react-native-elements/dist/helpers';
import { Avatar } from 'react-native-elements/dist/avatar/Avatar';
import axios from 'axios'
import * as FileSystem from 'expo-file-system';
import * as SQLite from 'expo-sqlite'
import business_database from './redux/Database_transactions.js'
import Gig_notifications from './websockets/Gig_notifications.js'
import * as Notifications from 'expo-notifications'
import notify from './websockets/Notifications.js'
import fun_database from './redux/Database_fun_transactions.js'
import OneSignal from 'react-native-onesignal';
export class App extends React.Component {
constructor(props){
super(props)
}
state = {
api_call : null,
notopen : true,
first_time : true,
};
async componentDidMount() {
await this.downloadAssets()
}
connect_phone_to_name(dict , number){
if (dict[number]){
return dict[number]
} else {
return 'unknown'
}
}
async api_calls(contacts,chats_contacts,token) {
await axios({
method : 'GET',
url : (this.props.state.Debug) ? ('http://192.168.43.232:8040/Scan_chats/') : ('https://multix-fun.herokuapp.com/Scan_chats/'),
data : {},
// timeout : 1000000,
headers : {
'content-type' : 'application/json',
'Authorization': 'Token ' + token ,
}
}).then((response) => {
if (response.status === 200){
// console.log(response.data)
this.setState({notopen : false})
let resp = response.data
let friends = []
for(let i = 0; i<contacts.length; i++){
for(let p = 0; p<resp.length; p++){
if(contacts[i] == resp[p].Contact){
// this.props.update_contacts(resp[p])
if(resp[p]){
friends.push(resp[p])
}
}
}
}
// console.log(friends)
this.props.store_fun_contacts_redux(friends)
this.props.store_online_chats({})
//setTimeout(()=>{
//await this.update_db_connes(resp , chats_contacts)
//},3000)
axios({
method : 'POST',
url : (this.props.state.Debug) ? ('http://192.168.43.232:8040/Update_friends') : ('https://multix-fun.herokuapp.com/Update_friends'),
data : {'friends' : friends},
// timeout : 100000,
headers : {
'content-type' : 'application/json',
'Authorization': 'Token ' + token ,
}
}).then(async(response_online)=>{
if (response_online.status == 202){
// let matched_data = fun_database.online_chats(response_online.data , response.data)
// this.props.store_online_chats(matched_data)
}
},()=>{
})
}
} , ()=>{
//tx.executeSql('SELECT * FROM Chats_contacts',[],(tx , Result_set)=>{
//if (this.state.first_time){
this.props.store_online_chats({})
this.props.store_fun_contacts_redux(chats_contacts)
//}
//},(error)=>{})
}
)
}
//async intermediate(contacts,chats_contacts,token){
//if (this.state.notopen){
//await this.api_calls(contacts,chats_contacts,token)
//setTimeout(async()=>{await this.intermediate(contacts,chats_contacts,token)},2000)
//}
//else {
//if (!this.props.fun.Refresh){
//setTimeout(async()=>{await this.intermediate(contacts,chats_contacts,token)},2000)
//} else {
//this.setState({notopen : true})
//setTimeout(async()=>{await this.intermediate(contacts,chats_contacts,token)},2000)
// }
//}
// }
async performAPICalls() {
const db = SQLite.openDatabase('Fun_database.db')
let chats_contacts = []
db.transaction((tx)=>{
tx.executeSql('SELECT * FROM Chats_contacts',[],(tx , Result_set_contacts)=>{
chats_contacts = Result_set_contacts.rows._array
this.setState({ chats_contacts : chats_contacts })
},(error)=>{console.log(error)})
tx.executeSql('SELECT * FROM Account WHERE id = 1',[],(tx,result)=>{
if (result.rows.length > 0){
fun_database.get_contacts_list().then(async (contacts)=>{
//console.log(contacts)
await this.api_calls(contacts,chats_contacts,result.rows.item(0)['Multix_token'])
//await this.intermediate(contacts,chats_contacts,result.rows.item(0)['Multix_token'])
//await this.api_calls(contacts)
})
}
})
},(error)=>{},()=>{})
}
// Loading messages and account data from the database
async Load_Fun_Account_data(){
const db = SQLite.openDatabase('Fun_database.db')
let phone_names = await fun_database.get_contacts_with_name()
db.transaction((tx)=>{
tx.executeSql('SELECT * FROM Account WHERE id = 1',
[],(tx , Result_set)=>{
//console.log(Result_set.rows.item(0))
if (Result_set.rows.length > 0){
this.props.store_fun_profile_redux(Result_set.rows.item(0))
let saved_contacts = []
tx.executeSql('SELECT * FROM Chats_contacts',[],(tx,result)=>{ saved_contacts = result.rows._array},(error)=>{console.log('error')})
tx.executeSql('SELECT * FROM Messages' , [] , (tx , Result)=>{
if (Result.rows.length > 0){
//Reversing the list got from the database to give us the order
let reversed_messages = (Result.rows._array).reverse()
// A dictionary to hold the index values during iteration for easy reference
let index_holder = {}
// A list to hold the messages
let messages = []
// A dummy value to hold the index value for the next free item to come
let current_free_index = 0
// A dummy list to hold the current positions of all chats that will be update later
let positions = []
//Sorting the queried messages from the database using the for loop
for(let i = 0; i<Result.rows.length; i++){
let item = reversed_messages[i]
//Checking if the index is recorded in the index_holder or not
if(index_holder[item.Server_id] === 0 || index_holder[item.Server_id]){
//If the index is already recorded in the indexholder list then jhus push the message in the list
messages[index_holder[item.Server_id]]['Messages'].push({
'id' : item.id ,
'Contact' : item.Contact ,
'Message' : item.Message ,
'Status' : item.Status ,
'Date' : item.Date ,
'Receiving' : item.Receiving,
'Server_id' : item.Server_id,
'forwarded' : item.forwarded,
'Starred' : item.Starred,
'Replied' : item.Replied,
'Type' : item.Type,
'Muk' : item.Muk,
'Seen' : item.Seen,
})
} else {
//If the index doesnot exist in the index holder then declare it as a dictionary and record its index in the index holder
index_holder[item.Server_id] = current_free_index
messages[current_free_index] = {}
// messages[current_free_index]['Name'] = item.Name
messages[current_free_index]['Server_id'] = item.Server_id
messages[current_free_index]['Contact_name'] = this.connect_phone_to_name(phone_names,item.Contact)
messages[current_free_index]['Name'] = item.Contact
//console.log(saved_contacts)
for(let i = 0; i<saved_contacts.length; i++){
if(saved_contacts[i].Server_id == item.Server_id){
messages[current_free_index]['Name'] = saved_contacts[i].Name
messages[current_free_index]['Contact_name'] = this.connect_phone_to_name(phone_names,saved_contacts[i].Contact)
} else {
}
}
messages[current_free_index]['Messages'] = []
messages[current_free_index]['Messages'].push({
'id' : item.id ,
'Contact' : item.Contact ,
'Message' : item.Message ,
'Status' : item.Status ,
'Date' : item.Date,
'Receiving' : item.Receiving,
'Server_id' : item.Server_id,
'forwarded' : item.forwarded,
'Starred' : item.Starred,
'Replied' : item.Replied,
'Type' : item.Type,
'Muk' : item.Muk,
'Seen' : item.Seen,
})
positions.push({
'Server_id' : item.Server_id,
'index' : current_free_index,
})
current_free_index ++
}
}
//console.log('navigating')
// Storing the sorted messages into the redux store for future reference
this.props.store_messages_redux(messages)
// Storing the positions of the different names
this.props.store_positions_redux(positions)
// Navigating to the main screen after loading the messages
this.props.navigation.navigate('Multix')
} else {
this.props.store_messages_redux([])
this.props.store_positions_redux([])
this.props.navigation.navigate('Multix')
}
} )
}else {
//console.log('Coming')
this.props.navigation.navigate('Terms And Conditions')
}
},(error) => {console.log(error)})
}, (error)=>{},()=>{})
}
/**
* Method that serves to load resources and make API calls
*/
prepareResources = async() => {
// Setting handlers for push notifications
const db = SQLite.openDatabase('Fun_database.db')
db.transaction((tx)=>{
tx.executeSql('SELECT * FROM Layout_Settings WHERE id = 1',[],async (tx,Result_colors)=>{
if (Result_colors.rows.length > 0){
//console.log(Result_colors.rows.item(0))
this.props.store_Layout_settings(Result_colors.rows.item(0))
} else {
let defaults = await fun_database.default_setup_layout()
this.props.store_Layout_settings(defaults)
}
},error => {console.log(error)})
},(error)=>{},()=>{})
//listening to changes of the push notifications from the server
let profile = business_database.business_data();
this.props.store_profile_redux(profile)
//OneSignal Init Code
OneSignal.setLogLevel(6, 0);
OneSignal.setAppId("d63de83f-34c6-4a3e-bd45-b13b1005a821");
//END OneSignal Init Code
//Prompt for push on iOS
OneSignal.promptForPushNotificationsWithUserResponse(response => {
console.log("Prompt response:", response);
});
//Method for handling notifications received while app in foreground
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
console.log("OneSignal: notification will show in foreground:", notificationReceivedEvent);
let notification = notificationReceivedEvent.getNotification();
const data = notification.additionalData
console.log("additionalData: ", data);
// Complete with null means don't show a notification.
notificationReceivedEvent.complete(notification);
});
//Method for handling notifications opened
OneSignal.setNotificationOpenedHandler(notification => {
console.log("OneSignal: notification opened:", notification);
});
// let notifications_status = notify.init()
// if (notifications_status){
// Notifications.setNotificationHandler({
// handleNotification: async () => ({
// shouldShowAlert: true,
// shouldPlaySound: true,
// shouldSetBadge: true,
// }),
// handleSuccess : async () => {
// const badge_number = await Notifications.getBadgeCountAsync()
// await Notifications.setBadgeCountAsync(badge_number + 1)
// },
// handleError : async () => {
// },
// });
// Notifications.addNotificationReceivedListener((notification)=>{
// });
// Notifications.addNotificationResponseReceivedListener((notification)=>{
// })
// //Notifications categories
// Notifications.setNotificationCategoryAsync(
// 'Multix Messages',
// )
// Notifications.setNotificationCategoryAsync('Multix Business')
// }
//getting data from the database
//storing the apps navigations in redux
this.props.action(this.props)
//console.log('Request failed')
await this.Load_Fun_Account_data()
};
/**
* Method that downloads all assets databases on startup in most cases for the first time
*/
async downloadAssets(){
const dir = await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite/')
const fun_dir = await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite/Business_database.db' )
let business_db = false
let fun_db = false
if (!dir.exists ){
await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'SQLite/' )
// Downloading the business database
await FileSystem.downloadAsync(this.props.state.Debug ? ('http://192.168.43.232:8080/Business_database.db') : ('https://mulapp.s3.eu-west-2.amazonaws.com/Multix+databases/Business_database.db'), FileSystem.documentDirectory + 'SQLite/Business_database.db').then((result) => {
business_db = true
}).catch((error) => {
alert( 'Please connect to the internet seems something is wrong with your connection')
})
// Downloading the fun database
await FileSystem.downloadAsync(this.props.state.Debug ? ('http://192.168.43.232:8080/Fun_database.db') : ('https://mulapp.s3.eu-west-2.amazonaws.com/Multix+databases/Fun_database.db'), FileSystem.documentDirectory + 'SQLite/Fun_database.db').then((result) => {
fun_db = true
}).catch((error) => {
//alert( 'Please connect to the internet seems something is wrong with your connection')
//this.props.navigation.goBack()
})
if (fun_db && business_db) {
await this.performAPICalls();
await this.prepareResources();
}
} else if (dir.exists && fun_dir.exists) {
//If dir exists meaning that the databases already exist t
//console.log('Folder exists . Proceeding...')
await this.performAPICalls();
await this.prepareResources();
} else if (dir.exists && !fun_dir.exists){
// Downloading the business database
await FileSystem.downloadAsync(this.props.state.Debug ? ('http://192.168.43.232:8080/Business_database.db') : ('https://mulapp.s3.eu-west-2.amazonaws.com/Multix+databases/Business_database.db'), FileSystem.documentDirectory + 'SQLite/Business_database.db').then((result) => {
business_db = true
}).catch((error) => {
alert( 'Please connect to the internet seems something is wrong with your connection')
})
// Downloading the fun database
await FileSystem.downloadAsync(this.props.state.Debug ? ('http://192.168.43.232:8080/Fun_database.db') : ('https://mulapp.s3.eu-west-2.amazonaws.com/Multix+databases/Fun_database.db'), FileSystem.documentDirectory + 'SQLite/Fun_database.db').then((result) => {
fun_db = true
}).catch((error) => {
//alert( 'Please connect to the internet seems something is wrong with your connection')
//this.props.navigation.goBack()
})
if (fun_db && business_db) {
await this.performAPICalls();
await this.prepareResources();
}
}
//console.log("done preparing local databases ....")
}
render() {
if (this.props.fun.app_started && this.props.fun.Fun_profile){
this.props.navigation.navigate('Multix')
}
return (
<View style={styles.container}>
<Avatar rounded size = {'xlarge'} source = {require('./assets/Notifications.png')} />
</View>
)
}
}
// Put any code you need to prepare your app in these functions
let mapStateToProps = (state_redux) => {
let state = state_redux.business
let fun = state_redux.fun
return {state,fun}
}
let mapDispatchToProps = (dispatch) => ({
action : (param) => {dispatch({type : 'Navigate' , navigation : param})},
store_profile_redux : (Profile) => dispatch({type : 'update_business_profile' , value : Profile }),
start_gig_notifications : (Name) => dispatch({type : 'create_business_websocket_instances' , Name : Name}),
store_fun_profile_redux : (profile) => dispatch({ type : 'store_fun_account_info' , Profile : profile }),
store_messages_redux : (Messages) => dispatch({ type : 'Load_messages' , Messages : Messages }),
store_positions_redux : (Positions) => dispatch({type : 'chats_positions' , list : Positions}),
store_fun_contacts_redux : (contacts) => dispatch({ type : 'store_active_contacts' , Contacts : contacts }),
store_online_chats : (online_chats) => dispatch({ type : 'online_chats' , Online_chats : online_chats }),
store_Layout_settings : (content) => dispatch({type : 'Layout_Settings' , content : content}),
update_contacts : (chat_data) => dispatch({type : 'update_active_contacts' , chat_data : chat_data}),
})
export default connect(mapStateToProps,mapDispatchToProps)(App)
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'black',
},
text: {
color: 'white',
fontWeight: 'bold',
top : 20
},
});