App.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. this.setState ({
  137. url: navState.url,
  138. status: navState.title,
  139. backButtonEnabled: navState.canGoBack,
  140. forwardButtonEnabled: navState.canGoForward,
  141. searchbar: false,
  142. });
  143. }
  144. onWebViewLoadStart (syntheticEvent) {
  145. this.loading = true;
  146. this.setState ({
  147. loading: this.loading,
  148. searchbar: false,
  149. });
  150. }
  151. onWebViewLoadEnd (syntheticEvent) {
  152. this.loading = false;
  153. this.setState ({
  154. loading: this.loading,
  155. searchbar: false,
  156. });
  157. }
  158. reload() {
  159. this.refs.webView.reload();
  160. }
  161. postMessage (data) {
  162. // posts a message to web view
  163. this.refs.webView.postMessage(data);
  164. }
  165. injectJavaScript (script) {
  166. // executes JavaScript immediately in web view
  167. this.refs.webView.injectJavaScript(script);
  168. }
  169. onWebViewShouldStartLoadWithRequest (event) {
  170. if (0 != event.url.indexOf('http') || (-1 == event.url.indexOf(`${BASE_URL}`) && ('android' === Platform.OS || ('click' == event.navigationType && 'ios' === Platform.OS)))) {
  171. Linking.canOpenURL(event.url).then(supported => {
  172. if (supported) {
  173. Linking.openURL(event.url);
  174. }
  175. else {
  176. console.log('Don\'t know how to open URI: ' + event.url);
  177. }
  178. return false;
  179. });
  180. return false;
  181. }
  182. if ('android' === Platform.OS)
  183. this.onWebViewLoadStart(); // show loading screen
  184. return true;
  185. }
  186. javascriptToInject () {
  187. return ``
  188. }
  189. componentDidCatch(error, info) {
  190. SplashScreen.hide();
  191. }
  192. componentDidMount() {
  193. OneSignal.init(ONESIGNALAPPID, {kOSSettingsKeyAutoPrompt : true});
  194. OneSignal.inFocusDisplaying(0);
  195. OneSignal.addEventListener('received', this.onNotificationReceived);
  196. OneSignal.addEventListener('opened', this.onNotificationOpened.bind(this));
  197. if (Platform.OS === 'android') {
  198. this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
  199. this.onBack(); // works best when the goBack is async
  200. return true;
  201. });
  202. }
  203. SplashScreen.hide();
  204. }
  205. componentWillUnmount() {
  206. OneSignal.removeEventListener('received', this.onNotificationReceived);
  207. OneSignal.removeEventListener('opened', this.onNotificationOpened);
  208. if (Platform.OS === 'android') {
  209. this.backHandler.remove();
  210. }
  211. }
  212. onNotificationReceived(notification) {
  213. console.log("Notification received: ", notification);
  214. }
  215. onNotificationOpened(openResult) {
  216. if ('undefined' !== typeof(openResult.notification.payload.additionalData) && 'undefined' !== typeof(openResult.notification.payload.additionalData.url) && !openResult.notification.isAppInFocus) {
  217. this.injectJavaScript(`window.location = '${openResult.notification.payload.additionalData.url}';`);
  218. }
  219. }
  220. onWebViewMessage(event) {
  221. let oMsgData;
  222. try {
  223. oMsgData = JSON.parse(event.nativeEvent.data);
  224. }
  225. catch(err) {
  226. console.warn(err);
  227. return;
  228. }
  229. if ('undefined' !== typeof(oMsgData.loggedin)) {
  230. this.setState ({
  231. loading: this.loading,
  232. data: oMsgData,
  233. searchbar: false,
  234. key: 'undefined' === typeof(this.state.data.loggedin) || this.state.data.loggedin != oMsgData.loggedin ? this.state.key + 1 : this.state.key,
  235. });
  236. }
  237. if ('undefined' !== typeof(oMsgData['push_tags']))
  238. OneSignal.sendTags(oMsgData['push_tags']);
  239. if ('undefined' !== typeof(oMsgData['stop_loading']) && oMsgData['stop_loading']) {
  240. this.onWebViewLoadEnd (event);
  241. }
  242. }
  243. drawerClose () {
  244. this.refs.drawer._root.close()
  245. }
  246. drawerOpen () {
  247. this.refs.drawer._root.open()
  248. }
  249. render() {
  250. StatusBar.setBarStyle(Platform.OS === 'android' ? 'light-content' : 'dark-content', false);
  251. return (
  252. <Container>
  253. <Drawer ref="drawer" content={this.renderDrawer()} onClose={this.drawerClose.bind(this)}>
  254. {this.state.searchbar ? this.renderToolbarSearch() : this.renderToolbar() }
  255. <WebView
  256. useWebKit={true}
  257. ref="webView"
  258. key={this.state.key}
  259. geolocationEnabled={true}
  260. builtInZoomControls={false}
  261. decelerationRate="normal"
  262. cacheEnabled={true}
  263. allowsLinkPreview={true}
  264. hideKeyboardAccessoryView={true}
  265. injectedJavaScript={this.javascriptToInject()}
  266. onShouldStartLoadWithRequest={this.onWebViewShouldStartLoadWithRequest.bind(this)}
  267. onNavigationStateChange={this.onWebViewNavigationStateChange.bind(this)}
  268. style={styles.containerWebView}
  269. source={{uri: `${BASE_URL}?skin=${TEMPLATE}`}}
  270. userAgent={"UNAMobileApp/Mobile (" + Platform.OS + ")"}
  271. onMessage={this.onWebViewMessage.bind(this)}
  272. allowFileAccess={true}
  273. onLoad={this.onWebViewLoadEnd.bind(this)}
  274. onLoadStart={this.onWebViewLoadStart.bind(this)}
  275. />
  276. {this.state.data.loggedin && (
  277. <Footer>
  278. <FooterTab>
  279. <Button vertical onPress={this.onHomeMenu.bind(this)}>
  280. <Icon name="home" type="FontAwesome5" />
  281. </Button>
  282. </FooterTab>
  283. <FooterTab>
  284. <Button vertical onPress={this.onNotificationsMenu.bind(this)} badge={this.state.data.bubbles['notifications-notifications'] > 0 ? true : false}>
  285. {this.state.data.bubbles['notifications-notifications'] > 0 &&
  286. (<Badge><Text>{this.state.data.bubbles['notifications-notifications']}</Text></Badge>)
  287. }
  288. <Icon name="bell" type="FontAwesome5" solid />
  289. </Button>
  290. </FooterTab>
  291. <FooterTab>
  292. <Button vertical onPress={this.onAddMenu.bind(this)}>
  293. <Icon name="plus-circle" type="FontAwesome5" solid />
  294. </Button>
  295. </FooterTab>
  296. <FooterTab>
  297. <Button vertical onPress={this.onMessengerMenu.bind(this)} badge={this.state.data.bubbles['notifications-messenger'] > 0 ? true : false}>
  298. {this.state.data.bubbles['notifications-messenger'] > 0 &&
  299. (<Badge><Text>{this.state.data.bubbles['notifications-messenger']}</Text></Badge>)
  300. }
  301. <Icon name="comments" type="FontAwesome5" solid />
  302. </Button>
  303. </FooterTab>
  304. <FooterTab>
  305. <Button vertical onPress={this.onProfileMenu.bind(this)} badge={this.state.data.bubbles_num > 0 ? true : false}>
  306. {this.state.data.bubbles_num > 0 &&
  307. (<Badge><Text>{this.state.data.bubbles_num}</Text></Badge>)
  308. }
  309. <Icon name="user" type="FontAwesome5" solid />
  310. </Button>
  311. </FooterTab>
  312. </Footer>
  313. )}
  314. {this.state.loading && (
  315. <View style={styles.viewLoading2}>
  316. <ActivityIndicator size="large" color="#fff" />
  317. </View>
  318. )}
  319. </Drawer>
  320. </Container>
  321. );
  322. }
  323. renderToolbar () {
  324. return (
  325. <Header>
  326. <Left>
  327. <Button style={Platform.OS === 'android' ? styles.buttonLeftTopAndroid : styles.buttonLeftTopIos} transparent onPress={this.onBackAndMainMenu.bind(this)}>
  328. <Icon name={this.state.backButtonEnabled ? 'ios-arrow-back' : 'ios-menu'} />
  329. </Button>
  330. </Left>
  331. <Body>
  332. <Title>{TITLE}</Title>
  333. </Body>
  334. <Right>
  335. <Button transparent>
  336. <Icon name='search' onPress={this.onSearchMenu.bind(this)} />
  337. </Button>
  338. </Right>
  339. </Header>
  340. );
  341. }
  342. renderToolbarSearch () {
  343. return (
  344. <Header searchBar rounded>
  345. <Item>
  346. <Icon name="search" />
  347. <Input ref="inputSearch" placeholder="Search" onEndEditing={this.onSearchCancelMenu.bind(this)} onSubmitEditing={this.onSearch.bind(this)} />
  348. </Item>
  349. <Button transparent onPress={this.onSearchCancelMenu.bind(this)}>
  350. <Text>Cancel</Text>
  351. </Button>
  352. </Header>
  353. );
  354. }
  355. renderLoadingView () {
  356. return (
  357. <View style={styles.viewLoading}>
  358. <Image source={require('./img/logo-loading.png')} />
  359. <Text>Loading...</Text>
  360. </View>
  361. );
  362. }
  363. renderDrawer () {
  364. return (
  365. <Container>
  366. <Content>
  367. <View style={styles.drawerImageContainer}>
  368. <Image style={styles.drawerImage} source={require('./img/logo-loading.png')} />
  369. </View>
  370. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerLoginMenu.bind(this)}>
  371. <Icon style={styles.drawerButtonIcon} name='key' type="FontAwesome5" solid />
  372. <Text>Login</Text>
  373. </Button>
  374. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerJoinMenu.bind(this)}>
  375. <Icon style={styles.drawerButtonIcon} name='plus-circle' type="FontAwesome5" solid />
  376. <Text>Join</Text>
  377. </Button>
  378. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerForgotMenu.bind(this)}>
  379. <Icon style={styles.drawerButtonIcon} name='lock' type="FontAwesome5" solid />
  380. <Text>Forgot Password</Text>
  381. </Button>
  382. </Content>
  383. </Container>
  384. );
  385. }
  386. }
  387. const styles = StyleSheet.create({
  388. containerWebView: {
  389. flex: 1,
  390. },
  391. viewLoading: {
  392. position: 'absolute',
  393. top: 0,
  394. left: 0,
  395. right: 0,
  396. bottom: 0,
  397. justifyContent: 'center',
  398. alignItems: 'center',
  399. backgroundColor: '#FFF',
  400. },
  401. viewLoading2: {
  402. position: 'absolute',
  403. width: Dimensions.get('window').width,
  404. height: Dimensions.get('window').height,
  405. justifyContent: 'center',
  406. alignItems: 'center',
  407. backgroundColor: '#00000066',
  408. },
  409. buttonLeftTopAndroid: {
  410. },
  411. buttonLeftTopIos: {
  412. marginLeft: 5,
  413. },
  414. drawerImageContainer: {
  415. height:100,
  416. justifyContent: 'center',
  417. alignItems: 'center',
  418. backgroundColor: '#FFF',
  419. },
  420. drawerImage: {
  421. height:33.5,
  422. width:125,
  423. },
  424. drawerButton: {
  425. justifyContent: 'flex-start',
  426. },
  427. drawerButtonIcon: {
  428. width: 20,
  429. fontSize: 20,
  430. justifyContent: 'center',
  431. textAlign: 'center',
  432. },
  433. });