@applicaster/zapp-react-dom-app
Version:
Zapp App Component for Applicaster's Quick Brick React Native App
83 lines (65 loc) • 2.03 kB
JavaScript
/* global tizen */
import * as React from "react";
import * as R from "ramda";
import PropTypes from "prop-types";
import { connectToStore } from "@applicaster/zapp-react-native-redux";
import { QUICK_BRICK_EVENTS } from "@applicaster/zapp-react-native-bridge/QuickBrick";
import { QuickBrickEvents } from "../../Polyfills/QuickBrickCommunicationModule";
import { Splash } from "../Splash";
const getAppLaunched = R.compose(
R.assoc("appLaunched", R.__, {}),
R.path(["appState", "appLaunched"])
);
class SplashHandler extends React.Component {
constructor(props) {
super(props);
this.state = {
splash: !this.props.appLaunched,
};
this.quickBrickEventListener = this.quickBrickEventListener.bind(this);
}
componentDidMount() {
this._isMounted = true;
if (SplashHandler.shouldShowSplash) {
this.unsubscribe = QuickBrickEvents.subscribe(
this.quickBrickEventListener
);
} else {
setTimeout(() => this.setState({ splash: false }), 1);
}
}
componentWillUnmount() {
this._isMounted = false;
if (this.unsubscribe) {
this.unsubscribe();
}
}
quickBrickEventListener(event) {
if (event === QUICK_BRICK_EVENTS.QUICK_BRICK_READY) {
if (this._isMounted) {
this.setState({ splash: false });
} else {
SplashHandler.shouldShowSplash = false;
}
}
if (event === QUICK_BRICK_EVENTS.MOVE_APP_TO_BACKGROUND) {
try {
tizen.application.getCurrentApplication().exit();
} catch (e) {
// eslint-disable-next-line no-console
console.warn("Cannot exit application", e);
}
}
}
render() {
const { splash } = this.state;
return splash ? <Splash /> : this.props.children;
}
}
SplashHandler.propTypes = {
appLaunched: PropTypes.bool.isRequired,
children: PropTypes.node.isRequired,
};
SplashHandler.shouldShowSplash = true;
SplashHandler.shouldShowSplash = true;
export const SplashLoader = connectToStore(getAppLaunched)(SplashHandler);