From f41236e3041a9f71480b3296df9436b17d0f218b Mon Sep 17 00:00:00 2001 From: itsscb Date: Mon, 30 Oct 2023 22:48:37 +0100 Subject: [PATCH] rf/extracts bottomnavigationbar & drawer --- frontend/app/lib/data/database.dart | 19 +- frontend/app/lib/gapi/client.dart | 23 +- frontend/app/lib/pages/dashboard_page.dart | 2 +- frontend/app/lib/pages/login_page.dart | 208 ++++------ frontend/app/lib/pages/start_page.dart | 462 +++++++++------------ frontend/app/lib/widgets/bottom_bar.dart | 20 + frontend/app/lib/widgets/side_drawer.dart | 29 ++ 7 files changed, 346 insertions(+), 417 deletions(-) create mode 100644 frontend/app/lib/widgets/bottom_bar.dart create mode 100644 frontend/app/lib/widgets/side_drawer.dart diff --git a/frontend/app/lib/data/database.dart b/frontend/app/lib/data/database.dart index 6342ddb..f5d42d8 100644 --- a/frontend/app/lib/data/database.dart +++ b/frontend/app/lib/data/database.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'package:fixnum/fixnum.dart'; import 'package:app/pb/google/protobuf/timestamp.pb.dart'; -import 'package:flutter/services.dart'; import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; @@ -49,7 +48,7 @@ class Session { } Future _initDatabase() async { - print('DB: INITIALIZING - start'); + // print('DB: INITIALIZING - start'); _database = await openDatabase( join(await getDatabasesPath(), 'df_database.db'), onCreate: (db, version) { @@ -59,7 +58,7 @@ class Session { }, version: 1, ); - print('DB: INITIALIZING - end'); + // print('DB: INITIALIZING - end'); return _database; } @@ -81,19 +80,25 @@ class Session { } Future insertSession(Session session) async { - print('INSERTING SESSION: ${session.sessionId}'); + // print('INSERTING SESSION: ${session.sessionId}'); final db = _database; - final result = await db.insert( + await db.insert( 'sessions', session.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); - print('INSERT RESULT: $result'); + // print('INSERT RESULT: $result'); } Future removeSession(String sessionId) async { final db = _database; await db.delete('sessions', where: 'sessionId = ?', whereArgs: [sessionId]); + this.sessionId = null; + refreshToken = null; + accessTokenExpiresAt = null; + refreshTokenExpiresAt = null; + accountId = null; + accessToken = null; } Future> getSessions() async { @@ -104,7 +109,7 @@ class Session { final List sessions = List.generate( maps.length, (i) { - print('GOT MAP: ${maps[i]}'); + // print('GOT MAP: ${maps[i]}'); return Session( sessionId: maps[i]['sessionId'] as String, diff --git a/frontend/app/lib/gapi/client.dart b/frontend/app/lib/gapi/client.dart index bacf512..aae5a46 100644 --- a/frontend/app/lib/gapi/client.dart +++ b/frontend/app/lib/gapi/client.dart @@ -8,7 +8,6 @@ import 'package:grpc/grpc.dart'; class GClient { GClient() { - // session = Session.newSession(); _init(); } @@ -16,8 +15,6 @@ class GClient { int port = 9090; Map metadata = {'Authorization': ''}; - // String accessToken = ''; - // Int64 accountId = Int64(); late Session session; @@ -43,16 +40,10 @@ class GClient { Future main(List args) async {} void _init() async { - // print('CLIENT: INITIALIZING CLIENT - start'); - session = await Session.newSession; - // print('CLIENT: getting sessions from database'); final sessions = await session.getSessions(); - print('CLIENT: got sessions from database: ${sessions.toString()}'); session = sessions[0]; - // print('CLIENT: INITIALIZING CLIENT - end'); - print(session.toString()); } Future createAccount( @@ -60,9 +51,7 @@ class GClient { try { final response = stub.createAccount(request); return response; - } catch (e) { - print('caught error: $e'); - } + } catch (e) {} return CreateAccountResponse(); } @@ -77,23 +66,18 @@ class GClient { email: email, password: password, )); - print(response); session.accessToken = response.accessToken; session.accountId = response.accountId; session.sessionId = response.sessionId; session.refreshToken = response.refreshToken; session.accessTokenExpiresAt = response.accessTokenExpiresAt; session.refreshTokenExpiresAt = response.refreshTokenExpiresAt; - print('GOT: ${session.toString()}'); try { session.insertSession(session); - } catch (err) { - print('ERROR WRITING DB: $err'); - } + } catch (err) {} metadata['Authorization'] = 'Bearer ${response.accessToken}'; - print('auth: ${metadata['Authorization']}'); onSuccess(); - // return response; + return response; } on GrpcError catch (e) { print('caught error: ${e.message}'); metadata['Authorization'] = ''; @@ -109,7 +93,6 @@ class GClient { Future getAccountInfo(GetAccountInfoRequest request, {required Function onError}) async { try { - // Map metadata = {'Authorization': 'Bearer $token'}; final response = await stub.getAccountInfo( request, options: CallOptions( diff --git a/frontend/app/lib/pages/dashboard_page.dart b/frontend/app/lib/pages/dashboard_page.dart index 70a1ea9..6db7ed0 100644 --- a/frontend/app/lib/pages/dashboard_page.dart +++ b/frontend/app/lib/pages/dashboard_page.dart @@ -35,7 +35,7 @@ class _DashboardPageState extends State { _setLoading(true); widget.client.getAccountInfo( GetAccountInfoRequest( - accountId: widget.client.session?.accountId, + accountId: widget.client.session.accountId, ), onError: () { ScaffoldMessenger.of(context).showSnackBar(const SnackBar( diff --git a/frontend/app/lib/pages/login_page.dart b/frontend/app/lib/pages/login_page.dart index 334911e..4856e08 100644 --- a/frontend/app/lib/pages/login_page.dart +++ b/frontend/app/lib/pages/login_page.dart @@ -1,7 +1,9 @@ import 'package:app/gapi/client.dart'; import 'package:app/pages/start_page.dart'; import 'package:app/widgets/background.dart'; +import 'package:app/widgets/bottom_bar.dart'; import 'package:app/widgets/loading_widget.dart'; +import 'package:app/widgets/side_drawer.dart'; import 'package:flutter/material.dart'; import 'package:grpc/grpc.dart'; @@ -84,8 +86,92 @@ class _LoginPageState extends State { // height: 80, // ), ), - bottomNavigationBar: const BottomBar(), - drawer: const SideDrawer(), + bottomNavigationBar: BottomBar( + children: [ + BottomNavigationBarItem( + label: 'back', + backgroundColor: Colors.white, + icon: IconButton( + onPressed: () => Navigator.of(context).pop(), + icon: const Icon( + Icons.arrow_back, + color: Colors.white, + ), + ), + ), + BottomNavigationBarItem( + backgroundColor: Colors.white, + label: 'Menu', + icon: IconButton( + onPressed: () => Scaffold.of(context).openDrawer(), + icon: const Icon( + Icons.menu, + color: Colors.white, + ), + ), + ), + ], + ), + drawer: SideDrawer(children: [ + const Spacer(), + TextButton( + onPressed: () { + Scaffold.of(context).closeDrawer(); + }, + child: const Row( + children: [ + Text( + 'About', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.question_answer, + color: Colors.white, + ), + ], + ), + ), + TextButton( + onPressed: () { + Scaffold.of(context).closeDrawer(); + }, + child: const Row( + children: [ + Text( + 'Datenschutz', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.privacy_tip, + color: Colors.white, + ), + ], + ), + ), + TextButton( + onPressed: () { + Scaffold.of(context).closeDrawer(); + }, + child: const Row( + children: [ + Text( + 'Impressum', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.apartment, + color: Colors.white, + ), + ], + ), + ), + const SizedBox( + height: 250, + ) + ]), body: !_loading ? Form( key: _formKey, @@ -256,121 +342,3 @@ class _LoginPageState extends State { ); } } - -class SideDrawer extends StatelessWidget { - const SideDrawer({ - super.key, - }); - - @override - Widget build(BuildContext context) { - return Drawer( - backgroundColor: Colors.black, - child: Padding( - padding: const EdgeInsets.all(40.0), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Spacer(), - TextButton( - onPressed: () { - Scaffold.of(context).closeDrawer(); - }, - child: const Row( - children: [ - Text( - 'About', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.question_answer, - color: Colors.white, - ), - ], - ), - ), - TextButton( - onPressed: () { - Scaffold.of(context).closeDrawer(); - }, - child: const Row( - children: [ - Text( - 'Datenschutz', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.privacy_tip, - color: Colors.white, - ), - ], - ), - ), - TextButton( - onPressed: () { - Scaffold.of(context).closeDrawer(); - }, - child: const Row( - children: [ - Text( - 'Impressum', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.apartment, - color: Colors.white, - ), - ], - ), - ), - const SizedBox( - height: 250, - ) - ], - ), - ), - ); - } -} - -class BottomBar extends StatelessWidget { - const BottomBar({ - super.key, - }); - - @override - Widget build(BuildContext context) { - return BottomNavigationBar( - items: [ - BottomNavigationBarItem( - label: 'back', - backgroundColor: Colors.white, - icon: IconButton( - onPressed: () => Navigator.of(context).pop(), - icon: const Icon( - Icons.arrow_back, - color: Colors.white, - ), - ), - ), - BottomNavigationBarItem( - backgroundColor: Colors.white, - label: 'Menu', - icon: IconButton( - onPressed: () => Scaffold.of(context).openDrawer(), - icon: const Icon( - Icons.menu, - color: Colors.white, - ), - ), - ), - ], - backgroundColor: Colors.black, - fixedColor: Colors.black, - // onTap: (value) => _bottomBarAction(value), - ); - } -} diff --git a/frontend/app/lib/pages/start_page.dart b/frontend/app/lib/pages/start_page.dart index b106f3b..e3f2ba9 100644 --- a/frontend/app/lib/pages/start_page.dart +++ b/frontend/app/lib/pages/start_page.dart @@ -1,6 +1,8 @@ import 'package:app/gapi/client.dart'; import 'package:app/pages/login_page.dart'; import 'package:app/widgets/background.dart'; +import 'package:app/widgets/bottom_bar.dart'; +import 'package:app/widgets/side_drawer.dart'; import 'package:flutter/material.dart'; import 'dart:core'; @@ -40,15 +42,194 @@ class _StartPageState extends State { appBar: AppBar( automaticallyImplyLeading: false, ), - drawer: SideDrawer( - onLogout: () { - setState(() { - widget.client?.session - .removeSession(widget.client!.session.sessionId!); - }); - }, - ), - bottomNavigationBar: BottomBar(widget: widget), + drawer: SideDrawer(children: [ + const Spacer(), + TextButton( + onPressed: () { + Scaffold.of(context).closeDrawer(); + }, + child: const Row( + children: [ + Text( + 'About', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.question_answer, + color: Colors.white, + ), + ], + ), + ), + TextButton( + onPressed: () { + Scaffold.of(context).closeDrawer(); + }, + child: const Row( + children: [ + Text( + 'Datenschutz', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.privacy_tip, + color: Colors.white, + ), + ], + ), + ), + TextButton( + onPressed: () { + Scaffold.of(context).closeDrawer(); + }, + child: const Row( + children: [ + Text( + 'Impressum', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.apartment, + color: Colors.white, + ), + ], + ), + ), + TextButton( + onPressed: () { + setState(() { + widget.client?.session.accessToken = null; + widget.client?.session + .removeSession(widget.client!.session.sessionId!); + }); + }, + child: const Row( + children: [ + Text( + 'Log out', + style: TextStyle(fontSize: 20), + ), + Spacer(), + Icon( + Icons.logout, + color: Colors.white, + ), + ], + ), + ), + const SizedBox( + height: 250, + ) + ]), + bottomNavigationBar: Builder(builder: (context) { + return BottomBar( + // onTap: (value) => _bottomBarAction(value), + children: widget.client?.session.accessToken != null + ? [ + BottomNavigationBarItem( + backgroundColor: Colors.white, + label: 'Personen', + icon: Column( + children: [ + IconButton( + onPressed: () => Scaffold.of(context).openDrawer(), + icon: const Icon( + Icons.group, + color: Colors.white, + ), + ), + const Text( + 'Personen', + style: TextStyle( + color: Colors.white, + fontSize: 16, + ), + ) + ], + ), + ), + BottomNavigationBarItem( + backgroundColor: Colors.white, + label: 'Menu', + icon: IconButton( + onPressed: () { + Scaffold.of(context).openDrawer(); + }, + icon: const Icon( + Icons.menu, + color: Colors.white, + ), + ), + ) + ] + : [ + BottomNavigationBarItem( + label: 'register', + backgroundColor: Colors.white, + icon: Column( + children: [ + IconButton( + onPressed: () { + Navigator.of(context).push(MaterialPageRoute( + builder: (context) => LoginPage())); + }, + icon: const Icon( + Icons.login, + color: Colors.white, + ), + ), + const Text( + 'Registrieren', + style: TextStyle( + color: Colors.white, + fontSize: 16, + ), + ) + ], + ), + ), + BottomNavigationBarItem( + label: 'login', + backgroundColor: Colors.white, + icon: Column( + children: [ + IconButton( + onPressed: () { + Navigator.of(context).push(MaterialPageRoute( + builder: (context) => LoginPage())); + }, + icon: const Icon( + Icons.login, + color: Colors.white, + ), + ), + const Text( + 'Login', + style: TextStyle( + color: Colors.white, + fontSize: 16, + ), + ) + ], + ), + ), + BottomNavigationBarItem( + backgroundColor: Colors.white, + label: 'Menu', + icon: IconButton( + onPressed: () => Scaffold.of(context).openDrawer(), + icon: const Icon( + Icons.menu, + color: Colors.white, + ), + ), + ), + ], + ); + }), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( @@ -74,54 +255,14 @@ class _StartPageState extends State { ), ), TextButton( - onPressed: () { - final s = widget.client?.session.getSessions(); - print(s); - print(widget.client?.session.accessToken); + onPressed: () async { + final s = await widget.client?.session.getSessions(); + print('SESSIONS: $s'); }, child: const Text( "GET SESSIONS", ), ), - // const Spacer(), - // Row( - // mainAxisAlignment: MainAxisAlignment.spaceEvenly, - // crossAxisAlignment: CrossAxisAlignment.center, - // children: widget.client?.accessToken == null - // ? [ - // ElevatedButton.icon( - // label: const Text('Login'), - // onPressed: () { - // // onChangePage(Pages.login); - // Navigator.of(context).push( - // MaterialPageRoute( - // builder: ((context) => LoginPage()), - // ), - // ); - // }, - // icon: const Icon( - // Icons.login, - // semanticLabel: 'Login', - // size: 16, - // ), - // ), - // ElevatedButton.icon( - // label: const Text('Registrieren'), - // onPressed: () {}, - // icon: const Icon( - // Icons.person_add, - // semanticLabel: 'Register', - // size: 16, - // ), - // ), - // ] - // : [], - // ), - // const SizedBox( - // height: 38, - // ), - - // const Text('data'), ], ), ), @@ -129,220 +270,3 @@ class _StartPageState extends State { ); } } - -class SideDrawer extends StatelessWidget { - SideDrawer({ - super.key, - required this.onLogout, - }); - - Function() onLogout; - - @override - Widget build(BuildContext context) { - return Drawer( - backgroundColor: Colors.black, - child: Padding( - padding: const EdgeInsets.all(40.0), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Spacer(), - TextButton( - onPressed: () { - Scaffold.of(context).closeDrawer(); - }, - child: const Row( - children: [ - Text( - 'About', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.question_answer, - color: Colors.white, - ), - ], - ), - ), - TextButton( - onPressed: () { - Scaffold.of(context).closeDrawer(); - }, - child: const Row( - children: [ - Text( - 'Datenschutz', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.privacy_tip, - color: Colors.white, - ), - ], - ), - ), - TextButton( - onPressed: () { - Scaffold.of(context).closeDrawer(); - }, - child: const Row( - children: [ - Text( - 'Impressum', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.apartment, - color: Colors.white, - ), - ], - ), - ), - TextButton( - onPressed: () { - onLogout(); - }, - child: const Row( - children: [ - Text( - 'Log out', - style: TextStyle(fontSize: 20), - ), - Spacer(), - Icon( - Icons.logout, - color: Colors.white, - ), - ], - ), - ), - const SizedBox( - height: 250, - ) - ], - ), - ), - ); - } -} - -class BottomBar extends StatelessWidget { - const BottomBar({ - super.key, - required this.widget, - }); - - final StartPage widget; - - @override - Widget build(BuildContext context) { - return BottomNavigationBar( - // onTap: (value) => _bottomBarAction(value), - items: widget.client?.session.accessToken != null - ? [ - BottomNavigationBarItem( - backgroundColor: Colors.white, - label: 'Personen', - icon: Column( - children: [ - IconButton( - onPressed: () => Scaffold.of(context).openDrawer(), - icon: const Icon( - Icons.group, - color: Colors.white, - ), - ), - const Text( - 'Personen', - style: TextStyle( - color: Colors.white, - fontSize: 16, - ), - ) - ], - ), - ), - BottomNavigationBarItem( - backgroundColor: Colors.white, - label: 'Menu', - icon: IconButton( - onPressed: () => Scaffold.of(context).openDrawer(), - icon: const Icon( - Icons.menu, - color: Colors.white, - ), - ), - ) - ] - : [ - BottomNavigationBarItem( - label: 'register', - backgroundColor: Colors.white, - icon: Column( - children: [ - IconButton( - onPressed: () { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => LoginPage())); - }, - icon: const Icon( - Icons.login, - color: Colors.white, - ), - ), - const Text( - 'Registrieren', - style: TextStyle( - color: Colors.white, - fontSize: 16, - ), - ) - ], - ), - ), - BottomNavigationBarItem( - label: 'login', - backgroundColor: Colors.white, - icon: Column( - children: [ - IconButton( - onPressed: () { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => LoginPage())); - }, - icon: const Icon( - Icons.login, - color: Colors.white, - ), - ), - const Text( - 'Login', - style: TextStyle( - color: Colors.white, - fontSize: 16, - ), - ) - ], - ), - ), - BottomNavigationBarItem( - backgroundColor: Colors.white, - label: 'Menu', - icon: IconButton( - onPressed: () => Scaffold.of(context).openDrawer(), - icon: const Icon( - Icons.menu, - color: Colors.white, - ), - ), - ), - ], - backgroundColor: Colors.black, - fixedColor: Colors.black, - ); - } -} diff --git a/frontend/app/lib/widgets/bottom_bar.dart b/frontend/app/lib/widgets/bottom_bar.dart new file mode 100644 index 0000000..eb9a437 --- /dev/null +++ b/frontend/app/lib/widgets/bottom_bar.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; + +class BottomBar extends StatelessWidget { + const BottomBar({ + super.key, + required this.children, + }); + + final List children; + + @override + Widget build(BuildContext context) { + return BottomNavigationBar( + items: children, + backgroundColor: Colors.black, + fixedColor: Colors.black, + // onTap: (value) => _bottomBarAction(value), + ); + } +} diff --git a/frontend/app/lib/widgets/side_drawer.dart b/frontend/app/lib/widgets/side_drawer.dart new file mode 100644 index 0000000..73f55e2 --- /dev/null +++ b/frontend/app/lib/widgets/side_drawer.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; + +class SideDrawer extends StatefulWidget { + const SideDrawer({ + super.key, + required this.children, + }); + + final List children; + + @override + State createState() => _SideDrawerState(); +} + +class _SideDrawerState extends State { + @override + Widget build(BuildContext context) { + return Drawer( + backgroundColor: Colors.black, + child: Padding( + padding: const EdgeInsets.all(40.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: widget.children, + ), + ), + ); + } +}