Skip to content
Snippets Groups Projects
Commit aff8e325 authored by Maximilian Betz's avatar Maximilian Betz
Browse files

configuration widget status feedback partly done.

parent 7a150f31
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ class _MyHomePageState extends State<Configuration> { ...@@ -24,7 +24,7 @@ class _MyHomePageState extends State<Configuration> {
var database = DatabaseInstance(); var database = DatabaseInstance();
SensorConnector connector = SensorConnector(); SensorConnector connector = SensorConnector();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
String _status = '';
Future<void> dumpToFile() async{ Future<void> dumpToFile() async{
var date = DateTime.now().toUtc(); var date = DateTime.now().toUtc();
...@@ -54,78 +54,79 @@ class _MyHomePageState extends State<Configuration> { ...@@ -54,78 +54,79 @@ class _MyHomePageState extends State<Configuration> {
Future<void> updateConfiguration() async { Future<void> updateConfiguration() async {
final EventStoreInstance event = EventStoreInstance(); final EventStoreInstance event = EventStoreInstance();
String token = '';
List<Device> devices = [];
try{
token = await connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password);
devices = await connector.fetchCollectionDevices(configuration.currentCollection.id);
event.currentEvent.id = 0;
event.currentEvent.urnId = configuration.devices[0].id; //TODO: fix if devices are an empty list.
event.currentEvent.urn = configuration.devices[0].urn;
event.currentEvent.description = '';
event.currentEvent.label = '';
event.currentEvent.type = configuration.eventTypes[0].name;
var date = DateTime.now().toUtc();
var isoDate = date.toIso8601String();
event.currentEvent.startDate = isoDate;
event.currentEvent.endDate = isoDate;
configuration.initialized = true;
HapticFeedback.vibrate();
//TODO: display success user feedback
//TODO: store complete "configuration"
await event.storeToSharedPrefs();
await configuration.storeToSharedPrefs();
debugPrint('Configuration stored!');
futureAuthToken.then((value){ //NOTE: Counterintuitive async callback function
debugPrint("Login credentials correct! Auth Token: " + value); } on SocketException catch (e) {
debugPrint('Exception: $e');
futureDevices.then((value) { _status = 'No connection';
debugPrint("Device List complete"); setState(() {});
configuration.devices = value; } catch (e) {
debugPrint('Exception: $e');
futureEventTypes.then((value) async { _status = '$e';
debugPrint("Event Types List complete"); setState(() {});
configuration.eventTypes = value; }
debugPrint("EventType Json: " + jsonEncode( _status = 'Success. Login valid, configuration stored';
configuration.eventTypes[0])); setState(() {});
debugPrint("Device Json: " + jsonEncode( return;
configuration.devices[0]));
debugPrint("Current Collection Json: " + jsonEncode(
configuration.currentCollection));
debugPrint("Sensor Login Json: " + jsonEncode(
configuration.loginInformation));
try {
//Update id and urn for the add event widget with some initial data
event.currentEvent.id = 0;
event.currentEvent.urnId = configuration.devices[0]
.id; //TODO: fix if devices are an empty list.
event.currentEvent.urn = configuration.devices[0].urn;
event.currentEvent.description = '';
event.currentEvent.label = '';
event.currentEvent.type = configuration.eventTypes[0]
.name;
var date = DateTime.now().toUtc();
var isoDate = date.toIso8601String();
event.currentEvent.startDate = isoDate;
event.currentEvent.endDate = isoDate;
configuration.initialized = true;
HapticFeedback.vibrate();
//TODO: display success user feedback
//TODO: store complete "configuration"
} catch (e) {
throw Exception('Something went wrong try again');
}
await event.storeToSharedPrefs();
await configuration.storeToSharedPrefs();
debugPrint('Configuration stored!');
});
});
});
//TODO: display failed as user feedback somehow
} }
Future<void> fetchInitData() async {
@override try {
void initState() { //Start fetching all possible sensor data as soon as possible:
super.initState(); futureCollections = connector.fetchCollections();
futureEventTypes = connector.fetchEventTypes();
if (configuration.currentCollection.id != -1) {
futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id);
}
//Start fetching all possible sensor data as soon as possible: await futureCollections;
futureCollections = connector.fetchCollections(); await futureEventTypes;
futureEventTypes = connector.fetchEventTypes(); await futureDevices;
if (configuration.currentCollection.id != -1) { }catch(e){
futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); debugPrint(e.toString());
} }
futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); }
@override
void initState() {
super.initState();
fetchInitData();
} }
late Future<String> eventTypes; late Future<String> eventTypes;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final EventStoreInstance events = EventStoreInstance(); final EventStoreInstance events = EventStoreInstance();
...@@ -134,95 +135,109 @@ class _MyHomePageState extends State<Configuration> { ...@@ -134,95 +135,109 @@ class _MyHomePageState extends State<Configuration> {
title: const Text('Configuration'), title: const Text('Configuration'),
), ),
body: SingleChildScrollView( body: SingleChildScrollView(
child: Container( child: Container(
margin: const EdgeInsets.symmetric(horizontal: 5.0), margin: const EdgeInsets.symmetric(horizontal: 5.0),
child: child:
Center( Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const SizedBox(height: 50), const SizedBox(height: 50),
const Text( const Text(
'You must be online to do something here!', 'You must be online to do something here!',
style: TextStyle(fontSize: 14) style: TextStyle(fontSize: 14)
), ),
const SizedBox(height: 50), const SizedBox(height: 50),
TextFormField( TextFormField(
keyboardType: TextInputType.emailAddress, keyboardType: TextInputType.emailAddress,
autofocus: false, autofocus: false,
initialValue: configuration.loginInformation.mail, initialValue: configuration.loginInformation.mail,
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: 'Sensor E-Mail', border: OutlineInputBorder(),
hintText: 'User for writing events', labelText: 'Sensor E-Mail',
hintText: 'User for writing events',
),
onChanged: (value) {
configuration.loginInformation.mail = value;
},
onFieldSubmitted: (value) {
//futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password);
},
), ),
onChanged: (value) { const SizedBox(height: 15.0),
configuration.loginInformation.mail = value; TextFormField(
}, autofocus: false,
onFieldSubmitted: (value) { initialValue: configuration.loginInformation.password,
futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); decoration: const InputDecoration(
}, border: OutlineInputBorder(),
), labelText: 'Sensor Password',
TextFormField( hintText: 'Password for writing events',
autofocus: false, ),
initialValue: configuration.loginInformation.password, onChanged: (value){
decoration: const InputDecoration( configuration.loginInformation.password = value;
labelText: 'Sensor Password', },
hintText: 'Password for writing events', onFieldSubmitted: (value) {
//futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password);
},
), ),
onChanged: (value){ const SizedBox(height: 50),
configuration.loginInformation.password = value; FutureBuilder<List<Collection>>(
}, future: futureCollections,
onFieldSubmitted: (value) { builder: (context, snapshot){
futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); if (snapshot.hasData)
}, {
), configuration.collections = [];
const SizedBox(height: 50), snapshot.data?.forEach((element) {
FutureBuilder<List<Collection>>( configuration.collections.add(element);
future: futureCollections, });
builder: (context, snapshot){
if (snapshot.hasData) /*Initialize active collection with first received
{
configuration.collections = [];
snapshot.data?.forEach((element) {
configuration.collections.add(element);
});
/*Initialize active collection with first received
collection if not initialized yet*/ collection if not initialized yet*/
if(configuration.currentCollection.id == -1){ if(configuration.currentCollection.id == -1){
configuration.currentCollection = configuration.collections[0]; configuration.currentCollection = configuration.collections[0];
futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id);
} }
return DropdownButtonFormField( return DropdownButtonFormField(
value: configuration.currentCollection.collectionName, value: configuration.currentCollection.collectionName,
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: 'Chose Collection', labelText: 'Chose Collection',
), border: OutlineInputBorder(),
items: ),
configuration.collections.map((Collection collection) { items:
return DropdownMenuItem( configuration.collections.map((Collection collection) {
value: collection.collectionName, return DropdownMenuItem(
child: Text(collection.collectionName), value: collection.collectionName,
); child: Text(collection.collectionName),
}).toList(), );
onChanged: (value) { }).toList(),
configuration.currentCollection = configuration.getCollectionFromName(value.toString()); onChanged: (value) {
//Fetch new selected collection devices configuration.currentCollection = configuration.getCollectionFromName(value.toString());
futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); //Fetch new selected collection devices
} futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id);
); }
} );
else{ }
return const CircularProgressIndicator(); else{
return const CircularProgressIndicator();
}
} }
} ),
), const SizedBox(height: 50.0),
], TextFormField(
readOnly: true,
autofocus: false,
enabled: false,
controller: TextEditingController(
text: _status),
),
const SizedBox(height: 15.0),
],
),
), ),
), ),
), ),
),
bottomNavigationBar: Container( bottomNavigationBar: Container(
margin: const EdgeInsets.symmetric(vertical: 10.0), margin: const EdgeInsets.symmetric(vertical: 10.0),
child: Row( child: Row(
...@@ -233,7 +248,7 @@ class _MyHomePageState extends State<Configuration> { ...@@ -233,7 +248,7 @@ class _MyHomePageState extends State<Configuration> {
heroTag: null, heroTag: null,
tooltip: 'Export events to local .json file', tooltip: 'Export events to local .json file',
icon: const Icon(Icons.download), icon: const Icon(Icons.download),
label: const Text('Export Events'), label: const Text('Dump Events'),
onPressed: () { onPressed: () {
dumpToFile(); dumpToFile();
...@@ -248,7 +263,7 @@ class _MyHomePageState extends State<Configuration> { ...@@ -248,7 +263,7 @@ class _MyHomePageState extends State<Configuration> {
heroTag: null, heroTag: null,
tooltip: 'Download corresponding devices and store locally', tooltip: 'Download corresponding devices and store locally',
icon: const Icon(Icons.save), icon: const Icon(Icons.save),
label: const Text('Update'), label: const Text('Store'),
onPressed: () { onPressed: () {
updateConfiguration(); updateConfiguration();
//TODO: display failed as user feedback somehow //TODO: display failed as user feedback somehow
...@@ -262,4 +277,7 @@ class _MyHomePageState extends State<Configuration> { ...@@ -262,4 +277,7 @@ class _MyHomePageState extends State<Configuration> {
} }
} }
//TODO: write configuration on app dispose! //TODO: write configuration on app dispose!
\ No newline at end of file
//TODO: grey out update button if nothing has changed. ?
\ No newline at end of file
...@@ -28,7 +28,6 @@ Future<void> loadConfiguration() async { ...@@ -28,7 +28,6 @@ Future<void> loadConfiguration() async {
void main() { void main() {
loadConfiguration(); loadConfiguration();
//configuration.loginInformation = SensorLogin.fromJson({"mail":"admin","password":"adminadmin"}); //TODO: replace for productive version. //configuration.loginInformation = SensorLogin.fromJson({"mail":"admin","password":"adminadmin"}); //TODO: replace for productive version.
runApp(MaterialApp( runApp(MaterialApp(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment