WIP session in json file
This commit is contained in:
parent
3323aae3ed
commit
d9fc06b126
0
assets/files/session.json
Normal file
0
assets/files/session.json
Normal file
39
lib/Helpers/SessionHelper.dart
Normal file
39
lib/Helpers/SessionHelper.dart
Normal file
@ -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<String> get _localPath async {
|
||||||
|
final directory = await getApplicationDocumentsDirectory();
|
||||||
|
|
||||||
|
return directory.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<File> get _localFile async {
|
||||||
|
final path = await _localPath;
|
||||||
|
return File('$path/session.json');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<File> writeSession(Session session) async {
|
||||||
|
final file = await _localFile;
|
||||||
|
|
||||||
|
// Write the file
|
||||||
|
return file.writeAsString(session.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
lib/Models/session.dart
Normal file
31
lib/Models/session.dart
Normal file
@ -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<String, dynamic> 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<String, dynamic> toMap() {
|
||||||
|
return {
|
||||||
|
'isRememberMe': isRememberMe,
|
||||||
|
'host': host,
|
||||||
|
'email': email,
|
||||||
|
'password': password
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return '{isRememberMe: $isRememberMe, host: $host, email: $email, password: $password}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,8 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:manager_app/Components/loading.dart';
|
import 'package:manager_app/Components/loading.dart';
|
||||||
import 'package:manager_app/Components/message_notification.dart';
|
import 'package:manager_app/Components/message_notification.dart';
|
||||||
import 'package:manager_app/Components/rounded_button.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/client.dart';
|
||||||
import 'package:manager_app/constants.dart';
|
import 'package:manager_app/constants.dart';
|
||||||
import 'package:managerapi/api.dart';
|
import 'package:managerapi/api.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class LoginScreen extends StatefulWidget {
|
class LoginScreen extends StatefulWidget {
|
||||||
@ -20,11 +25,12 @@ class LoginScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _LoginScreenState extends State<LoginScreen> {
|
class _LoginScreenState extends State<LoginScreen> {
|
||||||
String email = "test@email.be"; // DEV
|
String email; // DEV "test@email.be"
|
||||||
String password = "kljqsdkljqsd"; // DEV
|
String password ; // DEV = "kljqsdkljqsd"
|
||||||
String host = "http://192.168.31.96"; // DEV
|
String host ; // DEV = "http://192.168.31.96"
|
||||||
Client clientAPI;
|
Client clientAPI;
|
||||||
bool isLoading = false;
|
bool isLoading = false;
|
||||||
|
bool isRememberMe = false;
|
||||||
|
|
||||||
void authenticateTRY(dynamic appContext) async {
|
void authenticateTRY(dynamic appContext) async {
|
||||||
print("try auth.. ");
|
print("try auth.. ");
|
||||||
@ -48,6 +54,10 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||||||
print(token.accessToken);
|
print(token.accessToken);
|
||||||
setAccessToken(token.accessToken);
|
setAccessToken(token.accessToken);
|
||||||
|
|
||||||
|
if (isRememberMe) {
|
||||||
|
// update JSON FILE
|
||||||
|
}
|
||||||
|
|
||||||
showNotification(Colors.lightGreen, kWhite, 'Connexion réussie', context);
|
showNotification(Colors.lightGreen, kWhite, 'Connexion réussie', context);
|
||||||
|
|
||||||
isError = false;
|
isError = false;
|
||||||
@ -131,13 +141,27 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
_loadFromAsset();
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String>_loadFromAsset() async { // TODO remove and use JsonHelper
|
||||||
|
return await rootBundle.loadString("assets/files/session.json");
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final appContext = Provider.of<AppContext>(context);
|
final appContext = Provider.of<AppContext>(context);
|
||||||
Size size = MediaQuery.of(context).size;
|
Size size = MediaQuery.of(context).size;
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: Center(
|
body: FutureBuilder( // TODO REMOVE IT and replace by JSON Helper (load file in main) (to setState ok rememberMe)
|
||||||
|
future: loadJsonSessionFile(),
|
||||||
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
||||||
|
if (snapshot.connectionState == ConnectionState.done) {
|
||||||
|
return Center(
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
child: Container(
|
child: Container(
|
||||||
height: size.height *0.5,
|
height: size.height *0.5,
|
||||||
@ -178,6 +202,26 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||||||
password = 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),
|
SizedBox(height: size.height * 0.02),
|
||||||
!isLoading ? RoundedButton(
|
!isLoading ? RoundedButton(
|
||||||
text: "LOGIN",
|
text: "LOGIN",
|
||||||
@ -194,17 +238,45 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
} 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 {
|
Future loadJsonSessionFile() async { // TODO use jsonHelper in this method
|
||||||
print("TEST URL");
|
String jsonString = await _loadFromAsset();
|
||||||
try {
|
final jsonResponse = jsonDecode(jsonString);
|
||||||
var configs = await client.configurationApi.configurationGet();
|
|
||||||
return configs != null;
|
try{
|
||||||
} catch (ex) {
|
var rememberMeJson = jsonResponse.rememberMe != null ? jsonResponse.rememberMe : false;
|
||||||
return 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
39
pubspec.lock
39
pubspec.lock
@ -64,6 +64,20 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.1"
|
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:
|
cupertino_icons:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -77,7 +91,14 @@ packages:
|
|||||||
name: dart_vlc
|
name: dart_vlc
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
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:
|
drag_and_drop_lists:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -220,7 +241,7 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "1.8.0"
|
version: "1.8.0"
|
||||||
path_provider:
|
path_provider:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: path_provider
|
name: path_provider
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
@ -232,14 +253,14 @@ packages:
|
|||||||
name: path_provider_linux
|
name: path_provider_linux
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.2"
|
||||||
path_provider_macos:
|
path_provider_macos:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: path_provider_macos
|
name: path_provider_macos
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.2"
|
||||||
path_provider_platform_interface:
|
path_provider_platform_interface:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -253,7 +274,7 @@ packages:
|
|||||||
name: path_provider_windows
|
name: path_provider_windows
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.1"
|
version: "2.0.3"
|
||||||
pedantic:
|
pedantic:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -274,14 +295,14 @@ packages:
|
|||||||
name: plugin_platform_interface
|
name: plugin_platform_interface
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.1"
|
||||||
process:
|
process:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: process
|
name: process
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.2.1"
|
version: "4.2.3"
|
||||||
provider:
|
provider:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -363,7 +384,7 @@ packages:
|
|||||||
name: video_player
|
name: video_player
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.6"
|
version: "2.1.12"
|
||||||
video_player_platform_interface:
|
video_player_platform_interface:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -377,7 +398,7 @@ packages:
|
|||||||
name: video_player_web
|
name: video_player_web
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.1"
|
version: "2.0.2"
|
||||||
win32:
|
win32:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@ -37,6 +37,8 @@ dependencies:
|
|||||||
dart_vlc: ^0.0.6
|
dart_vlc: ^0.0.6
|
||||||
video_player: ^2.1.1
|
video_player: ^2.1.1
|
||||||
drag_and_drop_lists: ^0.3.2
|
drag_and_drop_lists: ^0.3.2
|
||||||
|
path_provider: ^2.0.2
|
||||||
|
crypt: ^4.0.1
|
||||||
window_size:
|
window_size:
|
||||||
git:
|
git:
|
||||||
url: git://github.com/google/flutter-desktop-embedding.git
|
url: git://github.com/google/flutter-desktop-embedding.git
|
||||||
@ -68,6 +70,7 @@ flutter:
|
|||||||
assets:
|
assets:
|
||||||
- assets/images/
|
- assets/images/
|
||||||
- assets/animations/
|
- assets/animations/
|
||||||
|
- assets/files/
|
||||||
|
|
||||||
# An image asset can refer to one or more resolution-specific "variants", see
|
# An image asset can refer to one or more resolution-specific "variants", see
|
||||||
# https://flutter.dev/assets-and-images/#resolution-aware.
|
# https://flutter.dev/assets-and-images/#resolution-aware.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user