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/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<LoginScreen> {
|
||||
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<LoginScreen> {
|
||||
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<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
|
||||
Widget build(BuildContext context) {
|
||||
final appContext = Provider.of<AppContext>(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<dynamic> 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: <Widget>[
|
||||
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: <Widget>[
|
||||
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);
|
||||
}
|
||||
}
|
||||
39
pubspec.lock
39
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:
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user