import * as React from "react"; import {Animated, StyleProp, StyleSheet, TextStyle, View} from "react-native"; import {reduxTools} from '@yoronsoft/js-utils'; import {configEmitterParams, popupDirection, configName} from "./config"; import {IProps, PopupDirectionData, PopupEmitterParams} from "./popup"; import PopupLayerView, {popupLayer} from "../popupLayer"; import app, {ThemeCss} from "../app"; const useEmitterParams = () => { const [anOpacity] = React.useState(new Animated.Value(0)); const [anXY, setAnXY] = React.useState(new Animated.ValueXY({x: 0, y: 0})); const [direction, setDirection] = React.useState(popupDirection.bottom); const [space, setSpace] = React.useState(0); const [isAnimated, setIsAnimated] = React.useState(false); const [value, setValue] = React.useState(undefined); React.useEffect(() => { const emitterData = app.emitter.get(configEmitterParams, (res: PopupEmitterParams) => { setValue(res.value); setIsAnimated(res.display); setSpace(res.space); setDirection(res.direction); if (res.display) setAnXY(new Animated.ValueXY(useDirectionParams(res.direction))) }) return () => emitterData.remove(); }, []) return { anOpacity, anXY, isAnimated, direction, space, value, } } const useDirectionParams = (direction: PopupDirectionData): { x: number, y: number } => { let value = {x: 0, y: 0}; if (direction === popupDirection.top) value = {x: 0, y: -50}; else if (direction === popupDirection.bottom) value = {x: 0, y: 50}; else if (direction === popupDirection.left) value = {x: -50, y: 0}; else if (direction === popupDirection.right) value = {x: 50, y: 0}; return value; } const getAnimatedDirection = (direction: PopupDirectionData) => { let value = {}; if (direction === popupDirection.top) value = {top: 0, left: 0, right: 0}; else if (direction === popupDirection.bottom) value = {bottom: 0, left: 0, right: 0}; else if (direction === popupDirection.left) value = {bottom: 0, left: 0, top: 0}; else if (direction === popupDirection.right) value = {bottom: 0, right: 0, top: 0}; return value; } const PopupInit: React.FC = () => { const css = app.theme.useGet(); const styles = styleSheet(css); const paramsData = useEmitterParams(); React.useEffect(() => { const emitterData = app.emitter.get(configEmitterParams, (res: PopupEmitterParams) => { reduxTools.update(configEmitterParams, res); if (res.display) popupLayer.show(configName); else popupLayer.hide(configName); }) return () => emitterData.remove(); }, []) let popupStyle: StyleProp = { position: 'absolute', padding: paramsData.space ?? 0, width: css.width, ...getAnimatedDirection(paramsData.direction), }; const direction = paramsData.direction; return {paramsData.value} } const styleSheet = (css: ThemeCss) => StyleSheet.create({ view: { flex: 1, position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, zIndex: 20, width: css.width, }, }); export default PopupInit