From b5f72ad000aaf98c68003f3c627c494993d13513 Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Wed, 26 May 2021 12:25:22 +0500 Subject: [PATCH] animated foreground notificaiton --- .../foregroundNotification.tsx | 166 ++++++++---------- .../foregroundNotification/styles.ts | 9 +- .../application/screen/applicationScreen.js | 3 + 3 files changed, 85 insertions(+), 93 deletions(-) diff --git a/src/components/foregroundNotification/foregroundNotification.tsx b/src/components/foregroundNotification/foregroundNotification.tsx index 3c8378ace..75beb6d1c 100644 --- a/src/components/foregroundNotification/foregroundNotification.tsx +++ b/src/components/foregroundNotification/foregroundNotification.tsx @@ -1,120 +1,108 @@ -import React, { Component } from 'react'; -import { Animated, TouchableOpacity, Text, View } from 'react-native'; +import React, { useEffect, useRef, useState} from 'react'; +import { Animated, Text, View } from 'react-native'; +import { useDispatch } from 'react-redux'; import { IconButton } from '..'; +import { toastNotification } from '../../redux/actions/uiAction'; import UserAvatar from '../userAvatar'; // Styles -import styles from './styles'; +import styles, { CONTAINER_HEIGHT } from './styles'; interface Props { + title:string; text:string; + usename:string; } -const ForegroundNotification = ({text}:Props) => { - const position = {top:0}; + +const ForegroundNotification = ({text, title, username }:Props) => { + let hideTimeout = null; + const dispatch = useDispatch(); + + const [duration] = useState(4000); + const [curText, setCurText] = useState(text); + const [isVisible, setIsVisible] = useState(false); + + + const animatedValue = useRef(new Animated.Value(-CONTAINER_HEIGHT)).current; + + + + useEffect(() => { + if(text !== curText && text !== ''){ + setCurText(text); + show(); + } + return ()=>{ + if(hideTimeout){ + clearTimeout(hideTimeout); + } + } + }, [text]); + + const show = () => { + // Will change fadeAnim value to 1 in 5 seconds + Animated.timing(animatedValue, { + toValue: 0, + duration: 350 + }).start(); + + setIsVisible(true); + + hideTimeout = setTimeout(()=>{ + hide(); + }, duration) + + }; + + const hide = () => { + if(hideTimeout || isVisible){ + // Will change fadeAnim value to 0 in 3 seconds + Animated.timing(animatedValue, { + toValue: -CONTAINER_HEIGHT, + duration: 200 + }).start(()=>{ + dispatch(toastNotification('')) + }); + + setIsVisible(false); + clearTimeout(hideTimeout); + } + }; + + + return ( - - + + + + + {title} + {text} + - - New Upvote - {text} + - + ) } -// class ToastNotification extends Component { - /* Props - * ------------------------------------------------ - * @prop { type } name - Description.... - */ - - // constructor(props) { - // super(props); - // this.state = { - // animatedValue: new Animated.Value(0), - // }; - // } - - // Component Functions - // _showToast() { - // const { duration } = this.props; - // const animatedValue = new Animated.Value(0); - - // this.setState({ animatedValue }); - - // Animated.timing(animatedValue, { toValue: 1, duration: 350 }).start(); - - // if (duration) { - // this.closeTimer = setTimeout(() => { - // this._hideToast(); - // }, duration); - // } - // } - - // _hideToast() { - // const { animatedValue } = this.state; - // const { onHide } = this.props; - - // Animated.timing(animatedValue, { toValue: 0.0, duration: 350 }).start(() => { - // if (onHide) { - // onHide(); - // } - // }); - - // if (this.closeTimer) { - // clearTimeout(this.closeTimer); - // } - // } - - // Component Life Cycles - // UNSAFE_componentWillMount() { - // this._showToast(); - // } - -// render() { -// const { text, textStyle, style, onPress, isTop } = this.props; -// const { animatedValue } = this.state; -// const outputRange = isTop ? [-50, 0] : [50, 0]; -// const y = animatedValue.interpolate({ -// inputRange: [0, 1], -// outputRange, -// }); -// const position = isTop ? { top: 100 } : { bottom: 100 }; - -// return ( -// onPress && onPress()}> -// -// {text} -// -// -// ); -// } -// } export default ForegroundNotification; diff --git a/src/components/foregroundNotification/styles.ts b/src/components/foregroundNotification/styles.ts index f07590aff..e821db2a4 100644 --- a/src/components/foregroundNotification/styles.ts +++ b/src/components/foregroundNotification/styles.ts @@ -1,29 +1,30 @@ import EStyleSheet from 'react-native-extended-stylesheet'; +export const CONTAINER_HEIGHT = 108; + export default EStyleSheet.create({ container: { position: 'absolute', zIndex: 9999, justifyContent: 'flex-end', - alignSelf: 'center', maxWidth: '$deviceWidth', minWidth: '$deviceWidth / 1.9', - height: 100, + height: CONTAINER_HEIGHT, width:'100%', backgroundColor: '$primaryDarkText', - margin: 5, shadowOffset: { height: 5, }, shadowColor: '#5f5f5fbf', shadowOpacity: 0.3, elevation: 3, - marginTop:-4 + top:0, }, text: { color: 'white', fontWeight: 'bold', fontSize: 14, paddingRight: 10, + paddingLeft:16, }, }); diff --git a/src/screens/application/screen/applicationScreen.js b/src/screens/application/screen/applicationScreen.js index 53574baa5..2acecfcb1 100644 --- a/src/screens/application/screen/applicationScreen.js +++ b/src/screens/application/screen/applicationScreen.js @@ -24,6 +24,7 @@ import { NoInternetConnection, AccountsBottomSheet, ActionModal, + ForegroundNotification, } from '../../../components'; // Themes (Styles) @@ -160,6 +161,8 @@ class ApplicationScreen extends Component { onHide={this._handleOnHideToastNotification} /> )} + +