BotZ_app/lib/ui/server_form.dart

180 lines
5.1 KiB
Dart
Raw Permalink Normal View History

2019-08-18 23:06:25 +02:00
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
2019-08-22 02:45:37 +02:00
import 'package:shared_preferences/shared_preferences.dart';
2019-08-18 23:06:25 +02:00
import 'dart:convert';
import 'dialog.dart';
import '../utils/verify_server.dart';
final log = Logger();
class ServerForm extends StatefulWidget {
2019-08-22 02:45:37 +02:00
ServerForm({@required this.defaultNextRoute, Key key}) : super(key: key);
final String title = "Server data";
final String defaultNextRoute;
2019-08-18 23:06:25 +02:00
@override
_ServerFormState createState() => _ServerFormState();
}
class _ServerFormState extends State<ServerForm> {
2019-08-22 02:45:37 +02:00
static String title = "Server info";
2019-08-18 23:06:25 +02:00
Uri zAddress;
Uri botZAddress;
bool _autoValidate = false;
bool _disableFields = false;
final _serverFormKey = GlobalKey<FormState>();
2019-08-22 02:45:37 +02:00
final _scaffoldKey = GlobalKey<ScaffoldState>();
2019-08-18 23:06:25 +02:00
final zController = TextEditingController();
final botZController = TextEditingController();
InputDecoration _textFieldDecoration(String label, IconData icon) {
return InputDecoration(
labelText: label,
icon: Icon(icon),
hintText: 'Please, insert $label address',
);
}
SnackBar showProgress() {
return SnackBar(
content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
CircularProgressIndicator(
value: null,
strokeWidth: 7.0,
),
Text('Processing Data')
],
));
}
String validateServer(String address) {
if (address.isEmpty) {
return 'Server address must not be empty';
}
try {
Uri.parse(address);
} on FormatException catch (e) {
return e.toString();
}
return null;
}
String validateZServer(String address) {
String baseValidation = this.validateServer(address);
if (baseValidation != null) {
return baseValidation;
}
if (utf8.encode(address).length > 72) {
return "Server address string length is > 72 bytes. Refusing to go on.";
}
return null;
}
2019-08-22 02:45:37 +02:00
Future<String> getNextRoute() async {
var persistence = await SharedPreferences.getInstance();
String nextRoute = persistence.getString("nextRoute");
if (nextRoute != null) {
log.d("nextRoute: $nextRoute");
return nextRoute;
}
log.d("defaultNextRoute: ${widget.defaultNextRoute}");
return null;
}
2019-08-18 23:06:25 +02:00
Future<void> _onPressed() async {
log.d("Button pressed");
2019-08-22 02:45:37 +02:00
var persistence = await SharedPreferences.getInstance();
2019-08-18 23:06:25 +02:00
setState(() {
_disableFields = true;
});
2019-08-22 02:45:37 +02:00
_scaffoldKey.currentState.showSnackBar(this.showProgress());
2019-08-18 23:06:25 +02:00
if (!_serverFormKey.currentState.validate()) {
setState(() {
_autoValidate = true;
_disableFields = false;
});
2019-08-22 02:45:37 +02:00
_scaffoldKey.currentState.hideCurrentSnackBar();
2019-08-18 23:06:25 +02:00
return;
}
bool resp;
Uri _botZAddr = Uri.parse(botZController.text);
Uri _zAddr = Uri.parse(zController.text);
log.d("Received botZAddr: $_botZAddr\tzAddr: $_zAddr");
try {
resp = await verifyServer(_botZAddr, _zAddr);
log.i("Secret is${resp ? ' ' : ' NOT '}a match");
} catch (err) {
log.e("Error: ${err.toString()}");
showDialog(
context: context,
builder: errorDialogBuilder("Error", "${err.toString()}"));
} finally {
log.d("Clean the state");
setState(() {
_disableFields = false;
});
2019-08-22 02:45:37 +02:00
_scaffoldKey.currentState.hideCurrentSnackBar();
2019-08-18 23:06:25 +02:00
}
if (!resp) {
showDialog(
context: context,
builder: errorDialogBuilder("Error",
"The BotZ server does not target the provided Z server."));
} else {
_serverFormKey.currentState.save();
2019-08-22 02:45:37 +02:00
persistence.setString("zAddress", this.zAddress.toString());
persistence.setString("botZAddress", this.botZAddress.toString());
String nextRoute = await getNextRoute();
if (nextRoute == null) {
nextRoute = widget.defaultNextRoute;
}
Navigator.pushNamed(context, nextRoute);
2019-08-18 23:06:25 +02:00
}
log.d("Done");
}
2019-08-22 02:45:37 +02:00
Form theForm() => Form(
key: _serverFormKey,
child: Column(children: <Widget>[
TextFormField(
autofocus: true,
autocorrect: false,
controller: zController,
enabled: !_disableFields,
decoration: this._textFieldDecoration("Z server", Icons.http),
validator: this.validateZServer,
autovalidate: _autoValidate,
onSaved: (String addr) {
zAddress = Uri.parse(addr);
}),
TextFormField(
autocorrect: false,
controller: botZController,
enabled: !_disableFields,
decoration: this._textFieldDecoration("BotZ server", Icons.http),
validator: this.validateServer,
autovalidate: _autoValidate,
onSaved: (String addr) {
botZAddress = Uri.parse(addr);
}),
RaisedButton(
onPressed: _onPressed,
child: Text('Save'),
),
]),
);
2019-08-18 23:06:25 +02:00
@override
Widget build(BuildContext context) {
2019-08-22 02:45:37 +02:00
return Scaffold(
key: _scaffoldKey, appBar: AppBar(title: Text(title)), body: theForm());
2019-08-18 23:06:25 +02:00
}
}