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();
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
appBar: AppBar(
title: const Text('Configuration'),
),
body: Container(
margin: const EdgeInsets.symmetric(horizontal: 5.0),
child:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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',
),
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 = [];
snapshot.data?.forEach((element) {
configuration.collections.add(element);
});
/*Initialize active collection with first received
if(configuration.currentCollection.id == -1){
configuration.currentCollection = configuration.collections[0];
futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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();
}
}
),
],
),
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
),
bottomNavigationBar: Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const SizedBox(width: 10.0),
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!
},
),
const SizedBox(width: 50),
FloatingActionButton.extended(
heroTag: null,
tooltip: 'Select Collection and download corresponding devices',
icon: const Icon(Icons.save),
label: const Text('Store'),
onPressed: () {
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 = 0;
events.currentEvent.urnId = 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');
}
});
});
//TODO: display failed as user feedback somehow
},
),
const SizedBox(width: 10.0),
],
//TODO: add scrowlable view here for landscape compatibility.