Adding operations and settings.

master
blallo 2019-08-30 01:29:37 +02:00
parent 1e976224cb
commit ccab0eef0b
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 221 additions and 0 deletions

View File

@ -4,6 +4,8 @@ import 'ui/welcome_page.dart';
import 'ui/credentials_form.dart';
import 'ui/server_form.dart';
import 'ui/dialog.dart';
import 'ui/settings_page.dart';
import 'ui/operations_page.dart';
void main() => runApp(MyApp());
@ -22,8 +24,10 @@ class MyApp extends StatelessWidget {
routes: {
"/": (context) => HomePage(nextRoute: "/welcome"),
"/welcome": (context) => WelcomePage(nextRoute: "/settings/server"),
"/settings": (context) => SettingsPage(),
"/settings/server": (context) => ServerForm(defaultNextRoute: "/settings/credentials"),
"/settings/credentials": (context) => CredentialsForm(defaultNextRoute: "/"),
"/operations": (context) => OperationsPage(),
});
}
}

View File

@ -0,0 +1,174 @@
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),
);
}
}

View File

@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'drawer.dart';
var log = Logger();
class SettingsPage extends StatelessWidget {
SettingsPage({this.title = "Settings"});
final String title;
Future<void> _goToServerForm(BuildContext context) async {
log.d("Going to server form");
var persistence = await SharedPreferences.getInstance();
persistence.setString("nextRoute", "/settings");
Navigator.of(context).pushNamed("/settings/server");
}
Future<void> _goToCredentialsForm(BuildContext context) async {
log.d("Going to credentials form");
var persistence = await SharedPreferences.getInstance();
persistence.setString("nextRoute", "/settings");
Navigator.of(context).pushNamed("/settings/credentials");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: ListView(padding: EdgeInsets.all(5), children: <Widget>[
ListTile(
title: Text("Change credentials"),
onTap: () => _goToCredentialsForm(context)),
ListTile(
title: Text("Change server configurations"),
onTap: () => _goToServerForm(context)),
]),
drawer: theDrawer(context),
);
}
}