@applicaster/zapp-react-dom-app
Version:
Zapp App Component for Applicaster's Quick Brick React Native App
82 lines (64 loc) • 1.92 kB
JavaScript
// @flow
/* global tizen */
import * as React from "react";
import * as R from "ramda";
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";
type Props = {
appLaunched: boolean;
children: any;
};
const getAppLaunched = R.compose(
R.assoc("appLaunched", R.__, {}),
R.path(["appState", "appLaunched"])
);
class SplashHandler extends React.Component<Props> {
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.shouldShowSplash = true;
export const SplashLoader = connectToStore(getAppLaunched)(SplashHandler);