Add loading banners

This commit is contained in:
Rodrigo Nascimento 2017-08-09 17:08:50 -03:00
parent f131287182
commit 8d52209935
5 changed files with 64 additions and 20 deletions

View File

@ -9,10 +9,11 @@ const styles = StyleSheet.create({
alignItems: 'center' alignItems: 'center'
}, },
number: { number: {
width: 20, minWidth: 20,
lineHeight: 20, fontSize: 14,
padding: 2,
borderRadius: 5, borderRadius: 5,
backgroundColor: 'green', backgroundColor: '#aaa',
color: '#fff', color: '#fff',
textAlign: 'center', textAlign: 'center',
overflow: 'hidden', overflow: 'hidden',
@ -47,9 +48,15 @@ export default class RoomItem extends React.PureComponent {
} }
render() { render() {
let name = this.props.item.name;
if (this.props.item.t === 'd') {
name = `@ ${ name }`;
} else {
name = `# ${ name }`;
}
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text onPress={this._onPress} style={styles.roomItem}>{ this.props.item.name }</Text> <Text onPress={this._onPress} style={styles.roomItem}>{ name }</Text>
{this.renderNumber(this.props.item)} {this.renderNumber(this.props.item)}
</View> </View>
); );

View File

@ -120,7 +120,7 @@ export function loadSubscriptions(cb) {
}); });
} }
export function loadMessagesForRoom(rid) { export function loadMessagesForRoom(rid, cb) {
Meteor.call('loadHistory', rid, null, 50, (err, data) => { Meteor.call('loadHistory', rid, null, 50, (err, data) => {
if (err) { if (err) {
console.error(err); console.error(err);
@ -133,6 +133,10 @@ export function loadMessagesForRoom(rid) {
realm.create('messages', message, true); realm.create('messages', message, true);
}); });
}); });
if (cb) {
cb();
}
}); });
Meteor.subscribe('stream-room-messages', rid, false); Meteor.subscribe('stream-room-messages', rid, false);

View File

@ -65,7 +65,7 @@ const messagesSchema = {
u: 'users', u: 'users',
// mentions: [], // mentions: [],
// channels: [], // channels: [],
_updatedAt: 'date', _updatedAt: { type: 'date', optional: true },
temp: { type: 'bool', optional: true } temp: { type: 'bool', optional: true }
} }
}; };

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { View, FlatList, StyleSheet } from 'react-native'; import { Text, View, FlatList, StyleSheet } from 'react-native';
// import Markdown from 'react-native-simple-markdown'; // import Markdown from 'react-native-simple-markdown';
import realm from '../lib/realm'; import realm from '../lib/realm';
import RocketChat, { loadMessagesForRoom, sendMessage } from '../lib/meteor'; import RocketChat, { loadMessagesForRoom, sendMessage } from '../lib/meteor';
@ -19,9 +19,15 @@ const styles = StyleSheet.create({
}, },
separator: { separator: {
height: 1, height: 1,
// width: "86%",
backgroundColor: '#CED0CE' backgroundColor: '#CED0CE'
// marginLeft: "14%" },
bannerContainer: {
backgroundColor: 'orange'
},
bannerText: {
margin: 5,
textAlign: 'center',
color: '#a00'
} }
}); });
@ -40,14 +46,20 @@ export default class RoomView extends React.Component {
// this.rid = 'GENERAL'; // this.rid = 'GENERAL';
this.state = { this.state = {
dataSource: this.getMessages() dataSource: this.getMessages(),
loaded: false
}; };
this.url = realm.objectForPrimaryKey('settings', 'Site_Url').value; this.url = realm.objectForPrimaryKey('settings', 'Site_Url').value;
} }
componentWillMount() { componentWillMount() {
loadMessagesForRoom(this.rid); loadMessagesForRoom(this.rid, () => {
this.setState({
...this.state,
loaded: true
});
});
realm.addListener('change', this.updateState); realm.addListener('change', this.updateState);
} }
@ -59,6 +71,7 @@ export default class RoomView extends React.Component {
updateState = () => { updateState = () => {
this.setState({ this.setState({
...this.state,
dataSource: this.getMessages() dataSource: this.getMessages()
}); });
}; };
@ -77,9 +90,20 @@ export default class RoomView extends React.Component {
/> />
); );
renderBanner = () => {
if (this.state.loaded === false) {
return (
<View style={styles.bannerContainer}>
<Text style={styles.bannerText}>Loading new messages...</Text>
</View>
);
}
}
render() { render() {
return ( return (
<KeyboardView style={styles.container} keyboardVerticalOffset={64}> <KeyboardView style={styles.container} keyboardVerticalOffset={64}>
{this.renderBanner()}
<FlatList <FlatList
ref={ref => this.listView = ref} ref={ref => this.listView = ref}
style={styles.list} style={styles.list}

View File

@ -20,6 +20,11 @@ const styles = StyleSheet.create({
list: { list: {
width: '100%' width: '100%'
}, },
emptyView: {
flexGrow: 1,
alignItems: 'stretch',
justifyContent: 'center'
},
emptyText: { emptyText: {
textAlign: 'center', textAlign: 'center',
fontSize: 18, fontSize: 18,
@ -29,8 +34,8 @@ const styles = StyleSheet.create({
backgroundColor: '#ddd' backgroundColor: '#ddd'
}, },
bannerText: { bannerText: {
lineHeight: 28, textAlign: 'center',
textAlign: 'center' margin: 5
} }
}); });
@ -107,18 +112,20 @@ export default class RoomsListView extends React.Component {
} }
renderBanner = () => { renderBanner = () => {
if (Meteor.getData().ddp.status !== 'connected') { const status = Meteor.getData() && Meteor.getData().ddp && Meteor.getData().ddp.status;
if (status === 'disconnected') {
return ( return (
<View style={styles.bannerContainer}> <View style={[styles.bannerContainer, { backgroundColor: '#0d0' }]}>
<Text style={styles.bannerText}>Connecting...</Text> <Text style={[styles.bannerText, { color: '#fff' }]}>Connecting...</Text>
</View> </View>
); );
} }
if (Meteor._isLoggingIn) { if (status === 'connected' && Meteor._isLoggingIn) {
return ( return (
<View style={styles.bannerContainer}> <View style={[styles.bannerContainer, { backgroundColor: 'orange' }]}>
<Text style={styles.bannerText}>Loggining...</Text> <Text style={[styles.bannerText, { color: '#a00' }]}>Authenticating...</Text>
</View> </View>
) )
} }
@ -150,7 +157,9 @@ export default class RoomsListView extends React.Component {
} }
return ( return (
<View style={styles.emptyView}>
<Text style={styles.emptyText}>No rooms</Text> <Text style={styles.emptyText}>No rooms</Text>
</View>
); );
} }