3 changed files with 221 additions and 0 deletions
@ -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), |
|||
); |
|||
} |
|||
} |
@ -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), |
|||
); |
|||
} |
|||
} |
Loading…
Reference in new issue