tablet-app/lib/Screens/Configuration/config_view.dart
2021-07-14 20:44:55 +02:00

293 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:managerapi/api.dart';
import 'package:provider/provider.dart';
import 'package:tablet_app/Components/Buttons/rounded_button.dart';
import 'package:tablet_app/Components/loading.dart';
import 'package:tablet_app/Components/rounded_input_field.dart';
import 'package:tablet_app/Helpers/DatabaseHelper.dart';
import 'package:tablet_app/Models/map-marker.dart';
import 'package:tablet_app/Models/tabletContext.dart';
import 'package:tablet_app/Screens/MainView/dropDown_configuration.dart';
import 'package:tablet_app/Screens/MainView/main_view.dart';
import 'package:tablet_app/app_context.dart';
import 'package:tablet_app/client.dart';
import 'package:tablet_app/constants.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'dart:io';
import 'package:unique_identifier/unique_identifier.dart';
class ConfigViewWidget extends StatefulWidget {
ConfigViewWidget();
@override
_ConfigViewWidget createState() => _ConfigViewWidget();
}
class _ConfigViewWidget extends State<ConfigViewWidget> {
Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution
String url = "http://192.168.31.96"; //DEV "http://192.168.31.96"
bool configOk = false;
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
SystemChrome.setEnabledSystemUIOverlays([]);
Size size = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: size.height,
width: size.width,
color: kBackgroundGrey,
child: Center(
child: Container(
height: size.height * 0.85,
width: size.width * 0.9,
child: configOk ? FutureBuilder(
future: getConfigurations(appContext),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(29),
color: kBackgroundLight,
),
height: size.height*0.3,
width: size.width*0.3,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 5),
child: Text(
"Choisir une configuration",
style: new TextStyle(
fontSize: 25
),
),
),
DropDownConfig(
configurations: snapshot.data,
onChange: (ConfigurationDTO configurationOut) async {
// CREATE DEVICE REQUEST
createDevice(configurationOut, appContext);
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) {
return MainViewWidget();
},
),
(Route<dynamic> route) => false // For pushAndRemoveUntil
);
},
),
],
),
),
);
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
} else {
return Center(
child: Container(
height: size.height * 0.2,
child: Loading()
)
);
}
}
) : Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(29),
color: kBackgroundLight,
),
height: size.height*0.3,
width: size.width*0.3,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Entrer l'url du manager",
style: new TextStyle(
fontSize: 25
),
),
),
Padding(
padding: const EdgeInsets.only(left: 50, right: 50),
child: RoundedInputField(
hintText: "URL",
onChanged: (value) {
setState(() {
url = value;
});
},
icon: Icons.language
),
),
RoundedButton(
text: "OK",
fontSize: 25,
press: () async {
var client = Client(url);
var isOk = await isValidApi(client);
if (isOk) {
Fluttertoast.showToast(
msg: "Connecté au manager",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.lightGreen,
textColor: Colors.white,
fontSize: 16.0
);
setState(() {
TabletAppContext tabletAppContext = new TabletAppContext();
tabletAppContext.host = url;
tabletAppContext.clientAPI = client;
appContext.setContext(tabletAppContext);
configOk = true;
});
} else {
Fluttertoast.showToast(
msg: "L'url ne correspond pas à un manager",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.redAccent,
textColor: Colors.white,
fontSize: 16.0
);
}
},
)
],
),
),
),
),
),
)
);
}
Future<void> createDevice(ConfigurationDTO configurationDTO, dynamic appContext) async {
TabletAppContext tabletAppContext = appContext.getContext();
DeviceDetailDTO newDevice = new DeviceDetailDTO();
newDevice.configurationId = configurationDTO.id;
newDevice.configuration = configurationDTO.label;
newDevice.connected = true;
newDevice.identifier = await UniqueIdentifier.serial;
newDevice.ipAddressWLAN = await getIP(true);
newDevice.ipAddressETH = await getIP(false);
newDevice.name = newDevice.ipAddressWLAN;
print(newDevice);
DeviceDetailDTO device = await tabletAppContext.clientAPI.deviceApi.deviceCreate(newDevice);
if (device != null) {
// STORE IT LOCALLY !!
TabletAppContext tabletAppContext = appContext.getContext();
tabletAppContext.id = device.identifier;
tabletAppContext.deviceId = device.id;
tabletAppContext.host = url;
tabletAppContext.language = "FR"; // By Default
tabletAppContext.configuration = configurationDTO;
appContext.setContext(tabletAppContext);
// STORE IT LOCALLY (SQLite)
await DatabaseHelper.instance.insert(tabletAppContext);
Fluttertoast.showToast(
msg: "La tablette a bien été créée",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.lightGreen,
textColor: Colors.white,
fontSize: 16.0
);
} else {
Fluttertoast.showToast(
msg: "Une erreur est survenue lors de la création de la tablette",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.deepOrangeAccent,
textColor: Colors.white,
fontSize: 16.0
);
}
}
isValidApi(Client client) async {
print("TEST URL");
try {
var configs = await client.configurationApi.configurationGet();
return configs != null;
} catch (ex) {
return false;
}
}
}
Future<String> getIP(bool isWLAN) async {
for (var interface in await NetworkInterface.list()) {
print('== Interface: ${interface.name} ==');
if (interface.name == "wlan0" && isWLAN) {
// wifi
return interface.addresses.first.address;
}
if (interface.name == "eth0" && !isWLAN) {
// wired
return interface.addresses.first.address;
}
}
return null;
}
boxDecoration(SectionDTO section) {
return BoxDecoration(
color: kBackgroundLight,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(30.0),
image: new DecorationImage(
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop),
image: new NetworkImage(
section.imageSource,
),
),
boxShadow: [
BoxShadow(
color: kBackgroundSecondGrey,
spreadRadius: 0.5,
blurRadius: 5,
offset: Offset(0, 1.5), // changes position of shadow
),
],
);
}
Future<List<ConfigurationDTO>> getConfigurations(dynamic appContext) async {
List<ConfigurationDTO> configurations = await appContext.getContext().clientAPI.configurationApi.configurationGet();
print("number of configurations " + configurations.length.toString());
configurations.forEach((element) {
print(element);
});
return configurations;
}