UNPKG

2.04 kBJavaScriptView Raw
1// @flow
2/* global tizen */
3import * as React from "react";
4import * as R from "ramda";
5
6import { connectToStore } from "@applicaster/zapp-react-native-redux";
7import { QUICK_BRICK_EVENTS } from "@applicaster/zapp-react-native-bridge/QuickBrick";
8
9import { QuickBrickEvents } from "../../Polyfills/QuickBrickCommunicationModule";
10import { Splash } from "../Splash";
11
12type Props = {
13 appReady: boolean,
14};
15
16export 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 // eslint-disable-next-line no-console
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}