From d9fc06b1262e68759fb72e7d4591de7d1210d59b Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Wed, 28 Jul 2021 18:11:45 +0200 Subject: [PATCH] WIP session in json file --- assets/files/session.json | 0 lib/Helpers/SessionHelper.dart | 39 +++++++ lib/Models/session.dart | 31 +++++ lib/Screens/login_screen.dart | 202 ++++++++++++++++++++++----------- pubspec.lock | 39 +++++-- pubspec.yaml | 3 + 6 files changed, 240 insertions(+), 74 deletions(-) create mode 100644 assets/files/session.json create mode 100644 lib/Helpers/SessionHelper.dart create mode 100644 lib/Models/session.dart diff --git a/assets/files/session.json b/assets/files/session.json new file mode 100644 index 0000000..e69de29 diff --git a/lib/Helpers/SessionHelper.dart b/lib/Helpers/SessionHelper.dart new file mode 100644 index 0000000..9f776b4 --- /dev/null +++ b/lib/Helpers/SessionHelper.dart @@ -0,0 +1,39 @@ +import 'dart:io'; + +import 'package:manager_app/Models/session.dart'; +import 'package:path_provider/path_provider.dart'; + +class SessionHelper { + // TODO instance LOAD FILE.. + Future get _localPath async { + final directory = await getApplicationDocumentsDirectory(); + + return directory.path; + } + + Future get _localFile async { + final path = await _localPath; + return File('$path/session.json'); + } + + Future writeSession(Session session) async { + final file = await _localFile; + + // Write the file + return file.writeAsString(session.toString()); + } + + Future readSession() async { + try { + final file = await _localFile; + + // Read the file + final contents = await file.readAsString(); + + return int.parse(contents); + } catch (e) { + // If encountering an error, return 0 + return 0; + } + } +} \ No newline at end of file diff --git a/lib/Models/session.dart b/lib/Models/session.dart new file mode 100644 index 0000000..73679e3 --- /dev/null +++ b/lib/Models/session.dart @@ -0,0 +1,31 @@ +class Session { + bool isRememberMe; + String host; + String email; + String password; + + Session({this.isRememberMe, this.host, this.email, this.password}); + + factory Session.fromJson(Map json) { + return new Session( + isRememberMe: json['isRememberMe'] as bool, + host: json['host'] as String, + email: json['email'] as String, + password: json['password'] as String + ); + } + + Map toMap() { + return { + 'isRememberMe': isRememberMe, + 'host': host, + 'email': email, + 'password': password + }; + } + + @override + String toString() { + return '{isRememberMe: $isRememberMe, host: $host, email: $email, password: $password}'; + } +} \ No newline at end of file diff --git a/lib/Screens/login_screen.dart b/lib/Screens/login_screen.dart index 58cc0e5..d93d8cb 100644 --- a/lib/Screens/login_screen.dart +++ b/lib/Screens/login_screen.dart @@ -1,4 +1,8 @@ +import 'dart:convert'; +import 'dart:io'; + import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:manager_app/Components/loading.dart'; import 'package:manager_app/Components/message_notification.dart'; import 'package:manager_app/Components/rounded_button.dart'; @@ -10,6 +14,7 @@ import 'package:manager_app/app_context.dart'; import 'package:manager_app/client.dart'; import 'package:manager_app/constants.dart'; import 'package:managerapi/api.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:provider/provider.dart'; class LoginScreen extends StatefulWidget { @@ -20,11 +25,12 @@ class LoginScreen extends StatefulWidget { } class _LoginScreenState extends State { - String email = "test@email.be"; // DEV - String password = "kljqsdkljqsd"; // DEV - String host = "http://192.168.31.96"; // DEV + String email; // DEV "test@email.be" + String password ; // DEV = "kljqsdkljqsd" + String host ; // DEV = "http://192.168.31.96" Client clientAPI; bool isLoading = false; + bool isRememberMe = false; void authenticateTRY(dynamic appContext) async { print("try auth.. "); @@ -48,6 +54,10 @@ class _LoginScreenState extends State { print(token.accessToken); setAccessToken(token.accessToken); + if (isRememberMe) { + // update JSON FILE + } + showNotification(Colors.lightGreen, kWhite, 'Connexion réussie', context); isError = false; @@ -131,80 +141,142 @@ class _LoginScreenState extends State { }); } + @override + void initState() { + _loadFromAsset(); + super.initState(); + } + + Future_loadFromAsset() async { // TODO remove and use JsonHelper + return await rootBundle.loadString("assets/files/session.json"); + } + @override Widget build(BuildContext context) { final appContext = Provider.of(context); Size size = MediaQuery.of(context).size; return Scaffold( - body: Center( - child: SingleChildScrollView( - child: Container( - height: size.height *0.5, - width: size.width *0.5, - decoration: BoxDecoration( - color: kWhite, - borderRadius: BorderRadius.circular(8.0), - boxShadow: [ - BoxShadow( - color: kWhite.withOpacity(0.3), - spreadRadius: 0.5, - blurRadius: 0.5, - offset: Offset(0, 1.5), // changes position of shadow + body: FutureBuilder( // TODO REMOVE IT and replace by JSON Helper (load file in main) (to setState ok rememberMe) + future: loadJsonSessionFile(), + builder: (context, AsyncSnapshot snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + return Center( + child: SingleChildScrollView( + child: Container( + height: size.height *0.5, + width: size.width *0.5, + decoration: BoxDecoration( + color: kWhite, + borderRadius: BorderRadius.circular(8.0), + boxShadow: [ + BoxShadow( + color: kWhite.withOpacity(0.3), + spreadRadius: 0.5, + blurRadius: 0.5, + offset: Offset(0, 1.5), // changes position of shadow + ), + ], + ), + child: Padding( + padding: const EdgeInsets.only(left: 60.0, right: 60.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + RoundedInputField( + hintText: "Host", + onChanged: (value) { + host = value; + }, + icon: Icons.home + ), + RoundedInputField( + hintText: "Email", + onChanged: (value) { + email = value; + }, + icon: Icons.person + ), + RoundedPasswordField( + onChanged: (value) { + password = value; + }, + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Checkbox( + checkColor: kTextLightColor, + activeColor: kPrimaryColor, + value: this.isRememberMe, + onChanged: (bool value) { + setState(() { + this.isRememberMe = value; + }); + }, + ), + Text("Se souvenir de moi", style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),), + ], + ), + ), + SizedBox(height: size.height * 0.02), + !isLoading ? RoundedButton( + text: "LOGIN", + fontSize: 25, + press: () { + authenticateTRY(appContext); + }, + ): Container( + height: size.height * 0.1, + child: Loading() + ), + ], + ), + ), + ), ), - ], - ), - child: Padding( - padding: const EdgeInsets.only(left: 60.0, right: 60.0), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - RoundedInputField( - hintText: "Host", - onChanged: (value) { - host = value; - }, - icon: Icons.home - ), - RoundedInputField( - hintText: "Email", - onChanged: (value) { - email = value; - }, - icon: Icons.person - ), - RoundedPasswordField( - onChanged: (value) { - password = value; - }, - ), - SizedBox(height: size.height * 0.02), - !isLoading ? RoundedButton( - text: "LOGIN", - fontSize: 25, - press: () { - authenticateTRY(appContext); - }, - ): Container( - height: size.height * 0.1, + ); + } else if (snapshot.connectionState == ConnectionState.none) { + return Text("No data"); + } else { + return Center( + child: Container( + height: size.height * 0.2, child: Loading() - ), - ], - ), - ), - ), - ), + ) + ); + } + } ), ); } - isValidApi(Client client) async { - print("TEST URL"); - try { - var configs = await client.configurationApi.configurationGet(); - return configs != null; - } catch (ex) { - return false; + Future loadJsonSessionFile() async { // TODO use jsonHelper in this method + String jsonString = await _loadFromAsset(); + final jsonResponse = jsonDecode(jsonString); + + try{ + var rememberMeJson = jsonResponse.rememberMe != null ? jsonResponse.rememberMe : false; + var hostJson = jsonResponse.host; + var emailJson = jsonResponse.email; + var passwordJson = jsonResponse.password; + + if (rememberMeJson && hostJson != null && emailJson != null && passwordJson != null) { + setState(() { + isRememberMe = rememberMeJson; + host = hostJson; + email = emailJson; + password = passwordJson; + }); + } + + } catch(Exception) { + } + + print(jsonResponse); } } \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index b7c1413..4afd60c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -64,6 +64,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.1" + crypt: + dependency: "direct main" + description: + name: crypt + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" cupertino_icons: dependency: "direct main" description: @@ -77,7 +91,14 @@ packages: name: dart_vlc url: "https://pub.dartlang.org" source: hosted - version: "0.0.7" + version: "0.0.9" + dart_vlc_ffi: + dependency: transitive + description: + name: dart_vlc_ffi + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.8" drag_and_drop_lists: dependency: "direct main" description: @@ -220,7 +241,7 @@ packages: source: hosted version: "1.8.0" path_provider: - dependency: transitive + dependency: "direct main" description: name: path_provider url: "https://pub.dartlang.org" @@ -232,14 +253,14 @@ packages: name: path_provider_linux url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.0.2" path_provider_macos: dependency: transitive description: name: path_provider_macos url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.0.2" path_provider_platform_interface: dependency: transitive description: @@ -253,7 +274,7 @@ packages: name: path_provider_windows url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.3" pedantic: dependency: transitive description: @@ -274,14 +295,14 @@ packages: name: plugin_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.0.1" process: dependency: transitive description: name: process url: "https://pub.dartlang.org" source: hosted - version: "4.2.1" + version: "4.2.3" provider: dependency: "direct main" description: @@ -363,7 +384,7 @@ packages: name: video_player url: "https://pub.dartlang.org" source: hosted - version: "2.1.6" + version: "2.1.12" video_player_platform_interface: dependency: transitive description: @@ -377,7 +398,7 @@ packages: name: video_player_web url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.2" win32: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 0725e13..a37d4dd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,8 @@ dependencies: dart_vlc: ^0.0.6 video_player: ^2.1.1 drag_and_drop_lists: ^0.3.2 + path_provider: ^2.0.2 + crypt: ^4.0.1 window_size: git: url: git://github.com/google/flutter-desktop-embedding.git @@ -68,6 +70,7 @@ flutter: assets: - assets/images/ - assets/animations/ + - assets/files/ # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware.