App.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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']))
  255. OneSignal.sendTags(oMsgData['push_tags']);
  256. if ('undefined' !== typeof(oMsgData['stop_loading']) && oMsgData['stop_loading']) {
  257. this.onWebViewLoadEnd (event);
  258. }
  259. }
  260. drawerClose () {
  261. this.refs.drawer._root.close()
  262. }
  263. drawerOpen () {
  264. this.refs.drawer._root.open()
  265. }
  266. render() {
  267. StatusBar.setBarStyle(Platform.OS === 'android' ? 'light-content' : 'dark-content', false);
  268. return (
  269. <Container>
  270. <Drawer ref="drawer" content={this.renderDrawer()} onClose={this.drawerClose.bind(this)}>
  271. {this.state.searchbar ? this.renderToolbarSearch() : this.renderToolbar() }
  272. <WebView
  273. useWebKit={true}
  274. ref="webView"
  275. key={this.state.key}
  276. geolocationEnabled={true}
  277. builtInZoomControls={false}
  278. decelerationRate="normal"
  279. cacheEnabled={true}
  280. allowsLinkPreview={true}
  281. hideKeyboardAccessoryView={true}
  282. injectedJavaScript={this.javascriptToInject()}
  283. onShouldStartLoadWithRequest={this.onWebViewShouldStartLoadWithRequest.bind(this)}
  284. onNavigationStateChange={this.onWebViewNavigationStateChange.bind(this)}
  285. style={styles.containerWebView}
  286. source={{uri: `${BASE_URL}?skin=${TEMPLATE}`}}
  287. userAgent={"UNAMobileApp/Mobile (" + Platform.OS + ")"}
  288. onMessage={this.onWebViewMessage.bind(this)}
  289. allowFileAccess={true}
  290. onLoad={this.onWebViewLoadEnd.bind(this)}
  291. onLoadStart={this.onWebViewLoadStart.bind(this)}
  292. />
  293. {this.state.data.loggedin && (
  294. <Footer>
  295. <FooterTab>
  296. <Button vertical onPress={this.onHomeMenu.bind(this)}>
  297. <Icon name="home" type="FontAwesome5" />
  298. </Button>
  299. </FooterTab>
  300. <FooterTab>
  301. <Button vertical onPress={this.onNotificationsMenu.bind(this)} badge={this.state.data.bubbles['notifications-notifications'] > 0 ? true : false}>
  302. {this.state.data.bubbles['notifications-notifications'] > 0 &&
  303. (<Badge><Text>{this.state.data.bubbles['notifications-notifications']}</Text></Badge>)
  304. }
  305. <Icon name="bell" type="FontAwesome5" solid />
  306. </Button>
  307. </FooterTab>
  308. <FooterTab>
  309. <Button vertical onPress={this.onAddMenu.bind(this)}>
  310. <Icon name="plus-circle" type="FontAwesome5" solid />
  311. </Button>
  312. </FooterTab>
  313. <FooterTab>
  314. <Button vertical onPress={this.onMessengerMenu.bind(this)} badge={this.state.data.bubbles['notifications-messenger'] > 0 ? true : false}>
  315. {this.state.data.bubbles['notifications-messenger'] > 0 &&
  316. (<Badge><Text>{this.state.data.bubbles['notifications-messenger']}</Text></Badge>)
  317. }
  318. <Icon name="comments" type="FontAwesome5" solid />
  319. </Button>
  320. </FooterTab>
  321. <FooterTab>
  322. <Button vertical onPress={this.onProfileMenu.bind(this)} badge={this.state.data.bubbles_num > 0 ? true : false}>
  323. {this.state.data.bubbles_num > 0 &&
  324. (<Badge><Text>{this.state.data.bubbles_num}</Text></Badge>)
  325. }
  326. <Icon name="user" type="FontAwesome5" solid />
  327. </Button>
  328. </FooterTab>
  329. </Footer>
  330. )}
  331. {this.state.loading && (
  332. <View style={styles.viewLoading2}>
  333. <ActivityIndicator size="large" color="#fff" />
  334. </View>
  335. )}
  336. </Drawer>
  337. </Container>
  338. );
  339. }
  340. renderToolbar () {
  341. return (
  342. <Header>
  343. <Left>
  344. <Button style={Platform.OS === 'android' ? styles.buttonLeftTopAndroid : styles.buttonLeftTopIos} transparent onPress={this.onBackAndMainMenu.bind(this)}>
  345. <Icon name={this.state.backButtonEnabled ? 'ios-arrow-back' : 'ios-menu'} />
  346. </Button>
  347. </Left>
  348. <Body>
  349. <Title>{TITLE}</Title>
  350. </Body>
  351. <Right>
  352. <Button transparent>
  353. <Icon name='search' onPress={this.onSearchMenu.bind(this)} />
  354. </Button>
  355. </Right>
  356. </Header>
  357. );
  358. }
  359. renderToolbarSearch () {
  360. return (
  361. <Header searchBar rounded>
  362. <Item>
  363. <Icon name="search" />
  364. <Input ref="inputSearch" placeholder="Search" onEndEditing={this.onSearchCancelMenu.bind(this)} onSubmitEditing={this.onSearch.bind(this)} />
  365. </Item>
  366. <Button transparent onPress={this.onSearchCancelMenu.bind(this)}>
  367. <Text>Cancel</Text>
  368. </Button>
  369. </Header>
  370. );
  371. }
  372. renderLoadingView () {
  373. return (
  374. <View style={styles.viewLoading}>
  375. <Image source={require('./img/logo-loading.png')} />
  376. <Text>Loading...</Text>
  377. </View>
  378. );
  379. }
  380. renderDrawer () {
  381. return (
  382. <Container>
  383. <Content>
  384. <View style={styles.drawerImageContainer}>
  385. <Image style={styles.drawerImage} source={require('./img/logo-loading.png')} />
  386. </View>
  387. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerLoginMenu.bind(this)}>
  388. <Icon style={styles.drawerButtonIcon} name='key' type="FontAwesome5" solid />
  389. <Text>Login</Text>
  390. </Button>
  391. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerJoinMenu.bind(this)}>
  392. <Icon style={styles.drawerButtonIcon} name='plus-circle' type="FontAwesome5" solid />
  393. <Text>Join</Text>
  394. </Button>
  395. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerForgotMenu.bind(this)}>
  396. <Icon style={styles.drawerButtonIcon} name='lock' type="FontAwesome5" solid />
  397. <Text>Forgot Password</Text>
  398. </Button>
  399. </Content>
  400. </Container>
  401. );
  402. }
  403. }
  404. const styles = StyleSheet.create({
  405. containerWebView: {
  406. flex: 1,
  407. },
  408. viewLoading: {
  409. position: 'absolute',
  410. top: 0,
  411. left: 0,
  412. right: 0,
  413. bottom: 0,
  414. justifyContent: 'center',
  415. alignItems: 'center',
  416. backgroundColor: '#FFF',
  417. },
  418. viewLoading2: {
  419. position: 'absolute',
  420. width: Dimensions.get('window').width,
  421. height: Dimensions.get('window').height,
  422. justifyContent: 'center',
  423. alignItems: 'center',
  424. backgroundColor: '#00000066',
  425. },
  426. buttonLeftTopAndroid: {
  427. },
  428. buttonLeftTopIos: {
  429. marginLeft: 5,
  430. },
  431. drawerImageContainer: {
  432. height:100,
  433. justifyContent: 'center',
  434. alignItems: 'center',
  435. backgroundColor: '#FFF',
  436. },
  437. drawerImage: {
  438. height:33.5,
  439. width:125,
  440. },
  441. drawerButton: {
  442. justifyContent: 'flex-start',
  443. },
  444. drawerButtonIcon: {
  445. width: 20,
  446. fontSize: 20,
  447. justifyContent: 'center',
  448. textAlign: 'center',
  449. },
  450. });