Skip to content
Snippets Groups Projects
Commit 71b994f2 authored by Maximilian Betz's avatar Maximilian Betz
Browse files

fixed bug. It is no longer possible to start two sync processes in parallel

parent 81b4d598
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'sensorconnector.dart';
import 'databaseconnector.dart';
import 'package:getwidget/getwidget.dart';
class ViewEvents extends StatefulWidget {
const ViewEvents({Key? key}) : super(key: key);
......@@ -23,65 +22,7 @@ class _ViewEvents extends State<ViewEvents> {
bool _cancelSync = false; //Flag to cancel sync process
bool _currentlySyncing = false; // State flag to indicate if sync is ongoing
var database = DatabaseInstance();
List<Event> _localEvents = [];
Widget _buildSyncDialog(BuildContext context) {
SensorConnector sensorConnector = SensorConnector();
String startButtonText = '';
String closeButtonText = '';
if (_currentlySyncing == true){
startButtonText = 'Syncing';
closeButtonText = 'Cancel';
}else{
startButtonText = 'Start';
closeButtonText = 'Close';
}
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
const Text('Syncing Events...'),
const SizedBox(height: 15.0),
GFProgressBar(
percentage: 0.7, //Get percentage from global variable here
backgroundColor : Colors.black12,
progressBarColor: Colors.blue,
),
const SizedBox(height: 5.0),
Text(_syncedEventCnt + ' / ' + _pendingEventCnt),
//TODO: show text XX pending events. count down
],
),
actions: <Widget>[
TextButton(
onPressed: () {
if(_currentlySyncing == false){
syncEvents(); //Initiate sync
}
},
child: Text(startButtonText), //Change text to Syncing... during Sync process.
),
TextButton(
onPressed: () {
// Depending on state. Cancel or close. TODO: implement statemachine.
if(_currentlySyncing){
_cancelSync = true; // Set flag so that async tasks stops syncing
}else{
Navigator.of(context).pop(); // Close widget only if sync has stopped.
}
},
child: Text(closeButtonText), // Change to Cancel during Sync process.
),
],
)
);
}
Future<void> fetchEventsFromDb() async{
final EventStoreInstance event = EventStoreInstance();
......@@ -113,14 +54,15 @@ class _ViewEvents extends State<ViewEvents> {
@override
void dispose() async {
_cancelSync = true;
/*Async update current configuration to shared preferences*/
final EventStoreInstance event = EventStoreInstance();
event.storeToSharedPrefs();
super.dispose();
debugPrint("View Events disposed");
}
Future<void> syncEvents() async {
_cancelSync = false;
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
SensorConnector connection = SensorConnector();
......@@ -140,6 +82,7 @@ class _ViewEvents extends State<ViewEvents> {
configuration.loginInformation.password);
var index = 0;
for (var event in events) {
debugPrint("Check ABC");
if(_cancelSync == true){
_currentlySyncing = false;
debugPrint('Sync process canceled');
......@@ -151,14 +94,18 @@ class _ViewEvents extends State<ViewEvents> {
event.toSensorJson().toString());
index++;
if (await connection.putEvent(event, token) == true) {
//Event has been posted to Sensor.
syncCounter--;
debugPrint(
'put success, remaining events: ' + syncCounter.toString());
debugPrint('put success, now pending: ' + syncCounter.toString());
event.status = 'EXPORTED'; //Update event to export only once
database.updateEvent(event); //Update Event as exported in SQL Database
await fetchEventsFromDb(); //update view list //TODO: this is bad for the sync performance!
setState(() {});
debugPrint('updated database');
////Update view list TODO: this is bad for the sync performance!
await fetchEventsFromDb();
if(mounted) {
setState(() {});
}
} else {
throw Exception('Sync for ' + event.urn + 'failed');
}
......@@ -171,7 +118,9 @@ class _ViewEvents extends State<ViewEvents> {
errorText = errorText.substring(10, errorText.length); //Remove 'Exception' from string.
_status = errorText;
_statusStyle = const TextStyle(color: Colors.red);
setState(() {});
if(mounted) {
setState(() {});
}
}
}else{
debugPrint('No PENDING event(s)');
......@@ -330,15 +279,13 @@ class _ViewEvents extends State<ViewEvents> {
icon: null,
label: const Text('Sync'),
onPressed: () {
syncEvents();
//showDialog(
// barrierDismissible: false,
// context: context,
// builder: (BuildContext context) => _buildSyncDialog(context),
//);
if(_currentlySyncing == false){
_currentlySyncing = true;
syncEvents();
}else{
debugPrint('Sync already running, will not start a second one');
}
},
),
],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment