diff --git a/lib/main.dart b/lib/main.dart index 46df3fe..4659d54 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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(), }); } } diff --git a/lib/ui/operations_page.dart b/lib/ui/operations_page.dart new file mode 100644 index 0000000..2e8d385 --- /dev/null +++ b/lib/ui/operations_page.dart @@ -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 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 { + Connection connection; + bool connected = false; + bool checkedIn = false; + bool _loading = false; + + Future 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 login() async { + log.d("Logging in..."); + setState(() { + _loading = true; + }); + await connection.login(); + setState(() { + connected = connection.isLoggedIn(); + checkedIn = connection.isCheckedIn(); + _loading = false; + }); + } + + Future logout() async { + log.d("Logging in..."); + setState(() { + _loading = true; + }); + await connection.logout(); + setState(() { + connected = connection.isLoggedIn(); + checkedIn = connection.isCheckedIn(); + _loading = false; + }); + } + + Future checkIn() async { + log.d("Checking in..."); + setState(() { + _loading = true; + }); + await connection.checkin(); + setState(() { + checkedIn = connection.isCheckedIn(); + _loading = false; + }); + } + + Future 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) => [ + loadingTile( + this._loading, + Row(children: [ + Icon(Icons.arrow_forward_ios), + Text("Login") + ]), + login) + ]; + + // Column( + // mainAxisAlignment: MainAxisAlignment.spaceEvenly, + // crossAxisAlignment: CrossAxisAlignment.center, + // children: [ + // Center(child: Text("You are not logged in")), + // RaisedButton(onPressed: login, child: loginButton()), + // ]); + + Widget checkInOutTile() { + if (checkedIn) { + return loadingTile( + this._loading, + Row(children: [Icon(Icons.arrow_downward), Text("Check out")]), + checkOut); + } else { + return loadingTile( + this._loading, + Row(children: [Icon(Icons.arrow_upward), Text("Check in")]), + checkIn); + } + } + + _loggedInBody(BuildContext context) => [ + checkInOutTile(), + loadingTile(this._loading, + Row(children: [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), + ); + } +} diff --git a/lib/ui/settings_page.dart b/lib/ui/settings_page.dart new file mode 100644 index 0000000..35664d3 --- /dev/null +++ b/lib/ui/settings_page.dart @@ -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 _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 _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: [ + ListTile( + title: Text("Change credentials"), + onTap: () => _goToCredentialsForm(context)), + ListTile( + title: Text("Change server configurations"), + onTap: () => _goToServerForm(context)), + ]), + drawer: theDrawer(context), + ); + } +}