2017-08-07 00:34:35 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Zeroconf from 'react-native-zeroconf';
|
2017-08-09 01:40:55 +00:00
|
|
|
import { View, Text, SectionList, Button, StyleSheet } from 'react-native';
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
import realm from '../lib/realm';
|
|
|
|
import { connect } from '../lib/meteor';
|
2017-08-07 00:34:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
view: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'stretch'
|
|
|
|
},
|
|
|
|
input: {
|
|
|
|
height: 40,
|
|
|
|
borderColor: '#aaa',
|
|
|
|
margin: 20,
|
|
|
|
padding: 5,
|
|
|
|
borderWidth: 0,
|
|
|
|
backgroundColor: '#f8f8f8'
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
textAlign: 'center',
|
|
|
|
color: '#888'
|
|
|
|
},
|
|
|
|
listItem: {
|
|
|
|
lineHeight: 18,
|
|
|
|
borderTopWidth: 2,
|
|
|
|
color: '#666',
|
|
|
|
padding: 14
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
height: 1,
|
|
|
|
backgroundColor: '#eee'
|
|
|
|
},
|
|
|
|
headerStyle: {
|
|
|
|
backgroundColor: '#eee',
|
|
|
|
lineHeight: 24,
|
|
|
|
paddingLeft: 14,
|
|
|
|
color: '#888'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const zeroconf = new Zeroconf();
|
|
|
|
|
|
|
|
export default class ListServerView extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
2017-08-07 00:34:35 +00:00
|
|
|
title: 'Servers',
|
|
|
|
headerRight: (
|
|
|
|
<Button
|
2017-08-09 01:40:55 +00:00
|
|
|
title='Add'
|
|
|
|
onPress={() => navigation.navigate('NewServer')}
|
2017-08-07 00:34:35 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
sections: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const getState = () => {
|
|
|
|
const sections = [{
|
|
|
|
title: 'My servers',
|
|
|
|
data: realm.objects('servers')
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (this.state.nearBy) {
|
2017-08-09 01:40:55 +00:00
|
|
|
const nearBy = Object.keys(this.state.nearBy)
|
|
|
|
.filter(key => this.state.nearBy[key].addresses);
|
2017-08-07 00:34:35 +00:00
|
|
|
if (nearBy.length) {
|
|
|
|
sections.push({
|
|
|
|
title: 'Nearby',
|
|
|
|
data: nearBy.map((key) => {
|
|
|
|
const server = this.state.nearBy[key];
|
2017-08-09 01:40:55 +00:00
|
|
|
const address = `http://${ server.addresses[0] }:${ server.port }`;
|
2017-08-07 00:34:35 +00:00
|
|
|
return {
|
|
|
|
id: address
|
2017-08-09 01:40:55 +00:00
|
|
|
};
|
2017-08-07 00:34:35 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...this.state,
|
|
|
|
sections
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const { navigation } = this.props;
|
|
|
|
|
|
|
|
if (navigation && navigation.state.params && navigation.state.params.newServer) {
|
|
|
|
return navigation.navigate('Login');
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentServer = realm.objects('servers').filtered('current = true')[0];
|
|
|
|
if (currentServer) {
|
|
|
|
connect(() => {
|
2017-08-09 01:40:55 +00:00
|
|
|
navigation.navigate('Login');
|
2017-08-07 00:34:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
zeroconf.on('update', () => {
|
|
|
|
this.state.nearBy = zeroconf.getServices();
|
|
|
|
this.setState(getState());
|
|
|
|
});
|
|
|
|
zeroconf.scan('http', 'tcp', 'local.');
|
|
|
|
|
|
|
|
realm.addListener('change', () => this.setState(getState()));
|
|
|
|
|
|
|
|
this.state = getState();
|
2017-08-09 01:40:55 +00:00
|
|
|
|
|
|
|
return null;
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onPressItem(item) {
|
|
|
|
const { navigate } = this.props.navigation;
|
|
|
|
realm.write(() => {
|
2017-08-09 01:40:55 +00:00
|
|
|
realm.objects('servers').filtered('current = true').forEach(server => (server.current = false));
|
2017-08-07 00:34:35 +00:00
|
|
|
realm.create('servers', { id: item.id, current: true }, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(() => {
|
|
|
|
navigate('Login');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item }) => (
|
|
|
|
<Text
|
|
|
|
style={styles.listItem}
|
2017-08-09 01:40:55 +00:00
|
|
|
onPress={() => { this.onPressItem(item); }}
|
2017-08-07 00:34:35 +00:00
|
|
|
>
|
|
|
|
{item.id}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
|
|
|
|
renderSectionHeader = ({ section }) => (
|
|
|
|
<Text style={styles.headerStyle}>{section.title}</Text>
|
|
|
|
);
|
|
|
|
|
|
|
|
renderSeparator = () => (
|
|
|
|
<View style={styles.separator} />
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={styles.view}>
|
|
|
|
<SectionList
|
|
|
|
style={styles.list}
|
|
|
|
sections={this.state.sections}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
renderSectionHeader={this.renderSectionHeader}
|
2017-08-09 01:40:55 +00:00
|
|
|
keyExtractor={item => item.id}
|
2017-08-07 00:34:35 +00:00
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|