Newer
Older
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'sensorconnector.dart';
class Configuration extends StatefulWidget {
@override
State<Configuration> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<Configuration> {
late Future<Collection> futureCollection;
late Future<List<Collection>> futureCollections;
late Future<List<EventType>> futureEventTypes;
late Future<List<Device>> futureDevices;
late Future<String> futureAuthToken;
SensorConnector connector = SensorConnector();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
@override
void initState() {
super.initState();
//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);
}
futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password);
late Future<String> eventTypes;
@override
Widget build(BuildContext context) {
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final EventStoreInstance events = EventStoreInstance();
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You must be online to do something here!',
TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: configuration.loginInformation.mail,
decoration: const InputDecoration(
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);
},
),
TextFormField(
autofocus: false,
initialValue: configuration.loginInformation.password,
decoration: const InputDecoration(
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);
},
),
const SizedBox(height: 50),
FutureBuilder<List<Collection>>(
future: futureCollections,
builder: (context, snapshot){
if (snapshot.hasData)
{
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);
return DropdownButtonFormField(
value: configuration.currentCollection.collectionName,
decoration: const InputDecoration(
),
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{
),
],
),
),
bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
FloatingActionButton.extended(
heroTag: null,
tooltip: 'Reset all data',
icon: const Icon(Icons.cancel),
label: const Text('Reset all'),
onPressed: () {
//events.reset(); //TODO: remove before release!
configuration.reset(); //TODO: remove before release this is not a real use case!
},
),
FloatingActionButton.extended(
tooltip: 'Select Collection and download corresponding devices',
icon: const Icon(Icons.save),
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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) {
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
events.currentEvent.id = configuration.devices[0]
.id; //TODO: fix if devices are an empty list.
events.currentEvent.urn = configuration.devices[0].urn;
events.currentEvent.description = '';
events.currentEvent.label = '';
events.currentEvent.type = configuration.eventTypes[0]
.name;
var date = DateTime.now().toUtc();
var isoDate = date.toIso8601String();
events.currentEvent.startDate = isoDate;
events.currentEvent.endDate = isoDate;
configuration.initialized = true;
HapticFeedback.vibrate();
//TODO: display success user feedback
} catch (e) {
throw Exception('Something went wrong try again');
}
});
),
],
),
);
}
}