From 660ad51d3ba0796ebfb3cb01011fac5279f82148 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 16 Mar 2019 16:14:05 +0100 Subject: [PATCH 1/3] Started implementing user logout --- lib/global.dart | 6 +++++ lib/pages/home.dart | 55 ++++++++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/global.dart b/lib/global.dart index 818d4ac..96eb56f 100644 --- a/lib/global.dart +++ b/lib/global.dart @@ -72,6 +72,12 @@ class VikunjaGlobalState extends State { }); } + void logoutUser() async { + setState(() => _loading = true); + return await _storage.deleteAll(); + setState(() => _loading = false); + } + void _loadCurrentUser() async { var currentUser = await _storage.read(key: 'currentUser'); if (currentUser == null) { diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 710d0c4..089ec8b 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; + import 'package:vikunja_app/components/AddDialog.dart'; import 'package:vikunja_app/components/GravatarImage.dart'; import 'package:vikunja_app/pages/namespace/namespace.dart'; @@ -15,12 +16,14 @@ class HomePage extends StatefulWidget { class HomePageState extends State { List _namespaces = []; + Namespace get _currentNamespace => _selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length ? _namespaces[_selectedDrawerIndex] : null; int _selectedDrawerIndex = -1; bool _loading = true; + bool _showUserDetails = false; _getDrawerItemWidget(int pos) { if (pos == -1) { @@ -71,19 +74,45 @@ class HomePageState extends State { _loadNamespaces(); } - @override - Widget build(BuildContext context) { - var currentUser = VikunjaGlobal.of(context).currentUser; - List drawerOptions = []; + Widget _namespacesWidget() { + List namespacesList = []; _namespaces .asMap() - .forEach((i, namespace) => drawerOptions.add(new ListTile( + .forEach((i, namespace) => namespacesList.add(new ListTile( leading: const Icon(Icons.folder), title: new Text(namespace.name), selected: i == _selectedDrawerIndex, onTap: () => _onSelectItem(i), ))); + return this._loading + ? Center(child: CircularProgressIndicator()) + : RefreshIndicator( + child: ListView( + padding: EdgeInsets.zero, + children: ListTile.divideTiles( + context: context, tiles: namespacesList) + .toList()), + onRefresh: _loadNamespaces, + ); + } + + Widget _userDetailsWidget() { + return ListView(padding: EdgeInsets.zero, children: [ + ListTile( + title: Text('Logout'), + leading: Icon(Icons.exit_to_app), + onTap: () { + VikunjaGlobal.of(context).logoutUser(); + }, + ), + ]); + } + + @override + Widget build(BuildContext context) { + var currentUser = VikunjaGlobal.of(context).currentUser; + return new Scaffold( appBar: AppBar(title: new Text(_currentNamespace?.name ?? 'Vikunja')), drawer: new Drawer( @@ -91,6 +120,11 @@ class HomePageState extends State { new UserAccountsDrawerHeader( accountEmail: currentUser == null ? null : Text(currentUser.email), accountName: currentUser == null ? null : Text(currentUser.username), + onDetailsPressed: () { + setState(() { + _showUserDetails = !_showUserDetails; + }); + }, currentAccountPicture: currentUser == null ? null : CircleAvatar( @@ -104,16 +138,7 @@ class HomePageState extends State { ), ), new Expanded( - child: this._loading - ? Center(child: CircularProgressIndicator()) - : RefreshIndicator( - child: ListView( - padding: EdgeInsets.zero, - children: ListTile.divideTiles( - context: context, tiles: drawerOptions) - .toList()), - onRefresh: _loadNamespaces, - )), + child: _showUserDetails ? _userDetailsWidget() : _namespacesWidget()), new Align( alignment: FractionalOffset.bottomCenter, child: Builder( -- 2.40.1 From 8499b18cc9cf2e988be539028f7d1daafaac94b1 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 16 Mar 2019 16:50:58 +0100 Subject: [PATCH 2/3] Fixed logging out throwing exceptions --- lib/global.dart | 22 ++++++++++++++++++---- lib/pages/home.dart | 9 +++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/global.dart b/lib/global.dart index 96eb56f..33e0aeb 100644 --- a/lib/global.dart +++ b/lib/global.dart @@ -33,12 +33,17 @@ class VikunjaGlobalState extends State { bool _loading = true; User get currentUser => _currentUser; + Client get client => _client; UserManager get userManager => new UserManager(_storage); + UserService newUserService(base) => new UserAPIService(Client(null, base)); + NamespaceService get namespaceService => new NamespaceAPIService(client); + TaskService get taskService => new TaskAPIService(client); + ListService get listService => new ListAPIService(client); @override @@ -72,10 +77,19 @@ class VikunjaGlobalState extends State { }); } - void logoutUser() async { - setState(() => _loading = true); - return await _storage.deleteAll(); - setState(() => _loading = false); + void logoutUser(BuildContext context) { + _storage.deleteAll().then((_) { + Navigator.pop(context); + setState(() { + _client = null; + _currentUser = null; + }); + }) + .catchError((err) { + Scaffold.of(context).showSnackBar(SnackBar( + content: Text('An error occured while logging out!'), + )); + }); } void _loadCurrentUser() async { diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 089ec8b..d475e3d 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -97,13 +97,13 @@ class HomePageState extends State { ); } - Widget _userDetailsWidget() { + Widget _userDetailsWidget(BuildContext context) { return ListView(padding: EdgeInsets.zero, children: [ ListTile( title: Text('Logout'), leading: Icon(Icons.exit_to_app), onTap: () { - VikunjaGlobal.of(context).logoutUser(); + VikunjaGlobal.of(context).logoutUser(context); }, ), ]); @@ -137,8 +137,9 @@ class HomePageState extends State { Theme.of(context).primaryColor, BlendMode.multiply)), ), ), - new Expanded( - child: _showUserDetails ? _userDetailsWidget() : _namespacesWidget()), + new Builder(builder: (BuildContext context) => Expanded( + child: _showUserDetails ? _userDetailsWidget(context) : _namespacesWidget() + )), new Align( alignment: FractionalOffset.bottomCenter, child: Builder( -- 2.40.1 From c41561da4fa1bde359d7056c2b394a9865076e2e Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 16 Mar 2019 16:51:53 +0100 Subject: [PATCH 3/3] format --- lib/global.dart | 3 +-- lib/pages/home.dart | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/global.dart b/lib/global.dart index 33e0aeb..061ae99 100644 --- a/lib/global.dart +++ b/lib/global.dart @@ -84,8 +84,7 @@ class VikunjaGlobalState extends State { _client = null; _currentUser = null; }); - }) - .catchError((err) { + }).catchError((err) { Scaffold.of(context).showSnackBar(SnackBar( content: Text('An error occured while logging out!'), )); diff --git a/lib/pages/home.dart b/lib/pages/home.dart index d475e3d..2216972 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -137,9 +137,11 @@ class HomePageState extends State { Theme.of(context).primaryColor, BlendMode.multiply)), ), ), - new Builder(builder: (BuildContext context) => Expanded( - child: _showUserDetails ? _userDetailsWidget(context) : _namespacesWidget() - )), + new Builder( + builder: (BuildContext context) => Expanded( + child: _showUserDetails + ? _userDetailsWidget(context) + : _namespacesWidget())), new Align( alignment: FractionalOffset.bottomCenter, child: Builder( -- 2.40.1