mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-03 19:54:12 +00:00 
			
		
		
		
	Can sign out of the application
This commit is contained in:
		@@ -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<bool> signedIn() async {
 | 
			
		||||
    return await get() != null;
 | 
			
		||||
@@ -19,19 +18,18 @@ class AccountCredentialsHelper {
 | 
			
		||||
  /// Set new login tokens
 | 
			
		||||
  Future<void> 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<LoginTokens> 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;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -38,4 +38,10 @@ class AccountHelper {
 | 
			
		||||
 | 
			
		||||
    return AuthResult.SUCCESS;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Sign out user
 | 
			
		||||
  Future<void> signOut() async {
 | 
			
		||||
    await AccountCredentialsHelper().set(null);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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<HomeRoute> {
 | 
			
		||||
        return Text("Conversations");
 | 
			
		||||
 | 
			
		||||
      case 1:
 | 
			
		||||
        return Text("Menu");
 | 
			
		||||
        return MenuScreen();
 | 
			
		||||
 | 
			
		||||
      default:
 | 
			
		||||
        throw "Invalid tab number : " + _currTab.toString();
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										66
									
								
								lib/ui/screens/menus_screen.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								lib/ui/screens/menus_screen.dart
									
									
									
									
									
										Normal file
									
								
							@@ -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<void> _confirmSignOut(BuildContext context) async  {
 | 
			
		||||
    final result = await showDialog<bool>(
 | 
			
		||||
      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: <Widget>[
 | 
			
		||||
            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: <Widget>[
 | 
			
		||||
        MenuTile(
 | 
			
		||||
          title: tr("Sign out"),
 | 
			
		||||
          onTap: () {
 | 
			
		||||
            _confirmSignOut(context);
 | 
			
		||||
          },
 | 
			
		||||
        )
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								lib/ui/tiles/menu_tile.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								lib/ui/tiles/menu_tile.dart
									
									
									
									
									
										Normal file
									
								
							@@ -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: <Widget>[
 | 
			
		||||
        Divider(),
 | 
			
		||||
        InkWell(
 | 
			
		||||
          onTap: onTap,
 | 
			
		||||
          child: Padding(
 | 
			
		||||
            padding: const EdgeInsets.all(16.0),
 | 
			
		||||
            child: Text(title),
 | 
			
		||||
          ),
 | 
			
		||||
        )
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user