BotZ_app/lib/ui/operations_page.dart

175 lines
4.2 KiB
Dart
Raw Permalink Normal View History

2019-08-30 01:29:37 +02:00
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'drawer.dart';
import '../utils/connection.dart';
var log = Logger();
typedef WhatNext = Future<void> Function();
Card loadingTile(bool loading, Widget subject, WhatNext callback) {
Widget widgetBody;
if (loading) {
widgetBody = CircularProgressIndicator();
} else {
widgetBody = subject;
}
return Card(
child: ListTile(title: Center(child: widgetBody), onTap: callback));
}
class OperationsPage extends StatefulWidget {
OperationsPage({Key key, this.title = "Operations"}) : super(key: key);
final String title;
@override
_OperationsState createState() => _OperationsState();
}
class _OperationsState extends State<OperationsPage> {
Connection connection;
bool connected = false;
bool checkedIn = false;
bool _loading = false;
Future<void> initConnect() async {
var persistence = await SharedPreferences.getInstance();
var _username = persistence.getString("username");
var _password = persistence.getString("password");
var _baseAddr = Uri.parse(persistence.getString("botZAddress"));
log.d("user: $_username\npass: $_password\nbaseAddr: $_baseAddr");
if (_username == null || _password == null || _baseAddr == null) {
throw Exception("Missing configurations!");
}
var _connection = Connection(_username, _password, _baseAddr);
setState(() {
connection = _connection;
connected = connection.isLoggedIn();
});
}
Future<void> login() async {
log.d("Logging in...");
setState(() {
_loading = true;
});
await connection.login();
setState(() {
connected = connection.isLoggedIn();
checkedIn = connection.isCheckedIn();
_loading = false;
});
}
Future<void> logout() async {
log.d("Logging in...");
setState(() {
_loading = true;
});
await connection.logout();
setState(() {
connected = connection.isLoggedIn();
checkedIn = connection.isCheckedIn();
_loading = false;
});
}
Future<void> checkIn() async {
log.d("Checking in...");
setState(() {
_loading = true;
});
await connection.checkin();
setState(() {
checkedIn = connection.isCheckedIn();
_loading = false;
});
}
Future<void> checkOut() async {
log.d("Checking out...");
setState(() {
_loading = true;
});
await connection.checkout();
setState(() {
checkedIn = connection.isCheckedIn();
_loading = false;
});
}
loginButton() {
if (_loading) {
return CircularProgressIndicator();
} else {
return Text("Login");
}
}
_loggedOutBody(BuildContext context) => <Widget>[
loadingTile(
this._loading,
Row(children: <Widget>[
Icon(Icons.arrow_forward_ios),
Text("Login")
]),
login)
];
// Column(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// crossAxisAlignment: CrossAxisAlignment.center,
// children: <Widget>[
// Center(child: Text("You are not logged in")),
// RaisedButton(onPressed: login, child: loginButton()),
// ]);
Widget checkInOutTile() {
if (checkedIn) {
return loadingTile(
this._loading,
Row(children: <Widget>[Icon(Icons.arrow_downward), Text("Check out")]),
checkOut);
} else {
return loadingTile(
this._loading,
Row(children: <Widget>[Icon(Icons.arrow_upward), Text("Check in")]),
checkIn);
}
}
_loggedInBody(BuildContext context) => <Widget>[
checkInOutTile(),
loadingTile(this._loading,
Row(children: <Widget>[Icon(Icons.close), Text("Logout")]), logout)
];
_body(BuildContext context) {
if (connected) {
return _loggedInBody(context);
} else {
return _loggedOutBody(context);
}
}
@override
void initState() {
super.initState();
initConnect();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: ListView(padding: EdgeInsets.all(5), children: _body(context)),
drawer: theDrawer(context),
);
}
}