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