App.js 15 KB

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