1 |
|
2 |
|
3 | import * as React from "react";
|
4 | import * as R from "ramda";
|
5 |
|
6 | import { connectToStore } from "@applicaster/zapp-react-native-redux";
|
7 | import { QUICK_BRICK_EVENTS } from "@applicaster/zapp-react-native-bridge/QuickBrick";
|
8 |
|
9 | import { QuickBrickEvents } from "../../Polyfills/QuickBrickCommunicationModule";
|
10 | import { Splash } from "../Splash";
|
11 |
|
12 | type Props = {
|
13 | appReady: boolean,
|
14 | };
|
15 |
|
16 | export function withAppLoader(Component) {
|
17 | class WrappedWithAppLoader extends React.Component<Props> {
|
18 | constructor(props) {
|
19 | super(props);
|
20 |
|
21 | this.state = {
|
22 | splash: !this.props.appReady,
|
23 | };
|
24 |
|
25 | this.quickBrickEventListener = this.quickBrickEventListener.bind(this);
|
26 | }
|
27 |
|
28 | componentDidMount() {
|
29 | this._isMounted = true;
|
30 |
|
31 | if (WrappedWithAppLoader.shouldShowSplash) {
|
32 | this.unsubscribe = QuickBrickEvents.subscribe(
|
33 | this.quickBrickEventListener
|
34 | );
|
35 | } else {
|
36 | setTimeout(() => this.setState({ splash: false }), 1);
|
37 | }
|
38 | }
|
39 |
|
40 | componentWillUnmount() {
|
41 | this._isMounted = false;
|
42 | if (this.unsubscribe) {
|
43 | this.unsubscribe();
|
44 | }
|
45 | }
|
46 |
|
47 | quickBrickEventListener(event, payload) {
|
48 | if (event === QUICK_BRICK_EVENTS.QUICK_BRICK_READY) {
|
49 | if (this._isMounted) {
|
50 | this.setState({ splash: false });
|
51 | } else {
|
52 | WrappedWithAppLoader.shouldShowSplash = false;
|
53 | }
|
54 | }
|
55 |
|
56 | if (event === QUICK_BRICK_EVENTS.MOVE_APP_TO_BACKGROUND) {
|
57 | try {
|
58 | tizen.application.getCurrentApplication().exit();
|
59 | } catch (e) {
|
60 |
|
61 | console.warn("Cannot exit application", e);
|
62 | }
|
63 | }
|
64 | }
|
65 |
|
66 | render() {
|
67 | const { splash } = this.state;
|
68 | return splash ? <Splash /> : <Component {...this.props} />;
|
69 | }
|
70 | }
|
71 |
|
72 | WrappedWithAppLoader.shouldShowSplash = true;
|
73 |
|
74 | return connectToStore(
|
75 | R.compose(
|
76 | R.assoc("appReady", R.__, {}),
|
77 | R.path(["appState", "appReady"])
|
78 | )
|
79 | )(WrappedWithAppLoader);
|
80 | }
|