App.js 15 KB

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