Skip to content

Commit

Permalink
feat(devtools): added network configuration UI on RN and binding JS -…
Browse files Browse the repository at this point in the history
…> Java -> Go
  • Loading branch information
aeddi committed Nov 6, 2018
1 parent 144a7bb commit 9aac7f8
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 105 deletions.
@@ -1,2 +1,2 @@
#Tue Oct 23 12:06:28 CEST 2018
#Mon Nov 05 13:28:04 CET 2018
connection.project.dir=
@@ -1,2 +1,2 @@
#Tue Oct 23 12:06:28 CEST 2018
#Mon Nov 05 13:28:04 CET 2018
connection.project.dir=..
Expand Up @@ -52,7 +52,29 @@ public void getPort(Promise promise) {
Long data = Core.getPort();
promise.resolve(data.toString());
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to get port :%s", err);
this.logger.format(Level.ERROR, this.getName(), "Unable to get port: %s", err);
promise.reject(err);
}
}

@ReactMethod
public void getNetworkConfig(Promise promise) {
try {
String config = Core.getNetworkConfig();
promise.resolve(config);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to get network config: %s", err);
promise.reject(err);
}
}

@ReactMethod
public void updateNetworkConfig(String config, Promise promise) {
try {
Core.updateNetworkConfig(config);
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to update network config: %s", err);
promise.reject(err);
}
}
Expand Down
@@ -0,0 +1,192 @@
import React, { PureComponent } from 'react'
import { Switch, NativeModules } from 'react-native'
import { Menu, Header } from '../../../../Library'

export default class Network extends PureComponent {
static navigationOptions = ({ navigation }) => {
return {
header: (
<Header
navigation={navigation}
title='Network configuration'
titleIcon='sliders'
rightBtnIcon={'save'}
onPressRightBtn={
navigation.state.params && navigation.state.params.updateConfig
}
backBtn
/>
),
}
}

state = {
loaded: false,
default_trans: false,
bluetooth_trans: false,
default_bootstrap: false,
custom_bootstrap: [],
mdns: false,
relay: false,
}

currentConfig = undefined

hasConfigChanged = () => {
if (
this.currentConfig !== undefined &&
(this.state.default_trans !== this.currentConfig.DefaultTransport ||
this.state.bluetooth_trans !== this.currentConfig.BluetoothTransport ||
this.state.default_bootstrap !== this.currentConfig.DefaultBootstrap ||
this.state.custom_bootstrap !== this.currentConfig.CustomBootstrap ||
this.state.mdns !== this.currentConfig.MDNS ||
this.state.relay !== this.currentConfig.Relay)
) {
this.props.navigation.setParams({
updateConfig: this.updateConfig,
})
} else {
this.props.navigation.setParams({
updateConfig: undefined,
})
}
}

updateConfig = async () => {
try {
let config = {
DefaultTransport: this.state.default_trans,
BluetoothTransport: this.state.bluetooth_trans,
DefaultBootstrap: this.state.default_bootstrap,
CustomBootstrap: this.state.custom_bootstrap,
MDNS: this.state.mdns,
Relay: this.state.relay,
}
let json = JSON.stringify(config)

await NativeModules.CoreModule.updateNetworkConfig(json)

this.currentConfig = config
this.props.navigation.setParams({
updateConfig: undefined,
})
} catch (err) {
console.error(err)
}
}

getCurrentConfig = async () => {
try {
let json = await NativeModules.CoreModule.getNetworkConfig()
this.currentConfig = JSON.parse(json)

this.setState({
loaded: true,
default_trans: this.currentConfig.DefaultTransport,
bluetooth_trans: this.currentConfig.BluetoothTransport,
default_bootstrap: this.currentConfig.DefaultBootstrap,
custom_bootstrap: this.currentConfig.CustomBootstrap,
mdns: this.currentConfig.MDNS,
relay: this.currentConfig.Relay,
})
} catch (err) {
console.error(err)
}
}

componentDidMount () {
this.getCurrentConfig()
}

render () {
return (
<Menu>
<Menu.Section title='Transports' customMarginTop={24}>
<Menu.Item
title='Default (TCP and Websocket)'
customRight={
<Switch
justify='end'
disabled={!this.state.loaded}
value={this.state.default_trans}
onValueChange={value => {
this.setState({ default_trans: value }, () => {
this.hasConfigChanged()
})
}}
/>
}
/>
<Menu.Item
title='Bluetooth (crash if On -> Off ; #514)'
customRight={
<Switch
justify='end'
disabled={!this.state.loaded}
value={this.state.bluetooth_trans}
onValueChange={value => {
this.setState({ bluetooth_trans: value }, () => {
this.hasConfigChanged()
})
}}
/>
}
/>
</Menu.Section>
<Menu.Section title='Bootstrap'>
<Menu.Item
title='Default bootstrap'
customRight={
<Switch
justify='end'
disabled={!this.state.loaded}
value={this.state.default_bootstrap}
onValueChange={value => {
this.setState({ default_bootstrap: value }, () => {
this.hasConfigChanged()
})
}}
/>
}
/>
<Menu.Item
title='Custom bootstrap (not implem. yet)'
onPress={() => this.hasConfigChanged()}
/>
</Menu.Section>
<Menu.Section title='Miscellaneous'>
<Menu.Item
title='Multicast DNS'
customRight={
<Switch
justify='end'
disabled={!this.state.loaded}
value={this.state.mdns}
onValueChange={value => {
this.setState({ mdns: value }, () => {
this.hasConfigChanged()
})
}}
/>
}
/>
<Menu.Item
title='Berty relay'
customRight={
<Switch
justify='end'
disabled={!this.state.loaded}
value={this.state.relay}
onValueChange={value => {
this.setState({ relay: value }, () => {
this.hasConfigChanged()
})
}}
/>
}
/>
</Menu.Section>
</Menu>
)
}
}
@@ -1,5 +1,6 @@
import React, { PureComponent } from 'react'
import { Menu, Header } from '../../../Library'
import { Platform } from 'react-native'
import { Menu, Header } from '../../../../Library'

export default class Network extends PureComponent {
static navigationOptions = ({ navigation }) => ({
Expand All @@ -13,17 +14,29 @@ export default class Network extends PureComponent {
),
})
render () {
const { navigation } = this.props
return (
<Menu>
<Menu.Section customMarginTop={1}>
<Menu.Item
icon='list'
title='List peers (not implemented)'
title='List peers'
onPress={() => {
console.log('List')
navigation.push('network/peers')
}}
/>
</Menu.Section>
{Platform.OS !== 'web' && (
<Menu.Section>
<Menu.Item
icon='sliders'
title='Network configuration'
onPress={() => {
navigation.push('network/config')
}}
/>
</Menu.Section>
)}
</Menu>
)
}
Expand Down
@@ -0,0 +1,15 @@
import { createSubStackNavigator } from '../../../../../helpers/react-navigation'
import Network from './Network'
import Peers from './Peers'
import Config from './Config'

export default createSubStackNavigator(
{
'network/list': Network,
'network/config': Config,
'network/peers': Peers,
},
{
initialRouteName: 'network/list',
}
)

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 9aac7f8

Please sign in to comment.