App.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. */
  5. import React, { Component } from 'react';
  6. import OneSignal from 'react-native-onesignal';
  7. import SplashScreen from 'react-native-splash-screen';
  8. import { WebView } from 'react-native-webview';
  9. import {
  10. Platform,
  11. StyleSheet,
  12. StyleProvider,
  13. Image,
  14. Linking,
  15. BackHandler,
  16. ActivityIndicator,
  17. Dimensions,
  18. StatusBar,
  19. } from 'react-native';
  20. import {
  21. View,
  22. Container, Content,
  23. Header, Title, Left, Body, Right, Item, Input,
  24. Footer, FooterTab, Badge,
  25. Text, Icon, Button,
  26. Drawer,
  27. } from 'native-base';
  28. type Props = {};
  29. const BASE_URL = 'https://una.io/';
  30. const TEMPLATE = 'protean';
  31. const TITLE = 'UNA.IO';
  32. const ONESIGNALAPPID = '';
  33. export default class App extends Component<Props> {
  34. loading = false;
  35. constructor(props) {
  36. super(props);
  37. this.state = {
  38. url: `${BASE_URL}?skin=${TEMPLATE}`,
  39. status: 'No Page Loaded',
  40. backButtonEnabled: false,
  41. forwardButtonEnabled: false,
  42. loading: this.loading,
  43. data: {},
  44. searchbar: false,
  45. key: 1,
  46. };
  47. this.onBack.bind(this);
  48. this.onMainMenu.bind(this);
  49. }
  50. onBack() {
  51. if (this.state.backButtonEnabled) {
  52. if ('android' === Platform.OS)
  53. this.onWebViewLoadStart(); // show loading screen
  54. // this.refs.webView.goBack();
  55. this.injectJavaScript("window.history.go(-1)");
  56. return true;
  57. }
  58. return false;
  59. }
  60. onMainMenu() {
  61. if (this.state.data.loggedin)
  62. this.injectJavaScript("bx_mobile_apps_show_main_menu()");
  63. else
  64. this.drawerOpen();
  65. }
  66. onBackAndMainMenu() {
  67. if (this.state.backButtonEnabled) {
  68. return this.onBack();
  69. }
  70. else {
  71. return this.onMainMenu();
  72. }
  73. }
  74. onProfileMenu() {
  75. this.injectJavaScript("bx_mobile_apps_show_profile_menu()");
  76. return true;
  77. }
  78. onAddMenu() {
  79. this.injectJavaScript("bx_mobile_apps_show_add_menu()");
  80. return true;
  81. }
  82. onNotificationsMenu() {
  83. this.injectJavaScript("bx_mobile_apps_show_notifications_menu()");
  84. return true;
  85. }
  86. onMessengerMenu() {
  87. this.injectJavaScript("bx_mobile_apps_show_messenger_menu()");
  88. return true;
  89. }
  90. onHomeMenu() {
  91. if (`${BASE_URL}?skin=${TEMPLATE}` == this.state.url || `${BASE_URL}` == this.state.url || `${BASE_URL}index.php` == this.state.url) {
  92. this.injectJavaScript("bx_mobile_apps_close_sliding_menus()");
  93. } else {
  94. this.setState ({
  95. url: `${BASE_URL}?skin=${TEMPLATE}`,
  96. searchbar: false,
  97. key: this.state.key + 1,
  98. });
  99. }
  100. return true;
  101. }
  102. onSearch(event) {
  103. this.onSearchCancelMenu();
  104. this.injectJavaScript(`window.location = '${BASE_URL}searchKeyword.php?keyword=${event.nativeEvent.text}';`);
  105. }
  106. onSearchCancelMenu() {
  107. this.setState ({
  108. loading: this.loading,
  109. searchbar: false,
  110. });
  111. return true;
  112. }
  113. onSearchMenu() {
  114. this.setState ({
  115. loading: this.loading,
  116. searchbar: true,
  117. });
  118. return true;
  119. }
  120. onDrawerLoginMenu() {
  121. this.onHomeMenu();
  122. this.drawerClose();
  123. return true;
  124. }
  125. onDrawerJoinMenu() {
  126. this.injectJavaScript(`window.location = '${BASE_URL}page/create-account';`);
  127. this.drawerClose();
  128. return true;
  129. }
  130. onDrawerForgotMenu() {
  131. this.injectJavaScript(`window.location = '${BASE_URL}page/forgot-password';`);
  132. this.drawerClose();
  133. return true;
  134. }
  135. onWebViewNavigationStateChange(navState) {
  136. if (-1 != navState.url.indexOf('#') && this.state.url == navState.url.substring(0, navState.url.indexOf('#'))) {
  137. this.onWebViewLoadEnd();
  138. return;
  139. }
  140. this.setState ({
  141. url: navState.url,
  142. status: navState.title,
  143. backButtonEnabled: navState.canGoBack,
  144. forwardButtonEnabled: navState.canGoForward,
  145. searchbar: false,
  146. });
  147. }
  148. onWebViewLoadStart (syntheticEvent) {
  149. if (-1 == this.state.url.indexOf(`${BASE_URL}`)) { // loading indicator is supported on original domains only
  150. this.onWebViewLoadEnd(syntheticEvent);
  151. return;
  152. }
  153. this.loading = true;
  154. this.setState ({
  155. loading: this.loading,
  156. searchbar: false,
  157. });
  158. }
  159. onWebViewLoadEnd (syntheticEvent) {
  160. this.loading = false;
  161. this.setState ({
  162. loading: this.loading,
  163. searchbar: false,
  164. });
  165. }
  166. reload() {
  167. this.refs.webView.reload();
  168. }
  169. postMessage (data) {
  170. // posts a message to web view
  171. this.refs.webView.postMessage(data);
  172. }
  173. injectJavaScript (script) {
  174. // executes JavaScript immediately in web view
  175. this.refs.webView.injectJavaScript(script);
  176. }
  177. onWebViewShouldStartLoadWithRequest (event) {
  178. var aExceptionsUrls = [
  179. 'redirect_uri=', 'redirect_uri%', 'redirectUri%', // Facebook/LinkedIn Connect and other Connect apps
  180. 'signin/oauth', 'signin%2Foauth', // Google Connect
  181. '/m/oauth2', // Dolphin/UNA Connect
  182. 'api.twitter.com', // Twitter Connect
  183. ];
  184. var bExceptionUrl = aExceptionsUrls.some(e => {
  185. return -1 !== event.url.indexOf(e);
  186. });
  187. if (0 != event.url.indexOf('http') || (!bExceptionUrl && -1 == event.url.indexOf(`${BASE_URL}`) && ('android' === Platform.OS || ('click' == event.navigationType && 'ios' === Platform.OS)))) {
  188. Linking.canOpenURL(event.url).then(supported => {
  189. if (supported) {
  190. Linking.openURL(event.url);
  191. }
  192. else {
  193. console.log('Don\'t know how to open URI: ' + event.url);
  194. }
  195. return false;
  196. });
  197. return false;
  198. }
  199. if ('android' === Platform.OS)
  200. this.onWebViewLoadStart(); // show loading screen
  201. return true;
  202. }
  203. javascriptToInject () {
  204. return ``
  205. }
  206. componentDidCatch(error, info) {
  207. SplashScreen.hide();
  208. }
  209. componentDidMount() {
  210. OneSignal.init(ONESIGNALAPPID, {kOSSettingsKeyAutoPrompt : true});
  211. OneSignal.inFocusDisplaying(0);
  212. OneSignal.addEventListener('received', this.onNotificationReceived);
  213. OneSignal.addEventListener('opened', this.onNotificationOpened.bind(this));
  214. if (Platform.OS === 'android') {
  215. this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
  216. this.onBack(); // works best when the goBack is async
  217. return true;
  218. });
  219. }
  220. SplashScreen.hide();
  221. }
  222. componentWillUnmount() {
  223. OneSignal.removeEventListener('received', this.onNotificationReceived);
  224. OneSignal.removeEventListener('opened', this.onNotificationOpened);
  225. if (Platform.OS === 'android') {
  226. this.backHandler.remove();
  227. }
  228. }
  229. onNotificationReceived(notification) {
  230. console.log("Notification received: ", notification);
  231. }
  232. onNotificationOpened(openResult) {
  233. if ('undefined' !== typeof(openResult.notification.payload.additionalData) && 'undefined' !== typeof(openResult.notification.payload.additionalData.url) && !openResult.notification.isAppInFocus) {
  234. this.injectJavaScript(`window.location = '${openResult.notification.payload.additionalData.url}';`);
  235. }
  236. }
  237. onWebViewMessage(event) {
  238. let oMsgData;
  239. try {
  240. oMsgData = JSON.parse(event.nativeEvent.data);
  241. }
  242. catch(err) {
  243. console.warn(err);
  244. return;
  245. }
  246. if ('undefined' !== typeof(oMsgData.loggedin)) {
  247. this.setState ({
  248. loading: this.loading,
  249. data: oMsgData,
  250. searchbar: false,
  251. key: 'undefined' === typeof(this.state.data.loggedin) || this.state.data.loggedin != oMsgData.loggedin ? this.state.key + 1 : this.state.key,
  252. });
  253. }
  254. if ('undefined' !== typeof(oMsgData['push_tags']) && oMsgData['push_tags'] !== false) {
  255. if ('undefined' !== typeof(oMsgData['push_tags']['email']) && oMsgData['push_tags']['email'].length) {
  256. OneSignal.setEmail(oMsgData['push_tags']['email'], oMsgData['push_tags']['email_hash'], (sError) => {
  257. console.warn("OneSignal set email error:" + sError);
  258. });
  259. delete oMsgData['push_tags']['email'];
  260. delete oMsgData['push_tags']['email_hash'];
  261. }
  262. OneSignal.sendTags(oMsgData['push_tags']);
  263. }
  264. if ('undefined' !== typeof(oMsgData['stop_loading']) && oMsgData['stop_loading']) {
  265. this.onWebViewLoadEnd (event);
  266. }
  267. }
  268. drawerClose () {
  269. this.refs.drawer._root.close()
  270. }
  271. drawerOpen () {
  272. this.refs.drawer._root.open()
  273. }
  274. render() {
  275. StatusBar.setBarStyle(Platform.OS === 'android' ? 'light-content' : 'dark-content', false);
  276. return (
  277. <Container>
  278. <Drawer ref="drawer" content={this.renderDrawer()} onClose={this.drawerClose.bind(this)}>
  279. {this.state.searchbar ? this.renderToolbarSearch() : this.renderToolbar() }
  280. <WebView
  281. useWebKit={true}
  282. ref="webView"
  283. key={this.state.key}
  284. geolocationEnabled={true}
  285. builtInZoomControls={false}
  286. decelerationRate="normal"
  287. cacheEnabled={true}
  288. allowsLinkPreview={true}
  289. hideKeyboardAccessoryView={true}
  290. injectedJavaScript={this.javascriptToInject()}
  291. onShouldStartLoadWithRequest={this.onWebViewShouldStartLoadWithRequest.bind(this)}
  292. onNavigationStateChange={this.onWebViewNavigationStateChange.bind(this)}
  293. style={styles.containerWebView}
  294. source={{uri: `${BASE_URL}?skin=${TEMPLATE}`}}
  295. userAgent={"UNAMobileApp/Mobile (" + Platform.OS + ")"}
  296. onMessage={this.onWebViewMessage.bind(this)}
  297. allowFileAccess={true}
  298. onLoad={this.onWebViewLoadEnd.bind(this)}
  299. onLoadStart={this.onWebViewLoadStart.bind(this)}
  300. />
  301. {this.state.data.loggedin && (
  302. <Footer>
  303. <FooterTab>
  304. <Button vertical onPress={this.onHomeMenu.bind(this)}>
  305. <Icon name="home" type="FontAwesome5" />
  306. </Button>
  307. </FooterTab>
  308. <FooterTab>
  309. <Button vertical onPress={this.onNotificationsMenu.bind(this)} badge={this.state.data.bubbles['notifications-notifications'] > 0 ? true : false}>
  310. {this.state.data.bubbles['notifications-notifications'] > 0 &&
  311. (<Badge><Text>{this.state.data.bubbles['notifications-notifications']}</Text></Badge>)
  312. }
  313. <Icon name="bell" type="FontAwesome5" solid />
  314. </Button>
  315. </FooterTab>
  316. <FooterTab>
  317. <Button vertical onPress={this.onAddMenu.bind(this)}>
  318. <Icon name="plus-circle" type="FontAwesome5" solid />
  319. </Button>
  320. </FooterTab>
  321. <FooterTab>
  322. <Button vertical onPress={this.onMessengerMenu.bind(this)} badge={this.state.data.bubbles['notifications-messenger'] > 0 ? true : false}>
  323. {this.state.data.bubbles['notifications-messenger'] > 0 &&
  324. (<Badge><Text>{this.state.data.bubbles['notifications-messenger']}</Text></Badge>)
  325. }
  326. <Icon name="comments" type="FontAwesome5" solid />
  327. </Button>
  328. </FooterTab>
  329. <FooterTab>
  330. <Button vertical onPress={this.onProfileMenu.bind(this)} badge={this.state.data.bubbles_num > 0 ? true : false}>
  331. {this.state.data.bubbles_num > 0 &&
  332. (<Badge><Text>{this.state.data.bubbles_num}</Text></Badge>)
  333. }
  334. <Icon name="user" type="FontAwesome5" solid />
  335. </Button>
  336. </FooterTab>
  337. </Footer>
  338. )}
  339. {this.state.loading && (
  340. <View style={styles.viewLoading2}>
  341. <ActivityIndicator size="large" color="#fff" />
  342. </View>
  343. )}
  344. </Drawer>
  345. </Container>
  346. );
  347. }
  348. renderToolbar () {
  349. return (
  350. <Header>
  351. <Left>
  352. <Button style={Platform.OS === 'android' ? styles.buttonLeftTopAndroid : styles.buttonLeftTopIos} transparent onPress={this.onBackAndMainMenu.bind(this)}>
  353. <Icon name={this.state.backButtonEnabled ? 'ios-arrow-back' : 'ios-menu'} />
  354. </Button>
  355. </Left>
  356. <Body>
  357. <Title>{TITLE}</Title>
  358. </Body>
  359. <Right>
  360. <Button transparent>
  361. <Icon name='search' onPress={this.onSearchMenu.bind(this)} />
  362. </Button>
  363. </Right>
  364. </Header>
  365. );
  366. }
  367. renderToolbarSearch () {
  368. return (
  369. <Header searchBar rounded>
  370. <Item>
  371. <Icon name="search" />
  372. <Input ref="inputSearch" placeholder="Search" onEndEditing={this.onSearchCancelMenu.bind(this)} onSubmitEditing={this.onSearch.bind(this)} />
  373. </Item>
  374. <Button transparent onPress={this.onSearchCancelMenu.bind(this)}>
  375. <Text>Cancel</Text>
  376. </Button>
  377. </Header>
  378. );
  379. }
  380. renderLoadingView () {
  381. return (
  382. <View style={styles.viewLoading}>
  383. <Image source={require('./img/logo-loading.png')} />
  384. <Text>Loading...</Text>
  385. </View>
  386. );
  387. }
  388. renderDrawer () {
  389. return (
  390. <Container>
  391. <Content>
  392. <View style={styles.drawerImageContainer}>
  393. <Image style={styles.drawerImage} source={require('./img/logo-loading.png')} />
  394. </View>
  395. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerLoginMenu.bind(this)}>
  396. <Icon style={styles.drawerButtonIcon} name='key' type="FontAwesome5" solid />
  397. <Text>Login</Text>
  398. </Button>
  399. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerJoinMenu.bind(this)}>
  400. <Icon style={styles.drawerButtonIcon} name='plus-circle' type="FontAwesome5" solid />
  401. <Text>Join</Text>
  402. </Button>
  403. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerForgotMenu.bind(this)}>
  404. <Icon style={styles.drawerButtonIcon} name='lock' type="FontAwesome5" solid />
  405. <Text>Forgot Password</Text>
  406. </Button>
  407. </Content>
  408. </Container>
  409. );
  410. }
  411. }
  412. const styles = StyleSheet.create({
  413. containerWebView: {
  414. flex: 1,
  415. },
  416. viewLoading: {
  417. position: 'absolute',
  418. top: 0,
  419. left: 0,
  420. right: 0,
  421. bottom: 0,
  422. justifyContent: 'center',
  423. alignItems: 'center',
  424. backgroundColor: '#FFF',
  425. },
  426. viewLoading2: {
  427. position: 'absolute',
  428. width: Dimensions.get('window').width,
  429. height: Dimensions.get('window').height,
  430. justifyContent: 'center',
  431. alignItems: 'center',
  432. backgroundColor: '#00000066',
  433. },
  434. buttonLeftTopAndroid: {
  435. },
  436. buttonLeftTopIos: {
  437. marginLeft: 5,
  438. },
  439. drawerImageContainer: {
  440. height:100,
  441. justifyContent: 'center',
  442. alignItems: 'center',
  443. backgroundColor: '#FFF',
  444. },
  445. drawerImage: {
  446. height:33.5,
  447. width:125,
  448. },
  449. drawerButton: {
  450. justifyContent: 'flex-start',
  451. },
  452. drawerButtonIcon: {
  453. width: 20,
  454. fontSize: 20,
  455. justifyContent: 'center',
  456. textAlign: 'center',
  457. },
  458. });