Newer
Older
import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'package:http/http.dart' as http;
import 'sensorconnector.dart';
class ViewEvents extends StatefulWidget {
const ViewEvents({Key? key}) : super(key: key);
@override
_ViewEvents createState() => _ViewEvents();
}
EventStoreInstance _events = EventStoreInstance();
String _sync_status = '';
//TODO: add exception handling and display exceptions to user.
@override
void initState() {
_sync_status = _events.getPendingEventCount().toString() + ' event(s) pending';
super.initState();
}
//final EventStoreInstance events = EventStoreInstance();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
SensorConnector connection = SensorConnector();
int _synccounter = 0; //For displaying progress during event upload
String? token = await connection.getAuthToken(configuration.loginInformation.mail,
configuration.loginInformation.password);
_synccounter = _events.getPendingEventCount();
debugPrint('Number of Events: ' + _events.events.length.toString());
debugPrint('Number of Pending Events: ' + _synccounter.toString());
var index = 0;
for (var event in _events.events) {
if (event.status == 'PENDING') {
debugPrint('Idx: ' + index.toString() + ' ' +
event.toSensorJson().toString());
index++;
if (await connection.putEvent(event, token) == true){
//Event has been posted to Sensor.
debugPrint('put success, remaining events: ' + _synccounter.toString());
event.status = 'EXPORTED'; //Update event status so that it is only exported once.
}else{
_sync_status = 'export failed: ' + event.urn;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("View Added Events"),
),
body: Container(
margin: const EdgeInsets.symmetric(horizontal: 5.0),
child:
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: DataTable(
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
103
104
105
106
107
108
columns: const <DataColumn>[
DataColumn(
label: Text(
'Id',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'URN',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Label',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Type',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Description',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'StartDate',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'EndDate',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Latitude',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Longitude',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Elevation',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Status',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
],
rows: <DataRow>[
for (var event in _events.events)
DataRow(
cells: <DataCell>[
DataCell(Text(event.id.toString())),
DataCell(Text(event.urn)),
DataCell(
TextFormField(
initialValue: event.label,
onFieldSubmitted: (val) {
event.label = val; //Update Database
},
),
),
DataCell(Text(event.type)),
DataCell(
TextFormField(
initialValue: event.description,
onFieldSubmitted: (val) {
event.description = val; //Update Database
},
),
),
DataCell(Text(event.startDate)),
DataCell(Text(event.endDate)),
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
TextFormField(
readOnly: true,
initialValue: event.latitude,
onFieldSubmitted: (val) {
event.latitude = val; //Update Database
},
),
),
DataCell(
TextFormField(
readOnly: true,
initialValue: event.longitude,
onFieldSubmitted: (val) {
event.longitude = val; //Update Database
},
),
),
DataCell(
TextFormField(
readOnly: true,
initialValue: event.elevation,
onFieldSubmitted: (val) {
event.elevation = val; //Update Database
},
),
),
DataCell(
TextFormField(
readOnly: true,
controller: TextEditingController( //Required to update field here
text: event.status,
),
onFieldSubmitted: (val) {
event.status = val; //Update Database
},
),
),
],
),
],
),
),
),
const SizedBox(width: 10),
Text(_sync_status),
FloatingActionButton.extended(
heroTag: null,
tooltip: 'Upload Events',
icon: null,
//TODO: allow editing fields here. Check validation of input value!