App.js 20 KB

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