From 446eb31356e3de8055b249df310f7ccdc594725d Mon Sep 17 00:00:00 2001
From: Maximilian Betz <Maximilian.Betz@awi.de>
Date: Fri, 4 Feb 2022 15:13:57 +0100
Subject: [PATCH] requesting collections works, filling dropdown button not yet

---
 README.md              |  18 +++++
 lib/configuration.dart | 174 ++++++++++++++++++-----------------------
 lib/datamodel.dart     |  68 ++++++++++++++++
 3 files changed, 161 insertions(+), 99 deletions(-)

diff --git a/README.md b/README.md
index ab870c5..70dd01b 100644
--- a/README.md
+++ b/README.md
@@ -13,3 +13,21 @@ A cross plattform project for android and ios to log events offline everywhere a
 ## Questions:
 - How to store not uploaded events locally?  Dumping jsons to a file?, ObjectBox,  SharedPreferences, 
 
+
+
+## Local Base Data Concept
+- The configuration.dart age widget is used to get the online available information for available
+  devices and event creation. 
+- This includes: All collections, All devices part of a selected collection, Event operation types
+- This "base data" is required to provide the dropdown menu options on the addevent.dart page.
+- This "base data" needs to be stored persistently, so that it is available also offline!
+
+## Local Data Event Storage 
+- Events added on the addevent.dart page have to be stored locally persistently. At least until the 
+  are exported to sensor.awi.de
+ 
+## Configuration Page
+- Dropdown "Collection" to select a collection
+- Textfield to show current locally selected and offline available collection  
+- Button "get Collection devices from Sensor"
+- Button "store Collection devices locally"
diff --git a/lib/configuration.dart b/lib/configuration.dart
index 68b83cf..0083978 100644
--- a/lib/configuration.dart
+++ b/lib/configuration.dart
@@ -2,26 +2,8 @@ import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:http/http.dart' as http;
 import 'dart:convert';
+import 'datamodel.dart';
 
-class Collection {
-  final int id;
-  final String description;
-  final String collectionName;
-
-  const Collection({
-    required this.id,
-    required this.description,
-    required this.collectionName,
-  });
-
-  factory Collection.fromJson(Map<String, dynamic> json) {
-    return Collection(
-      id: json['id'],
-      collectionName: json['collectionName'],
-      description: json['description'],
-    );
-  }
-}
 
 Future<Collection> fetchCollection() async {
   print("Start HTTP GET++##");
@@ -40,6 +22,25 @@ Future<Collection> fetchCollection() async {
   }
 }
 
