From 8cecc49e18df9115c12cf3c7684f25b80bbaa083 Mon Sep 17 00:00:00 2001
From: Maximilian Betz <Maximilian.Betz@awi.de>
Date: Tue, 15 Feb 2022 15:52:07 +0100
Subject: [PATCH] cleaned up adding event gnss code

---
 lib/addevent.dart | 99 ++++++++++++++++++++++++-----------------------
 1 file changed, 50 insertions(+), 49 deletions(-)

diff --git a/lib/addevent.dart b/lib/addevent.dart
index 7aa1958..9553536 100644
--- a/lib/addevent.dart
+++ b/lib/addevent.dart
@@ -10,6 +10,7 @@ class AddEvent extends StatefulWidget {
 }
 
 class _AddEventPageState extends State<AddEvent>  {
+  bool disableGnssEditing = true;
   final List<bool> _isGNSSSelected = [true];
   bool servicestatus = false;
   bool haspermission = false;
@@ -37,9 +38,10 @@ class _AddEventPageState extends State<AddEvent>  {
     alt = position.altitude.toString();
     accuracy = position.accuracy;
 
+    if (mounted){
     setState(() {
       //refresh UI
-    });
+    });};
     StreamSubscription<Position> positionStream = Geolocator.getPositionStream(
         locationSettings: locationSettings).listen((Position position) {
       print('Get Location: Lat:' + position.latitude.toString() +
@@ -51,14 +53,18 @@ class _AddEventPageState extends State<AddEvent>  {
       alt = position.altitude.toString();
       accuracy = position.accuracy;
 
-      setState(() {
-        //refresh UI on update
-      });
+
+      if (disableGnssEditing == true) {
+        if(mounted){
+        setState(() {
+          //refresh UI on update
+        });};
+      };
     });
   }
 
 
-  checkGnssPermission() async {
+  startGNSS() async {
     print("CheckGnssPermission");
     servicestatus = await Geolocator.isLocationServiceEnabled();
     if(servicestatus){
@@ -79,24 +85,25 @@ class _AddEventPageState extends State<AddEvent>  {
 
       if(haspermission){
         print('Location permissions granted');
+        if(this.mounted){
         setState(() {
           //refresh the UI
-        });
+        });};
 
         getLocation();
       }
     }else{
       print("GPS Service is not enabled, turn on GPS location");
     }
-
+    if(this.mounted){
     setState(() {
       //refresh the UI
-    });
+    });};
   }
 
   @override
   void initState() {
-    checkGnssPermission();
+    startGNSS();
     super.initState();
   }
 
@@ -115,7 +122,6 @@ class _AddEventPageState extends State<AddEvent>  {
   }
 
   //TODO: add field validators for freetext fields. Check allowed characters in sensor.awi.de
-  //TODO: (Description, Label, Latitude, Longitude, Elevation, )
 
   @override
   Widget build(BuildContext context) {
@@ -124,34 +130,27 @@ class _AddEventPageState extends State<AddEvent>  {
     final DeviceStoreInstance availableDevice = DeviceStoreInstance();
     final EventStoreInstance storedEvents = EventStoreInstance();
     final EventCurrentInstance currentEvent = EventCurrentInstance();
+    String gnssStatusText = "";
 
-    final bool _disable_gnss_editing = _isGNSSSelected[0];
-    String _gnss_status_text = "";
-
-    //print("###"+currentEvent.store.toString());
-
-    //final TextEditingController _controllerLat = TextEditingController(text: currentEvent.store.latitude);
-    final TextEditingController _controllerLat = TextEditingController(text: "LAT123");
-    //final TextEditingController _controllerLong = TextEditingController(text: currentEvent.store.longitude);
-    final TextEditingController _controllerLong = TextEditingController(text: "Long123");
-    //final TextEditingController _controllerAlt = TextEditingController(text: currentEvent.store.elevation);
-    final TextEditingController _controllerAlt = TextEditingController(text: "Alt123");
+    if (true == disableGnssEditing){
 
-    //currentEvent.store.
-    if (true == _isGNSSSelected[0]){
-      _controllerLat.text = lat;
-      _controllerLong.text = long;
-      _controllerAlt.text = alt;
+      // Update current event coordinates
+      currentEvent.store.latitude = lat;
+      currentEvent.store.longitude = long;
+      currentEvent.store.elevation = alt;
 
-      _gnss_status_text = "Precision: "+ accuracy.toStringAsFixed(2) +"m";
+      if(accuracy == 0.0){
+        gnssStatusText = "No-Fix";
+        }
+      else{
+        gnssStatusText = "Precision: "+ accuracy.toStringAsFixed(2) +"m";
+      }
     }
     else{
-      _gnss_status_text = "Off";
-      // do nothing and keep old value,
+      // Just display old event coordinates
+      gnssStatusText = "GNSS Disabled";
     }
 
-
-
     return Scaffold(
       appBar: AppBar(title: const Text("Add Event")),
       body:
@@ -210,28 +209,29 @@ class _AddEventPageState extends State<AddEvent>  {
               },
             ),
             TextField(
-              readOnly: true,
-              enabled: !_disable_gnss_editing,
-              controller: _controllerLat,
-              decoration: InputDecoration(
+              readOnly: false,
+              enabled: !disableGnssEditing,
+              //controller: _controllerLat,
+              controller: TextEditingController(text: currentEvent.store.latitude.toString()),
+              decoration: const InputDecoration(
                 labelText: 'Latitude',
                 border: OutlineInputBorder(),
               ),
             ),
             TextField(
-              readOnly: true,
-              enabled: !_disable_gnss_editing,
-              controller: _controllerLong,
-              decoration: InputDecoration(
+              readOnly: false,
+              enabled: !disableGnssEditing,
+              controller: TextEditingController(text: currentEvent.store.longitude.toString()),
+              decoration: const InputDecoration(
                 labelText: 'Longitude',
                 border: OutlineInputBorder(),
               ),
             ),
             TextField(
-              readOnly: true,
-              enabled: !_disable_gnss_editing,
-              controller: _controllerAlt,
-              decoration: InputDecoration(
+              readOnly: false,
+              enabled: !disableGnssEditing,
+              controller: TextEditingController(text: currentEvent.store.elevation.toString()),
+              decoration: const InputDecoration(
                 labelText: 'Elevation',
                 border: OutlineInputBorder(),
               ),
@@ -240,27 +240,28 @@ class _AddEventPageState extends State<AddEvent>  {
       bottomNavigationBar: Row(
         mainAxisAlignment: MainAxisAlignment.end,
         children: [
-          Text(_gnss_status_text),
-          SizedBox(width: 50),
+          Text(gnssStatusText),
+          const SizedBox(width: 50),
           ToggleButtons(
-            children: <Widget>[
+            children: const <Widget>[
               Icon(Icons.my_location),
             ],
             isSelected: _isGNSSSelected,
             onPressed: (int index) {
               setState(() {
                 _isGNSSSelected[index] = !_isGNSSSelected[index];
+                disableGnssEditing = _isGNSSSelected[index];
               });
             },
           ),
-          SizedBox(width: 50),
+          const SizedBox(width: 50),
           FloatingActionButton(
             heroTag: null,
             onPressed: _storeCurrentEvent,
             tooltip: 'Add Event',
-            child: Icon(Icons.add),
+            child: const Icon(Icons.add),
           ),
-          SizedBox(width: 20),
+          const SizedBox(width: 20),
         ],
       ),
     );
-- 
GitLab