From aff8e325f0949ba351b8f1be441c0f6b864f30ee Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Thu, 31 Mar 2022 16:44:54 +0200 Subject: [PATCH] configuration widget status feedback partly done. --- lib/configuration.dart | 310 ++++++++++++++++++++++------------------- lib/main.dart | 1 - 2 files changed, 164 insertions(+), 147 deletions(-) diff --git a/lib/configuration.dart b/lib/configuration.dart index 45b08ad..9b9b006 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -24,7 +24,7 @@ class _MyHomePageState extends State<Configuration> { var database = DatabaseInstance(); SensorConnector connector = SensorConnector(); final ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); - + String _status = ''; Future<void> dumpToFile() async{ var date = DateTime.now().toUtc(); @@ -54,78 +54,79 @@ class _MyHomePageState extends State<Configuration> { Future<void> updateConfiguration() async { 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); - - futureDevices.then((value) { - debugPrint("Device List complete"); - configuration.devices = value; - - futureEventTypes.then((value) async { - debugPrint("Event Types List complete"); - configuration.eventTypes = value; - - debugPrint("EventType Json: " + jsonEncode( - configuration.eventTypes[0])); - debugPrint("Device Json: " + jsonEncode( - 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 + + } on SocketException catch (e) { + debugPrint('Exception: $e'); + _status = 'No connection'; + setState(() {}); + } catch (e) { + debugPrint('Exception: $e'); + _status = '$e'; + setState(() {}); + } + + _status = 'Success. Login valid, configuration stored'; + setState(() {}); + return; } + Future<void> fetchInitData() async { - @override - void initState() { - super.initState(); + try { + //Start fetching all possible sensor data as soon as possible: + 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: - futureCollections = connector.fetchCollections(); - futureEventTypes = connector.fetchEventTypes(); - if (configuration.currentCollection.id != -1) { - futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); + await futureCollections; + await futureEventTypes; + await futureDevices; + }catch(e){ + debugPrint(e.toString()); } - futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); + } + + @override + void initState() { + super.initState(); + fetchInitData(); } late Future<String> eventTypes; @override Widget build(BuildContext context) { - final ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); final EventStoreInstance events = EventStoreInstance(); @@ -134,95 +135,109 @@ class _MyHomePageState extends State<Configuration> { title: const Text('Configuration'), ), body: SingleChildScrollView( - child: Container( - margin: const EdgeInsets.symmetric(horizontal: 5.0), - child: - Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: <Widget>[ - const SizedBox(height: 50), - const Text( - 'You must be online to do something here!', - style: TextStyle(fontSize: 14) - ), - const SizedBox(height: 50), - TextFormField( - keyboardType: TextInputType.emailAddress, - autofocus: false, - initialValue: configuration.loginInformation.mail, - decoration: const InputDecoration( - labelText: 'Sensor E-Mail', - hintText: 'User for writing events', + child: Container( + margin: const EdgeInsets.symmetric(horizontal: 5.0), + child: + Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: <Widget>[ + const SizedBox(height: 50), + const Text( + 'You must be online to do something here!', + style: TextStyle(fontSize: 14) + ), + const SizedBox(height: 50), + TextFormField( + keyboardType: TextInputType.emailAddress, + autofocus: false, + initialValue: configuration.loginInformation.mail, + decoration: const InputDecoration( + border: OutlineInputBorder(), + 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) { - configuration.loginInformation.mail = value; - }, - onFieldSubmitted: (value) { - futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); - }, - ), - TextFormField( - autofocus: false, - initialValue: configuration.loginInformation.password, - decoration: const InputDecoration( - labelText: 'Sensor Password', - hintText: 'Password for writing events', + const SizedBox(height: 15.0), + TextFormField( + autofocus: false, + initialValue: configuration.loginInformation.password, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Sensor Password', + hintText: 'Password for writing events', + ), + onChanged: (value){ + configuration.loginInformation.password = value; + }, + onFieldSubmitted: (value) { + //futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); + }, ), - onChanged: (value){ - configuration.loginInformation.password = value; - }, - onFieldSubmitted: (value) { - futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); - }, - ), - const SizedBox(height: 50), - FutureBuilder<List<Collection>>( - future: futureCollections, - builder: (context, snapshot){ - if (snapshot.hasData) - { - configuration.collections = []; - snapshot.data?.forEach((element) { - configuration.collections.add(element); - }); - - /*Initialize active collection with first received + const SizedBox(height: 50), + FutureBuilder<List<Collection>>( + future: futureCollections, + builder: (context, snapshot){ + if (snapshot.hasData) + { + configuration.collections = []; + snapshot.data?.forEach((element) { + configuration.collections.add(element); + }); + + /*Initialize active collection with first received collection if not initialized yet*/ - if(configuration.currentCollection.id == -1){ - configuration.currentCollection = configuration.collections[0]; - futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); - } + if(configuration.currentCollection.id == -1){ + configuration.currentCollection = configuration.collections[0]; + futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); + } - return DropdownButtonFormField( - value: configuration.currentCollection.collectionName, - decoration: const InputDecoration( - labelText: 'Chose Collection', - ), - items: - configuration.collections.map((Collection collection) { - return DropdownMenuItem( - value: collection.collectionName, - child: Text(collection.collectionName), - ); - }).toList(), - onChanged: (value) { - configuration.currentCollection = configuration.getCollectionFromName(value.toString()); - //Fetch new selected collection devices - futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); - } - ); - } - else{ - return const CircularProgressIndicator(); + return DropdownButtonFormField( + value: configuration.currentCollection.collectionName, + decoration: const InputDecoration( + labelText: 'Chose Collection', + border: OutlineInputBorder(), + ), + items: + configuration.collections.map((Collection collection) { + return DropdownMenuItem( + value: collection.collectionName, + child: Text(collection.collectionName), + ); + }).toList(), + onChanged: (value) { + configuration.currentCollection = configuration.getCollectionFromName(value.toString()); + //Fetch new selected collection devices + futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); + } + ); + } + 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( margin: const EdgeInsets.symmetric(vertical: 10.0), child: Row( @@ -233,7 +248,7 @@ class _MyHomePageState extends State<Configuration> { heroTag: null, tooltip: 'Export events to local .json file', icon: const Icon(Icons.download), - label: const Text('Export Events'), + label: const Text('Dump Events'), onPressed: () { dumpToFile(); @@ -248,7 +263,7 @@ class _MyHomePageState extends State<Configuration> { heroTag: null, tooltip: 'Download corresponding devices and store locally', icon: const Icon(Icons.save), - label: const Text('Update'), + label: const Text('Store'), onPressed: () { updateConfiguration(); //TODO: display failed as user feedback somehow @@ -262,4 +277,7 @@ class _MyHomePageState extends State<Configuration> { } } -//TODO: write configuration on app dispose! \ No newline at end of file +//TODO: write configuration on app dispose! + + +//TODO: grey out update button if nothing has changed. ? \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 77b0403..be3de5c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -28,7 +28,6 @@ Future<void> loadConfiguration() async { void main() { loadConfiguration(); - //configuration.loginInformation = SensorLogin.fromJson({"mail":"admin","password":"adminadmin"}); //TODO: replace for productive version. runApp(MaterialApp( -- GitLab