diff --git a/lib/helpers/account_credentials_helper.dart b/lib/helpers/account_credentials_helper.dart index 3c3409f..22b2523 100644 --- a/lib/helpers/account_credentials_helper.dart +++ b/lib/helpers/account_credentials_helper.dart @@ -10,7 +10,6 @@ import 'package:shared_preferences/shared_preferences.dart'; /// @author Pierre HUBERT class AccountCredentialsHelper { - /// Checkout whether current user is signed in or not Future signedIn() async { return await get() != null; @@ -19,21 +18,20 @@ class AccountCredentialsHelper { /// Set new login tokens Future set(LoginTokens tokens) async { SharedPreferences prefs = await SharedPreferences.getInstance(); - await prefs.setString("login_tokens", tokens.toString()); + await prefs.setString( + "login_tokens", tokens == null ? "null" : tokens.toString()); } /// Get current [LoginTokens]. Returns null if none or in case of failure Future get() async { try { - SharedPreferences prefs = await SharedPreferences.getInstance(); final string = prefs.getString("login_tokens"); - if(string == null) return null; + if (string == null || string == "null") return null; return LoginTokens.fromJSON(jsonDecode(string)); - - } on Exception catch(e){ + } on Exception catch (e) { print(e.toString()); return null; } } -} \ No newline at end of file +} diff --git a/lib/helpers/account_helper.dart b/lib/helpers/account_helper.dart index f4873ff..9dc561e 100644 --- a/lib/helpers/account_helper.dart +++ b/lib/helpers/account_helper.dart @@ -38,4 +38,10 @@ class AccountHelper { return AuthResult.SUCCESS; } + + /// Sign out user + Future signOut() async { + await AccountCredentialsHelper().set(null); + } + } diff --git a/lib/ui/routes/home_route.dart b/lib/ui/routes/home_route.dart index ec0280a..77c205f 100644 --- a/lib/ui/routes/home_route.dart +++ b/lib/ui/routes/home_route.dart @@ -1,3 +1,4 @@ +import 'package:comunic/ui/screens/menus_screen.dart'; import 'package:comunic/ui/tiles/CustomBottomNavigationBarItem.dart'; import 'package:flutter/material.dart'; @@ -50,7 +51,7 @@ class _HomeRouteState extends State { return Text("Conversations"); case 1: - return Text("Menu"); + return MenuScreen(); default: throw "Invalid tab number : " + _currTab.toString(); diff --git a/lib/ui/screens/menus_screen.dart b/lib/ui/screens/menus_screen.dart new file mode 100644 index 0000000..21aa318 --- /dev/null +++ b/lib/ui/screens/menus_screen.dart @@ -0,0 +1,66 @@ +import 'package:comunic/helpers/account_helper.dart'; +import 'package:comunic/ui/routes/login_route.dart'; +import 'package:comunic/ui/tiles/menu_tile.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:flutter/material.dart'; + +/// Menu screen +/// +/// @author Pierre HUBERT + +class MenuScreen extends StatelessWidget { + /// Ask the user if he really wants to sign out from the application + Future _confirmSignOut(BuildContext context) async { + final result = await showDialog( + context: context, + builder: (c) { + return AlertDialog( + title: Text(tr("Confirm sign out")), + content: Text( + tr("Do your really want to sign out from the application ?")), + actions: [ + FlatButton( + child: Text(tr("Cancel").toUpperCase()), + onPressed: () { + Navigator.pop(context, false); + }, + ), + FlatButton( + child: Text( + tr("Sign out").toUpperCase(), + style: TextStyle(color: Colors.red), + ), + onPressed: () { + Navigator.pop(context, true); + }, + ), + ], + ); + }, + ); + + if(result == null || !result) + return; + + await AccountHelper().signOut(); + + Navigator.pushReplacement(context, MaterialPageRoute(builder: (c){ + return LoginRoute(); + })); + + } + + @override + Widget build(BuildContext context) { + return ListView( + children: [ + MenuTile( + title: tr("Sign out"), + onTap: () { + _confirmSignOut(context); + }, + ) + ], + ); + } +} diff --git a/lib/ui/tiles/menu_tile.dart b/lib/ui/tiles/menu_tile.dart new file mode 100644 index 0000000..748b8b4 --- /dev/null +++ b/lib/ui/tiles/menu_tile.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; + +/// Menu tile +/// +/// @author Pierre HUBERT + +class MenuTile extends StatelessWidget { + final String title; + final GestureTapCallback onTap; + + const MenuTile({@required this.title, this.onTap}) : assert(title != null); + + @override + Widget build(BuildContext context) { + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Divider(), + InkWell( + onTap: onTap, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Text(title), + ), + ) + ], + ); + } +}