Skip to content

Commit

Permalink
planetary network fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LennertDefauw committed Apr 6, 2022
1 parent aa4712c commit e27045c
Showing 1 changed file with 47 additions and 59 deletions.
106 changes: 47 additions & 59 deletions app/lib/screens/planetary_network_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,13 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
print(_vpnState.ipAddress);

_ipAddress = "IP Address: " + _vpnState.ipAddress;
_statusMessage = new Text('Connected',
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 16));
_statusMessage =
new Text('Connected', style: TextStyle(color: Colors.green, fontWeight: FontWeight.bold, fontSize: 16));
return;
}
_ipAddress = '';
_statusMessage = new Text('Not Connected',
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.bold, fontSize: 16));
_statusMessage =
new Text('Not Connected', style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold, fontSize: 16));
});
}

Expand All @@ -85,9 +81,7 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
width: MediaQuery.of(context).size.width,
child: Image.asset('assets/planetary-network.png'),
),
Text('Planetary Network',
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 24)),
Text('Planetary Network', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
Padding(
padding: const EdgeInsets.only(top: 20, left: 40, right: 40),
child: Text(
Expand All @@ -105,42 +99,37 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
),
Padding(
padding: const EdgeInsets.only(left: 40, right: 40, top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_statusMessage,
Switch(
value: _isSwitched,
// If the user spams the button, the application would crash => use a timeout
onChanged: _vpnTimeoutRunning
? null
: (value) async {
// Check status => if connected early return
if (_vpnState.vpnConnected) {
return disconnectVpn();
}

connectingVpnmessage();
bool isVPNConnectionStarted = await _vpnState
.plugin
.startVpn(await getEdCurveKeys());

if (!isVPNConnectionStarted) {
return askForVpnPermissions();
}

await connectVpn();
},

activeColor: Theme.of(context).primaryColorDark,
),
]),
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
_statusMessage,
Switch(
value: _isSwitched,
// If the user spams the button, the application would crash => use a timeout
onChanged: _vpnTimeoutRunning
? null
: (value) async {
// Check status => if connected early return
if (_vpnState.vpnConnected) {
return disconnectVpn();
}

connectingVpnmessage();
bool isVPNConnectionStarted = await _vpnState.plugin.startVpn(await getEdCurveKeys());

if (!isVPNConnectionStarted) {
return askForVpnPermissions();
}

await connectVpn();
},

activeColor: Theme.of(context).primaryColorDark,
),
]),
),
new GestureDetector(
onTap: () async {
if (_vpnState.ipAddress != "") {
Clipboard.setData(
new ClipboardData(text: _vpnState.ipAddress));
Clipboard.setData(new ClipboardData(text: _vpnState.ipAddress));

final snackBar = SnackBar(
content: Text('Address copied to clipboard'),
Expand All @@ -162,18 +151,16 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
_ipAddress = '';
_vpnTimeoutRunning = true;
_statusMessage = new Text('Disconnecting ...',
style: TextStyle(
color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));
style: TextStyle(color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));

setState(() {});
}

void connectingVpnmessage() {
_ipAddress = '';
_vpnTimeoutRunning = true;
_statusMessage = new Text('Connecting ...',
style: TextStyle(
color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));
_statusMessage =
new Text('Connecting ...', style: TextStyle(color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));

setState(() {});
}
Expand All @@ -182,8 +169,7 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
_ipAddress = '';
_vpnTimeoutRunning = true;
_statusMessage = new Text('Disconnecting ...',
style: TextStyle(
color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));
style: TextStyle(color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));

setState(() {});

Expand All @@ -194,23 +180,26 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
_isSwitched = false;

_vpnTimeoutRunning = false;
_statusMessage = new Text('Not connected',
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.bold, fontSize: 16));
_statusMessage =
new Text('Not connected', style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold, fontSize: 16));

setState(() {});
}

void askForVpnPermissions() {
_ipAddress = "Please click connect again after accepting VPN permissions.";
_isSwitched = false;
_vpnTimeoutRunning = false;
_statusMessage =
new Text('Not connected', style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold, fontSize: 16));

setState(() {});
}

Future<void> connectVpn() async {
_vpnTimeoutRunning = true;
_statusMessage = new Text('Connecting ...',
style: TextStyle(
color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));
_statusMessage =
new Text('Connecting ...', style: TextStyle(color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 16));

setState(() {});

Expand All @@ -230,9 +219,8 @@ class _PlanetaryNetworkScreenState extends State<PlanetaryNetworkScreen> {
isDoneConnecting = false;
_vpnTimeoutRunning = false;
_isSwitched = true;
_statusMessage = new Text('Connected',
style: TextStyle(
color: Colors.green, fontWeight: FontWeight.bold, fontSize: 16));
_statusMessage =
new Text('Connected', style: TextStyle(color: Colors.green, fontWeight: FontWeight.bold, fontSize: 16));
_ipAddress = 'IP Address: ' + _vpnState.ipAddress;
setState(() {});
_vpnState.vpnConnected = true;
Expand Down

0 comments on commit e27045c

Please sign in to comment.