2019-06-10 16:23:19 +00:00
|
|
|
import React from 'react';
|
|
|
|
import {
|
|
|
|
View, Text, StyleSheet, TouchableOpacity, Animated, Easing
|
|
|
|
} from 'react-native';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import equal from 'deep-equal';
|
|
|
|
import { responsive } from 'react-native-responsive-ui';
|
|
|
|
import Touchable from 'react-native-platform-touchable';
|
|
|
|
|
2020-05-08 16:37:49 +00:00
|
|
|
import { hasNotch, isIOS, isTablet } from '../../utils/deviceInfo';
|
2019-06-10 16:23:19 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-06-10 16:23:19 +00:00
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import { removeNotification as removeNotificationAction } from '../../actions/notification';
|
|
|
|
import sharedStyles from '../../views/Styles';
|
|
|
|
import { ROW_HEIGHT } from '../../presentation/RoomItem';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../../theme';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2019-06-10 16:23:19 +00:00
|
|
|
|
|
|
|
const AVATAR_SIZE = 48;
|
|
|
|
const ANIMATION_DURATION = 300;
|
|
|
|
const NOTIFICATION_DURATION = 3000;
|
|
|
|
const BUTTON_HIT_SLOP = {
|
|
|
|
top: 12, right: 12, bottom: 12, left: 12
|
|
|
|
};
|
|
|
|
const ANIMATION_PROPS = {
|
|
|
|
duration: ANIMATION_DURATION,
|
|
|
|
easing: Easing.inOut(Easing.quad),
|
|
|
|
useNativeDriver: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
height: ROW_HEIGHT,
|
|
|
|
paddingHorizontal: 14,
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
position: 'absolute',
|
|
|
|
zIndex: 2,
|
|
|
|
width: '100%',
|
2019-12-04 16:39:53 +00:00
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth
|
2019-06-10 16:23:19 +00:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2019-11-19 17:05:08 +00:00
|
|
|
inner: {
|
|
|
|
flex: 1
|
|
|
|
},
|
2019-06-10 16:23:19 +00:00
|
|
|
avatar: {
|
|
|
|
marginRight: 10
|
|
|
|
},
|
|
|
|
roomName: {
|
|
|
|
fontSize: 17,
|
|
|
|
lineHeight: 20,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
fontSize: 14,
|
|
|
|
lineHeight: 17,
|
2019-12-04 16:39:53 +00:00
|
|
|
...sharedStyles.textRegular
|
2019-06-10 16:23:19 +00:00
|
|
|
},
|
|
|
|
close: {
|
|
|
|
marginLeft: 10
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NotificationBadge extends React.Component {
|
2019-06-10 16:23:19 +00:00
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
2020-02-11 14:09:14 +00:00
|
|
|
user: PropTypes.object,
|
2019-06-10 16:23:19 +00:00
|
|
|
notification: PropTypes.object,
|
|
|
|
window: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
removeNotification: PropTypes.func,
|
|
|
|
theme: PropTypes.string
|
2019-06-10 16:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.animatedValue = new Animated.Value(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
const { notification: nextNotification } = nextProps;
|
|
|
|
const {
|
2019-12-04 16:39:53 +00:00
|
|
|
notification: { payload }, window, theme
|
2019-06-10 16:23:19 +00:00
|
|
|
} = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-10 16:23:19 +00:00
|
|
|
if (!equal(nextNotification.payload, payload)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.window.width !== window.width) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { notification: { payload }, navigation } = this.props;
|
|
|
|
const navState = this.getNavState(navigation.state);
|
|
|
|
if (payload.rid) {
|
|
|
|
if (navState && navState.routeName === 'RoomView' && navState.params && navState.params.rid === payload.rid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.clearTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
show = () => {
|
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue: 1,
|
|
|
|
...ANIMATION_PROPS
|
2019-10-31 16:21:59 +00:00
|
|
|
}
|
2019-06-10 16:23:19 +00:00
|
|
|
).start(() => {
|
|
|
|
this.clearTimeout();
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.hide();
|
|
|
|
}, NOTIFICATION_DURATION);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hide = () => {
|
|
|
|
const { removeNotification } = this.props;
|
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue: 0,
|
|
|
|
...ANIMATION_PROPS
|
2019-10-31 16:21:59 +00:00
|
|
|
}
|
2019-06-10 16:23:19 +00:00
|
|
|
).start();
|
|
|
|
setTimeout(removeNotification, ANIMATION_DURATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout = () => {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getNavState = (routes) => {
|
|
|
|
if (!routes.routes) {
|
|
|
|
return routes;
|
|
|
|
}
|
|
|
|
return this.getNavState(routes.routes[routes.index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
goToRoom = async() => {
|
2020-02-12 17:21:11 +00:00
|
|
|
const { notification, navigation, baseUrl } = this.props;
|
|
|
|
const { payload } = notification;
|
2019-06-10 16:23:19 +00:00
|
|
|
const { rid, type, prid } = payload;
|
|
|
|
if (!rid) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-19 17:05:08 +00:00
|
|
|
const name = type === 'd' ? payload.sender.username : payload.name;
|
2020-02-12 17:21:11 +00:00
|
|
|
// if sub is not on local database, title will be null, so we use payload from notification
|
|
|
|
const { title = name } = notification;
|
2019-06-10 16:23:19 +00:00
|
|
|
await navigation.navigate('RoomsListView');
|
|
|
|
navigation.navigate('RoomView', {
|
2020-02-12 17:21:11 +00:00
|
|
|
rid, name: title, t: type, prid, baseUrl
|
2019-06-10 16:23:19 +00:00
|
|
|
});
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl, user: { id: userId, token }, notification, window, theme
|
2019-06-10 16:23:19 +00:00
|
|
|
} = this.props;
|
|
|
|
const { message, payload } = notification;
|
|
|
|
const { type } = payload;
|
2019-11-19 17:05:08 +00:00
|
|
|
const name = type === 'd' ? payload.sender.username : payload.name;
|
2020-02-12 17:21:11 +00:00
|
|
|
// if sub is not on local database, title and avatar will be null, so we use payload from notification
|
|
|
|
const { title = name, avatar = name } = notification;
|
2019-06-10 16:23:19 +00:00
|
|
|
|
|
|
|
let top = 0;
|
|
|
|
if (isIOS) {
|
|
|
|
const portrait = window.height > window.width;
|
|
|
|
if (portrait) {
|
2020-05-08 16:37:49 +00:00
|
|
|
top = hasNotch ? 45 : 20;
|
2019-06-10 16:23:19 +00:00
|
|
|
} else {
|
2019-11-25 20:01:17 +00:00
|
|
|
top = isTablet ? 20 : 0;
|
2019-06-10 16:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const translateY = this.animatedValue.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [-top - ROW_HEIGHT, top]
|
|
|
|
});
|
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<Animated.View
|
|
|
|
style={[
|
|
|
|
styles.container,
|
|
|
|
{
|
|
|
|
transform: [{ translateY }],
|
|
|
|
backgroundColor: themes[theme].focusedBackground,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
>
|
2019-06-10 16:23:19 +00:00
|
|
|
<Touchable
|
|
|
|
style={styles.content}
|
|
|
|
onPress={this.goToRoom}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
|
|
|
background={Touchable.SelectableBackgroundBorderless()}
|
|
|
|
>
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2020-02-12 17:21:11 +00:00
|
|
|
<Avatar text={avatar} size={AVATAR_SIZE} type={type} baseUrl={baseUrl} style={styles.avatar} userId={userId} token={token} />
|
2019-11-19 17:05:08 +00:00
|
|
|
<View style={styles.inner}>
|
2020-02-12 17:21:11 +00:00
|
|
|
<Text style={[styles.roomName, { color: themes[theme].titleText }]} numberOfLines={1}>{title}</Text>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.message, { color: themes[theme].titleText }]} numberOfLines={1}>{message}</Text>
|
2019-06-10 16:23:19 +00:00
|
|
|
</View>
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2019-06-10 16:23:19 +00:00
|
|
|
</Touchable>
|
|
|
|
<TouchableOpacity onPress={this.hide}>
|
2020-06-05 13:28:58 +00:00
|
|
|
<CustomIcon name='cancel' style={[styles.close, { color: themes[theme].titleText }]} size={20} />
|
2019-06-10 16:23:19 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
</Animated.View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
baseUrl: state.server.server,
|
2019-08-07 13:51:34 +00:00
|
|
|
notification: state.notification
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
removeNotification: () => dispatch(removeNotificationAction())
|
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default responsive(connect(mapStateToProps, mapDispatchToProps)(withTheme(NotificationBadge)));
|