+Future<List<Collection>> fetchCollections() async {
+  print("Fetching Collections...");
+
+  final response = await http
+      .get(Uri.parse('https://sensor.awi.de/rest/sensors/collections/getAllCollections?pointInTime=2018-07-03T12%3A30%3A55.389Z'));
+
+  if (response.statusCode == 200) {
+    List<Collection> collectionList;
+    return (json.decode(response.body) as List)
+        .map((i) => Collection.fromJson(i))
+        .toList();
+
+  } else {
+    // If the server did not return a 200 OK response,
+    // then throw an exception.
+    throw Exception('Failed to load Collection');
+  }
+}
+
 
 class Configuration extends StatefulWidget {
   @override
@@ -47,51 +48,25 @@ class Configuration extends StatefulWidget {
 }
 
 class _MyHomePageState extends State<Configuration> {
-  int _counter = 0;
-  int _factor = 1;
-
-  String selectedValue = '1';
-  var available_items = [
-    '1',
-    '10',
-    '100',
-  ];
-
 
   late Future<Collection> futureCollection;
+  late Future<List<Collection>> futureCollections;
 
   @override
   void initState() {
     super.initState();
     futureCollection = fetchCollection();
-  }
-  late Future<String> eventTypes;
+    futureCollections = fetchCollections();
 
-
-  void _incrementCounter() {
-    setState(() {
-      _counter = _counter + _factor;
-    });
-  }
-
-  void _decrementCounter() {
-    setState(() {
-      _counter = _counter - _factor;
-    });
-  }
-
-  void _resetCounter() {
-    setState(() {
-      _counter = 0;
-    });
   }
+  late Future<String> eventTypes;
 
-  void _getCollections() {
-    setState(() {});
-  }
 
   @override
   Widget build(BuildContext context) {
+    final CollectionStoreInstance storedCollections = CollectionStoreInstance();
+    final CollectionCurrentInstance currentCollection = CollectionCurrentInstance();
+
     return Scaffold(
       appBar: AppBar(
         title: Text('Configuration'),
@@ -100,40 +75,51 @@ class _MyHomePageState extends State<Configuration> {
         child: Column(
           mainAxisAlignment: MainAxisAlignment.center,
           children: <Widget>[
-            const Text(
-              'You have pushed the button this many times:',
-            ),
-            Text(
-              '$_counter',
-              style: Theme.of(context).textTheme.headline2,
-            ),
-            DropdownButton(
-              value: selectedValue,
-              icon: const Icon(Icons.keyboard_arrow_down),
-              items: available_items.map((String available_items) {
-                return DropdownMenuItem(
-                  value: available_items,
-                  child: Text(available_items),
-                );
-              }).toList(),
-              onChanged: (String? newValue) {
-                setState(() {
-                  selectedValue = newValue!;
-                  _factor = int.parse(selectedValue);
-                });
-              },
+            FutureBuilder<List<Collection>>(
+                future: futureCollections,
+                builder: (context, snapshot){
+                  if (snapshot.hasData)
+                  {
+                    print(snapshot.data);
+
+                    print(futureCollections);
+                    //storedCollections.store = snapshot.data!;  // Store data locally
+                    //var listlen = storedCollections.store.length();
+                    //print('Length ###: ' + storedCollections.store.length().toString());
+
+                    return DropdownButtonFormField(
+                        value: currentCollection.store.collectionName,
+                        decoration: const InputDecoration(
+                          labelText: 'Chosen Collection',
+                        ),
+                        items:
+                        storedCollections.store.map((Collection collection) {
+                          return DropdownMenuItem(
+                            value: collection.collectionName,
+                            child: Text(collection.collectionName),
+                          );
+                        }).toList(),
+                        onChanged: (value) {
+                          currentCollection.store.collectionName = value.toString();
+                        }
+                    );
+                  }
+                  else{
+                    return CircularProgressIndicator();
+                  }
+                }
             ),
             FutureBuilder<Collection>(
-              future: futureCollection,
-              builder: (context, snapshot){
-                if (snapshot.hasData)
+                future: futureCollection,
+                builder: (context, snapshot){
+                  if (snapshot.hasData)
                   {
                     return Text(snapshot.data!.collectionName);
                   }
-                else{
-                  return CircularProgressIndicator();
+                  else{
+                    return CircularProgressIndicator();
+                  }
                 }
-              }
             ),
           ],
         ),
@@ -141,29 +127,19 @@ class _MyHomePageState extends State<Configuration> {
       bottomNavigationBar: Row(
         mainAxisAlignment: MainAxisAlignment.spaceBetween,
         children: [
-          FloatingActionButton(
-            heroTag: null,
-            onPressed: _incrementCounter,
-            tooltip: 'Increment',
-            child: const Icon(Icons.add),
-          ),
-          FloatingActionButton(
-            heroTag: null,
-            onPressed: _decrementCounter,
-            tooltip: 'Decrement',
-            child: const Text('-'),
-          ),
-          FloatingActionButton(
+          FloatingActionButton.extended(
             heroTag: null,
-            onPressed: _resetCounter,
-            tooltip: 'Reset to 0',
-            child: const Icon(Icons.delete),
+            tooltip: 'Request Collections from sensor.awi.de',
+            icon: Icon(Icons.download),
+            label: const Text("Get Collections"),
+            onPressed: null,
           ),
-          FloatingActionButton(
+          FloatingActionButton.extended(
             heroTag: null,
-            onPressed: _getCollections,
-            tooltip: 'Reset to 0',
-            child: const Text('Update'),
+            tooltip: 'Select Collection and download corresponding devices',
+            icon: Icon(Icons.save),
+            label: const Text('Select'),
+            onPressed: null,
           ),
         ],
       ),
diff --git a/lib/datamodel.dart b/lib/datamodel.dart
index 8e9b1e8..3d972fc 100644
--- a/lib/datamodel.dart
+++ b/lib/datamodel.dart
@@ -1,5 +1,31 @@
 import 'package:shared_preferences/shared_preferences.dart';
 
+
+class Collection {
+  int id;
+  String description;
+  String collectionName;
+
+  Collection({
+    required this.id,
+    required this.description,
+    required this.collectionName,
+  });
+
+  factory Collection.fromJson(Map<String, dynamic> json) {
+    return Collection(
+      id: json['id'],
+      collectionName: json['collectionName'],
+      description: json['description'],
+    );
+  }
+
+  @override
+  String toString(){
+    return collectionName;
+  }
+}
+
 class Device{
   int id;
   String urn;
@@ -63,6 +89,28 @@ class EventType{
   }
 }
 
+abstract class CollectionStoreBase {
+  List<Collection> store = [];
+}
+
+class CollectionStoreInstance extends CollectionStoreBase {
+  static final CollectionStoreInstance _instance = CollectionStoreInstance
+      ._internal();
+
+  factory CollectionStoreInstance() {
+    return _instance;
+  }
+
+  CollectionStoreInstance._internal() {
+    store = [];
+  }
+
+  void reset() {
+    store = [];
+  }
+}
+
+
 
 abstract class DeviceStoreBase {
   List<Device> store = [];
@@ -90,6 +138,10 @@ abstract class EventCurrentBase{
   Event store = Event(0, 'urn0', '', '', '', 'PENDING');
 }
 
+abstract class CollectionCurrentBase{
+  Collection store = Collection(id: -1, description: 'description', collectionName: 'name');
+}
+
 abstract class EventTypeStoreBase{
   List<EventType> store = [];
 
@@ -135,6 +187,22 @@ class EventCurrentInstance extends EventCurrentBase {
   }
 }
 
+class CollectionCurrentInstance extends CollectionCurrentBase {
+  static final CollectionCurrentInstance _instance = CollectionCurrentInstance._internal();
+
+  factory CollectionCurrentInstance(){
+    return _instance;
+  }
+
+  CollectionCurrentInstance._internal(){
+    store = Collection(collectionName: 'name', description: 'description', id: -1);
+  }
+
+  void reset(){
+    store = Collection(collectionName: 'name', description: 'description', id: -1);
+  }
+}
+
 class EventTypeStoreInstance extends EventTypeStoreBase{
   static final EventTypeStoreInstance _instance = EventTypeStoreInstance._internal();
 
-- 
GitLab