-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.android.js
210 lines (177 loc) · 5.34 KB
/
index.android.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
BackAndroid,
Image,
StyleSheet,
Text,
TextInput,
TouchableHighlight,
View,
AsyncStorage,
Navigator,
ToolbarAndroid
} = React;
//var MD5 = require("crypto-js/md5");
var SplashScreen = require("./views/splashscreen");
var LoginScreen = require("./views/login");
var DrawerScreen = require('react-native-drawer');
var DrawerMenuScreen = require('./views/DrawerMenu');
var ListScreen = require('./views/ListScreen');
var DrawerModuleScreen = require('./views/DrawerScreen');
var LocalStorage = require("./utils/local_storage");
var localStorage = new LocalStorage();
var TimerMixin = require('react-timer-mixin');
var EventEmitter = require('RCTDeviceEventEmitter');
var Subscribable = require('Subscribable');
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var confModule = require('./config/Config');
var Config = new confModule();
var module = require('RCTRealtimeMessagingAndroid');
var RCTRealtimeMessaging = new module();
var moduleStorage = require('./controllers/StorageController');
var WebViewScreen = require('./views/WebView');
var _navigator;
BackAndroid.addEventListener('hardwareBackPress', () => {
if (_navigator && _navigator.getCurrentRoutes().length > 1) {
_navigator.pop();
return true;
}
return false;
});
var RouteMapper = function(route, navigationOperations, onComponentRef) {
_navigator = navigationOperations;
if (route.title === 'Realtime News') {
return (
<View style={{flex: 1}}>
<DrawerModuleScreen navigator={_navigator}/>
</View>
);
}
else if(route.title === 'webview'){
return(
<WebViewScreen url={route.url} body={route.body}/>
);
}
};
var RealtimeNews = React.createClass({
mixins: [TimerMixin, Subscribable.Mixin],
componentDidMount: function() {
this.setTimeout(
() => {
localStorage._getToken(this.updateState);
},
2000
);
this.addListenerOn(RCTDeviceEventEmitter, Config.STORAGE_NOTIFICATIONS, this.subscribeNotification);
RCTRealtimeMessaging.RTPushNotificationListener(this.onNotification);
this.addListenerOn(RCTDeviceEventEmitter, Config.LOGOUT, this.logout);
},
getInitialState() {
return {
isLogged: null,
username: "",
password: "",
selectedTab: 'recents'
};
},
onNotification: function(data)
{
var jsonData = JSON.stringify(data);
console.log("Received notification: " + jsonData);
var response = JSON.parse(jsonData).payload;
console.log("Response", response);
var url = response["URL"];
console.log("URL: " + url);
var body = response["Body"];
console.log("Body: " + body);
if(this.refs.nav){
EventEmitter.emit(Config.STORAGE_REFRESH, { itemUpdated: response });
this.refs.nav.push({ title: 'webview', url: url, body: body });
}
},
updateState: function(result){
if(result){
this.setState({isLogged: true});
}
else{
this.setState({isLogged: false});
}
},
togglePanel: function(){
if(this.refs.drawer._open){
this.refs.drawer.close();
}
else{
this.refs.drawer.open();
}
},
logout: function(){
this.setState({isLogged: false});
},
createDrawerMenu: function () {
return (<DrawerMenuScreen closeDrawer={() => {this.refs.drawer.close()}} logout={() => {localStorage._deleteToken(this.logout)}}/>);
},
subscribeNotification: function(){
console.log("susbcribeNotifications");
RCTRealtimeMessaging.RTConnect(
{
appKey:Config.APPKEY,
token:Config.TOKEN,
connectionMetadata:Config.CONNECTION_METADATA,
clusterUrl:Config.CLUSTER_URL,
projectId: Config.PROJECT_ID
});
RCTRealtimeMessaging.RTEventListener("onConnected",function(){
console.log('Connected to Realtime Messaging');
RCTRealtimeMessaging.RTSubscribeWithNotifications(Config.NOTIFICATIONS_CHANNEL, true);
});
RCTRealtimeMessaging.RTEventListener("onSubscribed", function(subscribedEvent){
console.log('Subscribed channel: ' + subscribedEvent.channel);
RCTRealtimeMessaging.RTDisconnect();
}),
RCTRealtimeMessaging.RTEventListener("onDisconnect", function(){
console.log('Disconnected from Realtime Messaging');
});
RCTRealtimeMessaging.RTEventListener("onException",function(exceptionEvent){
console.log("Exception:" + exceptionEvent.error);
});
},
render: function() {
if(this.state.isLogged === false){
return (
<LoginScreen login={() => {this.updateState(true)}}/>
);
}
else if(this.state.isLogged){
return(
<View style={styles.navigator}>
<ToolbarAndroid
onIconClicked={() => EventEmitter.emit('DrawerMenu_TogglePanel')}
navIcon={require('./ic_menu_black_24dp.png')}
title="RealtimeNews"
style={{backgroundColor: '#e9eaed',height: 56}} />
<Navigator
ref="nav"
initialRoute={{title: 'Realtime News'}}
renderScene={RouteMapper}
/>
</View>
);
}
return (
<SplashScreen />
);
}
});
var styles = StyleSheet.create({
navigator:{
flex:1
}
});
AppRegistry.registerComponent('RealtimeNews', () => RealtimeNews);