2017-08-07 00:34:35 +00:00
|
|
|
import React from 'react';
|
2017-08-21 00:11:46 +00:00
|
|
|
|
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2017-08-07 00:34:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-04-24 19:34:03 +00:00
|
|
|
// import Zeroconf from 'react-native-zeroconf';
|
2017-11-13 12:49:19 +00:00
|
|
|
import { View, Text, SectionList, StyleSheet, SafeAreaView } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
|
|
|
import LoggedView from './View';
|
2017-08-18 21:30:16 +00:00
|
|
|
import { setServer } from '../actions/server';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../lib/realm';
|
2017-08-21 00:11:46 +00:00
|
|
|
import Fade from '../animations/fade';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Touch from '../utils/touch';
|
2017-08-07 00:34:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
view: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center',
|
2017-08-12 20:52:55 +00:00
|
|
|
alignItems: 'stretch',
|
|
|
|
backgroundColor: '#fff'
|
2017-08-07 00:34:35 +00:00
|
|
|
},
|
|
|
|
input: {
|
|
|
|
height: 40,
|
|
|
|
borderColor: '#aaa',
|
|
|
|
margin: 20,
|
|
|
|
padding: 5,
|
|
|
|
borderWidth: 0,
|
|
|
|
backgroundColor: '#f8f8f8'
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
textAlign: 'center',
|
|
|
|
color: '#888'
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
height: 1,
|
|
|
|
backgroundColor: '#eee'
|
|
|
|
},
|
|
|
|
headerStyle: {
|
|
|
|
backgroundColor: '#eee',
|
|
|
|
lineHeight: 24,
|
|
|
|
paddingLeft: 14,
|
|
|
|
color: '#888'
|
2017-08-21 00:11:46 +00:00
|
|
|
},
|
|
|
|
serverItem: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
padding: 14
|
|
|
|
},
|
|
|
|
listItem: {
|
2018-04-24 19:34:03 +00:00
|
|
|
color: '#666',
|
|
|
|
flexGrow: 1,
|
|
|
|
lineHeight: 30
|
2017-08-21 00:11:46 +00:00
|
|
|
},
|
|
|
|
serverChecked: {
|
|
|
|
flexGrow: 0
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
// const zeroconf = new Zeroconf();
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-08-13 01:35:09 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
2017-09-01 19:42:50 +00:00
|
|
|
server: state.server.server,
|
2017-09-21 17:08:00 +00:00
|
|
|
login: state.login,
|
|
|
|
connected: state.meteor.connected
|
2017-08-13 01:35:09 +00:00
|
|
|
}), dispatch => ({
|
2017-08-18 21:30:16 +00:00
|
|
|
selectServer: server => dispatch(setServer(server))
|
2017-08-13 01:35:09 +00:00
|
|
|
}))
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class ListServerView extends LoggedView {
|
2017-08-07 00:34:35 +00:00
|
|
|
static propTypes = {
|
2017-09-21 17:08:00 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2017-09-01 19:42:50 +00:00
|
|
|
login: PropTypes.object.isRequired,
|
2017-08-22 01:24:41 +00:00
|
|
|
selectServer: PropTypes.func.isRequired,
|
2017-09-21 17:08:00 +00:00
|
|
|
connected: PropTypes.bool.isRequired,
|
2017-08-13 01:35:09 +00:00
|
|
|
server: PropTypes.string
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('ListServerView', props);
|
2017-08-07 00:34:35 +00:00
|
|
|
this.state = {
|
|
|
|
sections: []
|
|
|
|
};
|
2017-12-27 15:22:06 +00:00
|
|
|
this.data = database.databases.serversDB.objects('servers');
|
2018-04-24 19:34:03 +00:00
|
|
|
// this.redirected = false;
|
2017-12-27 15:22:06 +00:00
|
|
|
this.data.addListener(this.updateState);
|
2017-08-09 16:19:17 +00:00
|
|
|
}
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2018-03-06 17:40:44 +00:00
|
|
|
componentDidMount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
// zeroconf.on('update', this.updateState);
|
|
|
|
// zeroconf.scan('http', 'tcp', 'local.');
|
|
|
|
this.updateState();
|
|
|
|
this.jumpToSelectedServer();
|
2018-03-06 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
// componentDidUpdate() {
|
|
|
|
// if (this.props.connected &&
|
|
|
|
// this.props.server &&
|
|
|
|
// !this.props.login.token &&
|
|
|
|
// !this.redirected) {
|
|
|
|
// this.redirected = true;
|
|
|
|
// this.props.navigation.navigate({ key: 'LoginSignup', routeName: 'LoginSignup' });
|
|
|
|
// } else if (!this.props.connected) {
|
|
|
|
// this.redirected = false;
|
|
|
|
// }
|
|
|
|
// }
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2017-08-09 16:19:17 +00:00
|
|
|
componentWillUnmount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
// zeroconf.stop();
|
2017-12-27 15:22:06 +00:00
|
|
|
this.data.removeAllListeners();
|
2018-04-24 19:34:03 +00:00
|
|
|
// zeroconf.removeListener('update', this.updateState);
|
|
|
|
}
|
|
|
|
|
|
|
|
openLogin = () => {
|
|
|
|
this.props.navigation.navigate({ key: 'LoginSignup', routeName: 'LoginSignup' });
|
|
|
|
}
|
|
|
|
|
|
|
|
selectAndNavigateTo = (server) => {
|
|
|
|
this.props.selectServer(server);
|
|
|
|
this.openLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
jumpToSelectedServer() {
|
|
|
|
if (this.props.server && !this.props.login.isRegistering) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.openLogin();
|
|
|
|
}, 300);
|
|
|
|
}
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 20:52:55 +00:00
|
|
|
onPressItem = (item) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.selectAndNavigateTo(item.id);
|
2017-08-09 16:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getState = () => {
|
|
|
|
const sections = [{
|
|
|
|
title: 'My servers',
|
2017-12-27 15:22:06 +00:00
|
|
|
data: this.data
|
2017-08-09 16:19:17 +00:00
|
|
|
}];
|
2018-04-24 19:34:03 +00:00
|
|
|
//
|
|
|
|
// this.state.nearBy = zeroconf.getServices();
|
|
|
|
// if (this.state.nearBy) {
|
|
|
|
// const nearBy = Object.keys(this.state.nearBy)
|
|
|
|
// .filter(key => this.state.nearBy[key].addresses);
|
|
|
|
// if (nearBy.length) {
|
|
|
|
// sections.push({
|
|
|
|
// title: 'Nearby',
|
|
|
|
// data: nearBy.map((key) => {
|
|
|
|
// const server = this.state.nearBy[key];
|
|
|
|
// const address = `http://${ server.addresses[0] }:${ server.port }`;
|
|
|
|
// return {
|
|
|
|
// id: address
|
|
|
|
// };
|
|
|
|
// })
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
2017-08-09 16:19:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...this.state,
|
|
|
|
sections
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
updateState = () => {
|
|
|
|
this.setState(this.getState());
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item }) => (
|
2018-04-24 19:34:03 +00:00
|
|
|
<Touch
|
|
|
|
underlayColor='#ccc'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
onPress={() => { this.onPressItem(item); }}
|
|
|
|
>
|
|
|
|
<View style={styles.serverItem}>
|
|
|
|
<Text
|
|
|
|
style={[styles.listItem]}
|
|
|
|
adjustsFontSizeToFit
|
|
|
|
>
|
|
|
|
{item.id}
|
|
|
|
</Text>
|
|
|
|
<Fade visible={this.props.server === item.id}>
|
|
|
|
<Icon
|
|
|
|
iconSize={24}
|
|
|
|
size={24}
|
|
|
|
style={styles.serverChecked}
|
|
|
|
name='ios-checkmark-circle-outline'
|
|
|
|
/>
|
|
|
|
</Fade>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
2017-08-07 00:34:35 +00:00
|
|
|
);
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
|
2017-08-07 00:34:35 +00:00
|
|
|
renderSectionHeader = ({ section }) => (
|
|
|
|
<Text style={styles.headerStyle}>{section.title}</Text>
|
|
|
|
);
|
|
|
|
|
|
|
|
renderSeparator = () => (
|
|
|
|
<View style={styles.separator} />
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<SafeAreaView style={styles.view}>
|
|
|
|
<SectionList
|
|
|
|
style={styles.list}
|
|
|
|
sections={this.state.sections}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
renderSectionHeader={this.renderSectionHeader}
|
|
|
|
keyExtractor={item => item.id}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
2017-08-07 00:34:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|