From 1e3518554b7006386c21d0b4b51dc9061dae3213 Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 18 Mar 2019 16:56:15 +0000 Subject: [PATCH] Logout (#35) :C --- lib/global.dart | 19 +++++++++++++++ lib/pages/home.dart | 59 +++++++++++++++++++++++++++++++++------------ 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/lib/global.dart b/lib/global.dart index 818d4ac..061ae99 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,6 +77,20 @@ class VikunjaGlobalState extends State { }); } + 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 { var currentUser = await _storage.read(key: 'currentUser'); if (currentUser == null) { diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 652adb0..a8be62b 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -17,31 +17,59 @@ class HomePage extends StatefulWidget { class HomePageState extends State with AfterLayoutMixin { List _namespaces = []; + Namespace get _currentNamespace => _selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length ? _namespaces[_selectedDrawerIndex] : null; int _selectedDrawerIndex = -1; bool _loading = true; + bool _showUserDetails = false; @override void afterFirstLayout(BuildContext context) { _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(BuildContext context) { + return ListView(padding: EdgeInsets.zero, children: [ + ListTile( + title: Text('Logout'), + leading: Icon(Icons.exit_to_app), + onTap: () { + VikunjaGlobal.of(context).logoutUser(context); + }, + ), + ]); + } + + @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( @@ -49,6 +77,11 @@ class HomePageState extends State with AfterLayoutMixin { 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( @@ -61,17 +94,11 @@ class HomePageState extends State with AfterLayoutMixin { Theme.of(context).primaryColor, BlendMode.multiply)), ), ), - new Expanded( - child: this._loading - ? Center(child: CircularProgressIndicator()) - : RefreshIndicator( - child: ListView( - padding: EdgeInsets.zero, - children: ListTile.divideTiles( - context: context, tiles: drawerOptions) - .toList()), - onRefresh: _loadNamespaces, - )), + new Builder( + builder: (BuildContext context) => Expanded( + child: _showUserDetails + ? _userDetailsWidget(context) + : _namespacesWidget())), new Align( alignment: FractionalOffset.bottomCenter, child: Builder(