App.js 16 KB

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