App.js 16 KB

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