Newer
Older
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'package:http/http.dart' as http;
Future<bool> syncEvents() async {
final EventStoreInstance events = EventStoreInstance();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
String baseUrl = 'https://sandbox.sensor.awi.de/rest/sensors/events/putEvent/';
debugPrint('Number of Events: ' + events.events.length.toString());
debugPrint('Pending Events:');
var index = 0;
for (var event in events.events){
if (event.status == 'PENDING') {
debugPrint('Idx: ' + index.toString() + ' ' + event.toSensorJson().toString());
index++;
url = baseUrl + event.id.toString() + '?createVersion=false';
debugPrint('XXX ' + Uri.parse(url).toString());
final response = await http.put(Uri.parse(url),
headers: {
"Content-Type": "application/json",
"Cookie": "x-auth-token=72d9d4d20a33f87edca7e1ba01ce8db8"
},
body: event.toSensorJson().toString(),
encoding: Encoding.getByName("utf-8"),
);
debugPrint('put success');
event.status = 'EXPORTED'; //Update event status so that it is only exported once.
debugPrint(response.body.toString());
debugPrint(response.headers.toString());
debugPrint(response.headers['set-cookie']);
} else {
debugPrint('Header: ' + response.headers.toString());
debugPrint('Body: ' + response.body.toString());
debugPrint('StatusCode: ' + response.statusCode.toString());
throw Exception('Failed to put.');
}
}
}
return true;
}
class ViewEvents extends StatelessWidget {
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
103
104
105
@override
Widget build(BuildContext context) {
// Get singleton to access locally stored events:
final EventStoreInstance events = EventStoreInstance();
return Scaffold(
appBar: AppBar(
title: const Text("View Added Events"),
),
body: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: DataTable(
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>[
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)),
171
172
173
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
203
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,
initialValue: event.status,
onFieldSubmitted: (val) {
event.status = val; //Update Database
},
),
),
],
),
],
),
),
),
mainAxisAlignment: MainAxisAlignment.end,
FloatingActionButton.extended(
heroTag: null,
tooltip: 'Upload Events',
icon: null,
//showDialog(
// context: context,
// builder: (BuildContext context) => _buildPopupDialog(context),
//);
Widget _buildPopupDialog(BuildContext context) {
return AlertDialog(
title: const Text('Popup example'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Text("Hello"),
],
),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
textColor: Theme.of(context).primaryColor,
child: const Text('Close'),
),
],
);
}
//TODO: allow editing fields here. Check validation of input value!