[NEW] Open room on tap(push notification) (#116)

* fix frozen screen when app return from background (Android)

* Update state.js

* Update state.js

* init task

* fix updateAt

* remove timeout to show banner

* remove unused state saga, and comment about go to room method
This commit is contained in:
Guilherme Gazzo 2017-11-28 15:47:56 -02:00 committed by GitHub
parent 23d2d87bfb
commit 599b7c8368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 41 deletions

View File

@ -30,22 +30,9 @@ export default class Banner extends React.PureComponent {
authenticating: PropTypes.bool, authenticating: PropTypes.bool,
offline: PropTypes.bool offline: PropTypes.bool
} }
componentWillMount() {
this.setState({
slow: false
});
this.timer = setTimeout(() => this.setState({ slow: true }), 5000);
}
componentWillUnmount() {
clearTimeout(this.timer);
}
render() { render() {
const { connecting, authenticating, offline } = this.props; const { connecting, authenticating, offline } = this.props;
if (!this.state.slow) {
return null;
}
if (offline) { if (offline) {
return ( return (
<View style={[styles.bannerContainer, { backgroundColor: 'red' }]}> <View style={[styles.bannerContainer, { backgroundColor: 'red' }]}>

View File

@ -28,7 +28,7 @@ const AuthRoutes = StackNavigator(
screen: RoomView, screen: RoomView,
navigationOptions({ navigation }) { navigationOptions({ navigation }) {
return { return {
title: navigation.state.params.title || 'Room' title: navigation.state.params.name || navigation.state.params.room.name || 'Room'
// [drawerIconPosition]: (<DrawerMenuButton navigation={navigation} />)÷ // [drawerIconPosition]: (<DrawerMenuButton navigation={navigation} />)÷
}; };
} }

View File

@ -21,3 +21,23 @@ export function goBack() {
config.navigator.dispatch(action); config.navigator.dispatch(action);
} }
} }
export function goRoom({ rid, name }, counter = 0) {
// about counter: we can call this method before navigator be set. so we have to wait, if we tried a lot, we give up ...
if (!rid || !name || counter > 10) {
return;
}
if (!config.navigator) {
return setTimeout(() => goRoom({ rid, name }, counter + 1), 200);
}
const action = NavigationActions.reset({
index: 1,
actions: [
NavigationActions.navigate({ routeName: 'RoomsList' }),
NavigationActions.navigate({ routeName: 'Room', params: { room: { rid, name }, rid, name } })
]
});
return config.navigator.dispatch(action);
}

View File

@ -1,6 +1,15 @@
import PushNotification from 'react-native-push-notification'; import PushNotification from 'react-native-push-notification';
import { AsyncStorage } from 'react-native'; import { AsyncStorage } from 'react-native';
import EJSON from 'ejson';
import { goRoom } from './containers/routes/NavigationService';
const handleNotification = (notification) => {
if (notification.usernInteraction) {
return;
}
const { rid, name } = EJSON.parse(notification.ejson);
return rid && name && goRoom({ rid, name });
};
PushNotification.configure({ PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android) // (optional) Called when Token is generated (iOS and Android)
@ -9,9 +18,7 @@ PushNotification.configure({
}, },
// (required) Called when a remote or local notification is opened or received // (required) Called when a remote or local notification is opened or received
onNotification(notification) { onNotification: handleNotification,
console.log('NOTIFICATION:', notification);
},
// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications) // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: '673693445664', senderID: '673693445664',
@ -25,7 +32,7 @@ PushNotification.configure({
// Should the initial notification be popped automatically // Should the initial notification be popped automatically
// default: true // default: true
popInitialNotification: false, popInitialNotification: true,
/** /**
* (optional) default: true * (optional) default: true

View File

@ -22,5 +22,5 @@ const root = function* root() {
state() state()
]); ]);
}; };
// Consider using takeEvery
export default root; export default root;

View File

@ -70,7 +70,6 @@ export default class RoomView extends React.Component {
editCancel: PropTypes.func, editCancel: PropTypes.func,
rid: PropTypes.string, rid: PropTypes.string,
server: PropTypes.string, server: PropTypes.string,
sid: PropTypes.string,
name: PropTypes.string, name: PropTypes.string,
Site_Url: PropTypes.string, Site_Url: PropTypes.string,
Message_TimeFormat: PropTypes.string, Message_TimeFormat: PropTypes.string,
@ -79,12 +78,9 @@ export default class RoomView extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.sid = props.navigation.state.params.room.sid;
this.rid = this.rid =
props.rid || props.rid ||
props.navigation.state.params.room.rid || props.navigation.state.params.room.rid;
realm.objectForPrimaryKey('subscriptions', this.sid).rid;
this.data = realm this.data = realm
.objects('messages') .objects('messages')
@ -92,7 +88,6 @@ export default class RoomView extends React.Component {
.sorted('ts', true); .sorted('ts', true);
this.room = realm.objects('subscriptions').filtered('rid = $0', this.rid); this.room = realm.objects('subscriptions').filtered('rid = $0', this.rid);
this.state = { this.state = {
slow: false,
dataSource: ds.cloneWithRows([]), dataSource: ds.cloneWithRows([]),
loaded: true, loaded: true,
joined: typeof props.rid === 'undefined' joined: typeof props.rid === 'undefined'
@ -103,19 +98,15 @@ export default class RoomView extends React.Component {
this.props.navigation.setParams({ this.props.navigation.setParams({
title: title:
this.props.name || this.props.name ||
this.props.navigation.state.params.room.name || this.props.navigation.state.params.name ||
realm.objectForPrimaryKey('subscriptions', this.sid).name this.props.navigation.state.params.room.name
}); });
this.timer = setTimeout(() => this.setState({ slow: true }), 5000);
this.props.openRoom({ rid: this.rid }); this.props.openRoom({ rid: this.rid });
this.data.addListener(this.updateState); this.data.addListener(this.updateState);
} }
componentDidMount() { componentDidMount() {
this.updateState(); this.updateState();
} }
componentDidUpdate() {
return !this.props.loading && clearTimeout(this.timer);
}
componentWillUnmount() { componentWillUnmount() {
clearTimeout(this.timer); clearTimeout(this.timer);
this.data.removeAllListeners(); this.data.removeAllListeners();
@ -160,7 +151,7 @@ export default class RoomView extends React.Component {
}; };
renderBanner = () => renderBanner = () =>
(this.state.slow && this.props.loading ? ( (this.props.loading ? (
<View style={styles.bannerContainer}> <View style={styles.bannerContainer}>
<Text style={styles.bannerText}>Loading new messages...</Text> <Text style={styles.bannerText}>Loading new messages...</Text>
</View> </View>

View File

@ -11,6 +11,7 @@ import realm from '../lib/realm';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
import RoomItem from '../presentation/RoomItem'; import RoomItem from '../presentation/RoomItem';
import Banner from '../containers/Banner'; import Banner from '../containers/Banner';
import { goRoom } from '../containers/routes/NavigationService';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
@ -191,11 +192,7 @@ export default class RoomsListView extends React.Component {
}); });
}; };
_onPressItem = (id, item = {}) => { _onPressItem = (item = {}) => {
const navigateToRoom = (room) => {
this.props.navigation.navigate('Room', { room, title: room.name });
};
const clearSearch = () => { const clearSearch = () => {
this.setState({ this.setState({
searchText: '' searchText: ''
@ -220,16 +217,16 @@ export default class RoomsListView extends React.Component {
} }
}); });
})) }))
.then(sub => navigateToRoom({ sid: sub._id, name: sub.name })) .then(sub => goRoom({ room: sub, name: sub.name }))
.then(() => clearSearch()); .then(() => clearSearch());
} else { } else {
clearSearch(); clearSearch();
navigateToRoom({ rid: item._id, name: item.name }); goRoom(item);
} }
return; return;
} }
navigateToRoom({ sid: id, name: item.name }); goRoom(item);
clearSearch(); clearSearch();
} }
@ -264,7 +261,7 @@ export default class RoomsListView extends React.Component {
type={item.t} type={item.t}
baseUrl={this.props.Site_Url} baseUrl={this.props.Site_Url}
dateFormat='MM-DD-YYYY HH:mm:ss' dateFormat='MM-DD-YYYY HH:mm:ss'
onPress={() => this._onPressItem(item._id, item)} onPress={() => this._onPressItem(item)}
/> />
) )

View File

@ -19,6 +19,7 @@
"babel-plugin-transform-decorators-legacy": "^1.3.4", "babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-remove-console": "^6.8.5", "babel-plugin-transform-remove-console": "^6.8.5",
"babel-polyfill": "^6.26.0", "babel-polyfill": "^6.26.0",
"ejson": "^2.1.2",
"moment": "^2.19.2", "moment": "^2.19.2",
"prop-types": "^15.6.0", "prop-types": "^15.6.0",
"react": "16.1.1", "react": "16.1.1",