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 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. componentWillMount() {
  189. OneSignal.init(ONESIGNALAPPID, {kOSSettingsKeyAutoPrompt : true});
  190. OneSignal.addEventListener('received', this.onNotificationReceived);
  191. OneSignal.addEventListener('opened', this.onNotificationOpened.bind(this));
  192. }
  193. componentDidMount() {
  194. if (Platform.OS === 'android') {
  195. this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
  196. this.onBack(); // works best when the goBack is async
  197. return true;
  198. });
  199. }
  200. }
  201. componentWillUnmount() {
  202. this.backHandler.remove();
  203. OneSignal.removeEventListener('received', this.onNotificationReceived);
  204. OneSignal.removeEventListener('opened', this.onNotificationOpened);
  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.setState({
  212. url: openResult.notification.payload.additionalData.url,
  213. searchbar: false,
  214. });
  215. }
  216. }
  217. onWebViewMessage(event) {
  218. let oMsgData;
  219. try {
  220. oMsgData = JSON.parse(event.nativeEvent.data);
  221. }
  222. catch(err) {
  223. console.warn(err);
  224. return;
  225. }
  226. if ('undefined' !== typeof(oMsgData.loggedin)) {
  227. this.setState ({
  228. loading: this.loading,
  229. data: oMsgData,
  230. searchbar: false,
  231. key: 'undefined' === typeof(this.state.data.loggedin) || this.state.data.loggedin != oMsgData.loggedin ? this.state.key + 1 : this.state.key,
  232. });
  233. }
  234. if ('undefined' !== typeof(oMsgData['push_tags']))
  235. OneSignal.sendTags(oMsgData['push_tags']);
  236. if ('undefined' !== typeof(oMsgData['stop_loading']) && oMsgData['stop_loading']) {
  237. this.onWebViewLoadEnd (event);
  238. }
  239. }
  240. drawerClose () {
  241. this.refs.drawer._root.close()
  242. }
  243. drawerOpen () {
  244. this.refs.drawer._root.open()
  245. }
  246. render() {
  247. return (
  248. <Container>
  249. <Drawer ref="drawer" content={this.renderDrawer()} onClose={this.drawerClose.bind(this)}>
  250. {this.state.searchbar ? this.renderToolbarSearch() : this.renderToolbar() }
  251. <WebView
  252. useWebKit={true}
  253. ref="webView"
  254. key={this.state.key}
  255. geolocationEnabled={true}
  256. builtInZoomControls={false}
  257. decelerationRate="normal"
  258. injectedJavaScript={this.javascriptToInject()}
  259. onShouldStartLoadWithRequest={this.onWebViewShouldStartLoadWithRequest.bind(this)}
  260. onNavigationStateChange={this.onWebViewNavigationStateChange.bind(this)}
  261. style={styles.containerWebView}
  262. source={{uri: `${BASE_URL}?skin=${TEMPLATE}`}}
  263. userAgent={"UNAMobileApp/Mobile (" + Platform.OS + ")"}
  264. onMessage={this.onWebViewMessage.bind(this)}
  265. allowFileAccess={true}
  266. onLoad={this.onWebViewLoadEnd.bind(this)}
  267. onLoadStart={this.onWebViewLoadStart.bind(this)}
  268. />
  269. {this.state.data.loggedin && (
  270. <Footer>
  271. <FooterTab>
  272. <Button vertical onPress={this.onHomeMenu.bind(this)}>
  273. <Icon name="ios-home" />
  274. </Button>
  275. </FooterTab>
  276. <FooterTab>
  277. <Button vertical onPress={this.onNotificationsMenu.bind(this)} badge={this.state.data.bubbles['notifications-notifications'] > 0 ? true : false}>
  278. {this.state.data.bubbles['notifications-notifications'] > 0 &&
  279. (<Badge><Text>{this.state.data.bubbles['notifications-notifications']}</Text></Badge>)
  280. }
  281. <Icon name="ios-notifications" />
  282. </Button>
  283. </FooterTab>
  284. <FooterTab>
  285. <Button vertical onPress={this.onAddMenu.bind(this)}>
  286. <Icon name="ios-add-circle-outline" />
  287. </Button>
  288. </FooterTab>
  289. <FooterTab>
  290. <Button vertical onPress={this.onMessengerMenu.bind(this)} badge={this.state.data.bubbles['notifications-messenger'] > 0 ? true : false}>
  291. {this.state.data.bubbles['notifications-messenger'] > 0 &&
  292. (<Badge><Text>{this.state.data.bubbles['notifications-messenger']}</Text></Badge>)
  293. }
  294. <Icon name="ios-chatbubbles" />
  295. </Button>
  296. </FooterTab>
  297. <FooterTab>
  298. <Button vertical onPress={this.onProfileMenu.bind(this)} badge={this.state.data.bubbles_num > 0 ? true : false}>
  299. {this.state.data.bubbles_num > 0 &&
  300. (<Badge><Text>{this.state.data.bubbles_num}</Text></Badge>)
  301. }
  302. <Icon name="ios-person" />
  303. </Button>
  304. </FooterTab>
  305. </Footer>
  306. )}
  307. {this.state.loading && (
  308. <View style={styles.viewLoading2}>
  309. <ActivityIndicator size="large" color="#fff" />
  310. </View>
  311. )}
  312. </Drawer>
  313. </Container>
  314. );
  315. }
  316. renderToolbar () {
  317. return (
  318. <Header>
  319. <Left>
  320. <Button style={Platform.OS === 'android' ? styles.buttonLeftTopAndroid : styles.buttonLeftTopIos} transparent onPress={this.onBackAndMainMenu.bind(this)}>
  321. <Icon name={this.state.backButtonEnabled ? 'ios-arrow-back' : 'ios-menu'} />
  322. </Button>
  323. </Left>
  324. <Body>
  325. <Title>{TITLE}</Title>
  326. </Body>
  327. <Right>
  328. <Button transparent>
  329. <Icon name='ios-search' onPress={this.onSearchMenu.bind(this)} />
  330. </Button>
  331. </Right>
  332. </Header>
  333. );
  334. }
  335. renderToolbarSearch () {
  336. return (
  337. <Header searchBar rounded>
  338. <Item>
  339. <Icon name="ios-search" />
  340. <Input ref="inputSearch" placeholder="Search" onEndEditing={this.onSearchCancelMenu.bind(this)} onSubmitEditing={this.onSearch.bind(this)} />
  341. </Item>
  342. <Button transparent onPress={this.onSearchCancelMenu.bind(this)}>
  343. <Text>Cancel</Text>
  344. </Button>
  345. </Header>
  346. );
  347. }
  348. renderLoadingView () {
  349. return (
  350. <View style={styles.viewLoading}>
  351. <Image source={require('./img/logo-loading.png')} />
  352. <Text>Loading...</Text>
  353. </View>
  354. );
  355. }
  356. renderDrawer () {
  357. return (
  358. <Container>
  359. <Content>
  360. <View style={styles.drawerImageContainer}>
  361. <Image style={styles.drawerImage} source={require('./img/logo-loading.png')} />
  362. </View>
  363. <Button iconLeft transparent onPress={this.onDrawerLoginMenu.bind(this)}>
  364. <Icon style={styles.drawerButtonIcon} name='ios-key' />
  365. <Text>Login</Text>
  366. </Button>
  367. <Button iconLeft transparent onPress={this.onDrawerJoinMenu.bind(this)}>
  368. <Icon style={styles.drawerButtonIcon} name='ios-add-circle-outline' />
  369. <Text>Join</Text>
  370. </Button>
  371. <Button iconLeft transparent onPress={this.onDrawerForgotMenu.bind(this)}>
  372. <Icon style={styles.drawerButtonIcon} name='ios-lock' />
  373. <Text>Forgot Password</Text>
  374. </Button>
  375. </Content>
  376. </Container>
  377. );
  378. }
  379. }
  380. const styles = StyleSheet.create({
  381. containerWebView: {
  382. flex: 1,
  383. },
  384. viewLoading: {
  385. position: 'absolute',
  386. top: 0,
  387. left: 0,
  388. right: 0,
  389. bottom: 0,
  390. justifyContent: 'center',
  391. alignItems: 'center',
  392. backgroundColor: '#FFF',
  393. },
  394. viewLoading2: {
  395. position: 'absolute',
  396. width: Dimensions.get('window').width,
  397. height: Dimensions.get('window').height,
  398. justifyContent: 'center',
  399. alignItems: 'center',
  400. backgroundColor: '#00000066',
  401. },
  402. buttonLeftTopAndroid: {
  403. },
  404. buttonLeftTopIos: {
  405. marginLeft: 5,
  406. },
  407. drawerImageContainer: {
  408. height:100,
  409. justifyContent: 'center',
  410. alignItems: 'center',
  411. backgroundColor: '#FFF',
  412. },
  413. drawerImage: {
  414. height:33.5,
  415. width:125,
  416. },
  417. drawerButtonIcon: {
  418. width: 20,
  419. justifyContent: 'center',
  420. },
  421. });