282 lines
9.7 KiB
Dart
282 lines
9.7 KiB
Dart
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';
|
|
import 'package:manager_app/Components/rounded_input_field.dart';
|
|
import 'package:manager_app/Components/rounded_password_field.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/Screens/Main/main_screen.dart';
|
|
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 {
|
|
LoginScreen({Key key}) : super(key: key);
|
|
|
|
@override
|
|
_LoginScreenState createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
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.. ");
|
|
print(this.email);
|
|
print(this.password);
|
|
|
|
clientAPI = Client(this.host);
|
|
|
|
var isError = true;
|
|
if (this.email != null && this.password != null && this.host != null) {
|
|
|
|
// if () {} // Add if token exist and not null + not expired
|
|
try {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
LoginDTO loginDTO = new LoginDTO(email: email, password: password);
|
|
TokenDTO token = await clientAPI.authenticationApi.authenticationAuthenticateWithJson(loginDTO);
|
|
print("Token ??");
|
|
print(token);
|
|
print(token.accessToken);
|
|
setAccessToken(token.accessToken);
|
|
|
|
if (isRememberMe) {
|
|
// update JSON FILE
|
|
}
|
|
|
|
showNotification(Colors.lightGreen, kWhite, 'Connexion réussie', context);
|
|
|
|
isError = false;
|
|
// Set the appContext
|
|
if (appContext.getContext() == null) {
|
|
ManagerAppContext managerAppContext = new ManagerAppContext();
|
|
// store user info locally
|
|
managerAppContext.email = email;
|
|
managerAppContext.token = token;
|
|
managerAppContext.clientAPI = clientAPI;
|
|
print(managerAppContext);
|
|
|
|
appContext.setContext(managerAppContext);
|
|
}
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
|
|
Navigator.pushAndRemoveUntil(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return MainScreen();
|
|
},
|
|
),
|
|
(Route<dynamic> route) => false // For pushAndRemoveUntil
|
|
);
|
|
}
|
|
catch (e) {
|
|
print("error auth");
|
|
print(e);
|
|
showNotification(Colors.orange, kWhite, 'Un problème est survenu lors de la connexion', context);
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
if (!isError) {
|
|
/*UserDetailDTO user = await clientAPI.userApi.userGetDetail("6071f74aaf542f03116f2b9b");
|
|
print("user values ??");
|
|
print(user.email);
|
|
print(user.id);
|
|
|
|
List<ConfigurationDTO> configurations = await clientAPI.configurationApi.configurationGet();
|
|
print("number of configurations " + configurations.length.toString());
|
|
configurations.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
/*List<SectionDTO> sections = await clientAPI.sectionApi.sectionGetFromConfiguration(id);
|
|
print("number of sections " + sections.length.toString());
|
|
sections.forEach((element) {
|
|
print(element);
|
|
});*/
|
|
|
|
SectionDTO section = await clientAPI.sectionApi.sectionGetDetail("60916249494b9eaf283b44f7");
|
|
print(section);
|
|
|
|
List<ResourceDTO> resources = await clientAPI.resourceApi.resourceGet();
|
|
print("number of resources " + resources.length.toString());
|
|
resources.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
List<DeviceDTO> devices = await clientAPI.deviceApi.deviceGet();
|
|
print("number of devices " + devices.length.toString());
|
|
devices.forEach((element) {
|
|
print(element);
|
|
});*/
|
|
}
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
clientAPI.apiApi.authentications.forEach((key, auth) {
|
|
if (auth is OAuth) {
|
|
auth.accessToken = accessToken;
|
|
}
|
|
});
|
|
}
|
|
|
|
@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: 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()
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
} else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text("No data");
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
height: size.height * 0.2,
|
|
child: Loading()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
),
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |