App.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. 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 = '***REMOVED***';
  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. componentDidCatch(error, info) {
  189. SplashScreen.hide();
  190. }
  191. componentDidMount() {
  192. OneSignal.init(ONESIGNALAPPID, {kOSSettingsKeyAutoPrompt : true});
  193. OneSignal.addEventListener('received', this.onNotificationReceived);
  194. OneSignal.addEventListener('opened', this.onNotificationOpened.bind(this));
  195. if (Platform.OS === 'android') {
  196. this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
  197. this.onBack(); // works best when the goBack is async
  198. return true;
  199. });
  200. }
  201. SplashScreen.hide();
  202. }
  203. componentWillUnmount() {
  204. OneSignal.removeEventListener('received', this.onNotificationReceived);
  205. OneSignal.removeEventListener('opened', this.onNotificationOpened);
  206. if (Platform.OS === 'android') {
  207. this.backHandler.remove();
  208. }
  209. }
  210. onNotificationReceived(notification) {
  211. console.log("Notification received: ", notification);
  212. }
  213. onNotificationOpened(openResult) {
  214. if ('undefined' !== typeof(openResult.notification.payload.additionalData) && 'undefined' !== typeof(openResult.notification.payload.additionalData.url) && !openResult.notification.isAppInFocus) {
  215. this.injectJavaScript(`window.location = '${openResult.notification.payload.additionalData.url}';`);
  216. }
  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. cacheEnabled={true}
  260. injectedJavaScript={this.javascriptToInject()}
  261. onShouldStartLoadWithRequest={this.onWebViewShouldStartLoadWithRequest.bind(this)}
  262. onNavigationStateChange={this.onWebViewNavigationStateChange.bind(this)}
  263. style={styles.containerWebView}
  264. source={{uri: `${BASE_URL}?skin=${TEMPLATE}`}}
  265. userAgent={"UNAMobileApp/Mobile (" + Platform.OS + ")"}
  266. onMessage={this.onWebViewMessage.bind(this)}
  267. allowFileAccess={true}
  268. onLoad={this.onWebViewLoadEnd.bind(this)}
  269. onLoadStart={this.onWebViewLoadStart.bind(this)}
  270. />
  271. {this.state.data.loggedin && (
  272. <Footer>
  273. <FooterTab>
  274. <Button vertical onPress={this.onHomeMenu.bind(this)}>
  275. <Icon name="ios-home" />
  276. </Button>
  277. </FooterTab>
  278. <FooterTab>
  279. <Button vertical onPress={this.onNotificationsMenu.bind(this)} badge={this.state.data.bubbles['notifications-notifications'] > 0 ? true : false}>
  280. {this.state.data.bubbles['notifications-notifications'] > 0 &&
  281. (<Badge><Text>{this.state.data.bubbles['notifications-notifications']}</Text></Badge>)
  282. }
  283. <Icon name="ios-notifications" />
  284. </Button>
  285. </FooterTab>
  286. <FooterTab>
  287. <Button vertical onPress={this.onAddMenu.bind(this)}>
  288. <Icon name="ios-add-circle-outline" />
  289. </Button>
  290. </FooterTab>
  291. <FooterTab>
  292. <Button vertical onPress={this.onMessengerMenu.bind(this)} badge={this.state.data.bubbles['notifications-messenger'] > 0 ? true : false}>
  293. {this.state.data.bubbles['notifications-messenger'] > 0 &&
  294. (<Badge><Text>{this.state.data.bubbles['notifications-messenger']}</Text></Badge>)
  295. }
  296. <Icon name="ios-chatbubbles" />
  297. </Button>
  298. </FooterTab>
  299. <FooterTab>
  300. <Button vertical onPress={this.onProfileMenu.bind(this)} badge={this.state.data.bubbles_num > 0 ? true : false}>
  301. {this.state.data.bubbles_num > 0 &&
  302. (<Badge><Text>{this.state.data.bubbles_num}</Text></Badge>)
  303. }
  304. <Icon name="ios-person" />
  305. </Button>
  306. </FooterTab>
  307. </Footer>
  308. )}
  309. {this.state.loading && (
  310. <View style={styles.viewLoading2}>
  311. <ActivityIndicator size="large" color="#fff" />
  312. </View>
  313. )}
  314. </Drawer>
  315. </Container>
  316. );
  317. }
  318. renderToolbar () {
  319. return (
  320. <Header>
  321. <Left>
  322. <Button style={Platform.OS === 'android' ? styles.buttonLeftTopAndroid : styles.buttonLeftTopIos} transparent onPress={this.onBackAndMainMenu.bind(this)}>
  323. <Icon name={this.state.backButtonEnabled ? 'ios-arrow-back' : 'ios-menu'} />
  324. </Button>
  325. </Left>
  326. <Body>
  327. <Title>{TITLE}</Title>
  328. </Body>
  329. <Right>
  330. <Button transparent>
  331. <Icon name='ios-search' onPress={this.onSearchMenu.bind(this)} />
  332. </Button>
  333. </Right>
  334. </Header>
  335. );
  336. }
  337. renderToolbarSearch () {
  338. return (
  339. <Header searchBar rounded>
  340. <Item>
  341. <Icon name="ios-search" />
  342. <Input ref="inputSearch" placeholder="Search" onEndEditing={this.onSearchCancelMenu.bind(this)} onSubmitEditing={this.onSearch.bind(this)} />
  343. </Item>
  344. <Button transparent onPress={this.onSearchCancelMenu.bind(this)}>
  345. <Text>Cancel</Text>
  346. </Button>
  347. </Header>
  348. );
  349. }
  350. renderLoadingView () {
  351. return (
  352. <View style={styles.viewLoading}>
  353. <Image source={require('./img/logo-loading.png')} />
  354. <Text>Loading...</Text>
  355. </View>
  356. );
  357. }
  358. renderDrawer () {
  359. return (
  360. <Container>
  361. <Content>
  362. <View style={styles.drawerImageContainer}>
  363. <Image style={styles.drawerImage} source={require('./img/logo-loading.png')} />
  364. </View>
  365. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerLoginMenu.bind(this)}>
  366. <Icon style={styles.drawerButtonIcon} name='ios-key' />
  367. <Text>Login</Text>
  368. </Button>
  369. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerJoinMenu.bind(this)}>
  370. <Icon style={styles.drawerButtonIcon} name='ios-add-circle-outline' />
  371. <Text>Join</Text>
  372. </Button>
  373. <Button style={styles.drawerButton} iconLeft transparent onPress={this.onDrawerForgotMenu.bind(this)}>
  374. <Icon style={styles.drawerButtonIcon} name='ios-lock' />
  375. <Text>Forgot Password</Text>
  376. </Button>
  377. </Content>
  378. </Container>
  379. );
  380. }
  381. }
  382. const styles = StyleSheet.create({
  383. containerWebView: {
  384. flex: 1,
  385. },
  386. viewLoading: {
  387. position: 'absolute',
  388. top: 0,
  389. left: 0,
  390. right: 0,
  391. bottom: 0,
  392. justifyContent: 'center',
  393. alignItems: 'center',
  394. backgroundColor: '#FFF',
  395. },
  396. viewLoading2: {
  397. position: 'absolute',
  398. width: Dimensions.get('window').width,
  399. height: Dimensions.get('window').height,
  400. justifyContent: 'center',
  401. alignItems: 'center',
  402. backgroundColor: '#00000066',
  403. },
  404. buttonLeftTopAndroid: {
  405. },
  406. buttonLeftTopIos: {
  407. marginLeft: 5,
  408. },
  409. drawerImageContainer: {
  410. height:100,
  411. justifyContent: 'center',
  412. alignItems: 'center',
  413. backgroundColor: '#FFF',
  414. },
  415. drawerImage: {
  416. height:33.5,
  417. width:125,
  418. },
  419. drawerButton: {
  420. justifyContent: 'flex-start',
  421. },
  422. drawerButtonIcon: {
  423. width: 20,
  424. justifyContent: 'center',
  425. },
  426. });