BotZ_app/lib/ui/main_page.dart

148 lines
4.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
var log = Logger();
class _DataState {
_DataState(
{this.userPresent = false,
this.passPresent = false,
this.zServerPresent = false,
this.botZServerPresent = false});
bool userPresent;
bool passPresent;
bool zServerPresent;
bool botZServerPresent;
bool all() {
assert(userPresent != null);
assert(passPresent != null);
assert(zServerPresent != null);
assert(botZServerPresent != null);
var result =
userPresent && passPresent && zServerPresent && botZServerPresent;
return result;
}
String toString() {
return "\tuser: $userPresent\n\tpass: $passPresent\n\tzServer: $zServerPresent\n\tbotZServer: $botZServerPresent";
}
}
class HomePage extends StatefulWidget {
HomePage(
{@required this.nextRoute,
this.timeout = const Duration(seconds: 1),
Key key})
: assert(nextRoute != null),
super(key: key);
final String nextRoute;
final Duration timeout;
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
_DataState status = _DataState();
bool _retrieved = false;
bool _waitingForWelcome = false;
String title = "BotZ";
Future<void> _retriveStateData() async {
var persistence = await SharedPreferences.getInstance();
var stateFromPersistence = {
"user": (persistence.getString("username") != null) ? true : false,
"pass": (persistence.getString("password") != null) ? true : false,
"zAddr": (persistence.getString("zAddress") != null) ? true : false,
"botZAddr": (persistence.getString("botZAddress") != null) ? true : false,
};
log.d("State from persistence: ${stateFromPersistence.toString()}");
setState(() {
status.userPresent = stateFromPersistence["user"];
status.passPresent = stateFromPersistence["pass"];
status.zServerPresent = stateFromPersistence["zAddr"];
status.botZServerPresent = stateFromPersistence["botZAddr"];
_retrieved = true;
});
}
@override
void initState() {
super.initState();
_retriveStateData();
}
bool _checkData() {
log.d("State: ${status.toString()}");
if (status.all()) {
log.d("Necessary data is present");
return true;
}
log.d("Necessary data is MISSING");
return false;
}
void _goToNextPage() {
Navigator.pushReplacementNamed(context, widget.nextRoute);
}
Future<void> _cleanState() async {
var persistence = await SharedPreferences.getInstance();
persistence.setString("username", null);
persistence.setString("password", null);
persistence.setString("zAddress", null);
persistence.setString("botZAddress", null);
setState(() {
status.userPresent = false;
status.passPresent = false;
status.zServerPresent = false;
status.botZServerPresent = false;
});
}
Widget mainView() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(onPressed: _cleanState, child: Text("Clean state")),
Column(children: <Widget>[
Text("Username: ${status.userPresent}"),
Text("Password: ${status.passPresent}"),
Text("Z Address: ${status.zServerPresent}"),
Text("BotZ Address: ${status.botZServerPresent}"),
])
],
));
}
Widget _body() {
log.d("Retrieved: $_retrieved");
if (_retrieved) {
if (_checkData()) {
log.d("Main body");
return mainView();
} else {
if (!_waitingForWelcome) {
log.d("Go to ask the user for the data...");
Future.delayed(widget.timeout, _goToNextPage);
setState(() {
_waitingForWelcome = true;
});
}
}
}
return CircularProgressIndicator();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: _body(),
);
}
}