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

sync progress alert dialog partly done

parent d073bed3
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'sensorconnector.dart';
import 'databaseconnector.dart';
//import 'package:getwidget/getwidget.dart';
import 'package:getwidget/getwidget.dart';
class ViewEvents extends StatefulWidget {
const ViewEvents({Key? key}) : super(key: key);
......@@ -18,14 +18,27 @@ class _ViewEvents extends State<ViewEvents> {
TextStyle _statusStyle = const TextStyle(color: Colors.black);
String _appBarText = '';
int _selectedRow = -1;
String _pendingEventCnt = '';
String _syncedEventCnt = '';
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 = [];
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(
......@@ -34,36 +47,40 @@ class _ViewEvents extends State<ViewEvents> {
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
const Text('Syncing Events...'),
//TODO: show progress bar here., Horizontal progress bar?
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
//GFProgressBar(
// percentage: 0.7,
// backgroundColor : Colors.pink,
// progressBarColor: Colors.red,
//),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
//Start sync process but only once.
if(_currentlySyncing == false){
syncEvents(); //Initiate sync
}
},
child: const Text('Start'), //Change text to Syncing... during Sync process.
child: Text(startButtonText), //Change text to Syncing... during Sync process.
),
TextButton(
onPressed: () {
// Depending on state. Cancel or close. TODO: implement statemachine.
Navigator.of(context).pop();
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: const Text('Close'), // Change to Cancel during Sync process.
child: Text(closeButtonText), // Change to Cancel during Sync process.
),
],
)
);
}
Future<void> fetchEventsFromDb() async{
......@@ -103,11 +120,14 @@ class _ViewEvents extends State<ViewEvents> {
}
Future<void> syncEvents() async {
_cancelSync = false;
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
SensorConnector connection = SensorConnector();
List<Event> events = await database.getPendingEvents();
int syncCounter = events.length;
_pendingEventCnt = syncCounter.toString();
debugPrint('Pending Events');
for (var event in events){
debugPrint(event.toString());
......@@ -120,6 +140,13 @@ class _ViewEvents extends State<ViewEvents> {
configuration.loginInformation.password);
var index = 0;
for (var event in events) {
if(_cancelSync == true){
_currentlySyncing = false;
debugPrint('Sync process canceled');
return;
}
_currentlySyncing = true;
debugPrint('Idx: ' + index.toString() + ' ' +
event.toSensorJson().toString());
index++;
......@@ -138,6 +165,7 @@ class _ViewEvents extends State<ViewEvents> {
}
_status = 'export success';
_statusStyle = const TextStyle(color: Colors.green);
_syncedEventCnt = index.toString();
} catch (e) {
String errorText = e.toString();
errorText = errorText.substring(10, errorText.length); //Remove 'Exception' from string.
......@@ -148,6 +176,7 @@ class _ViewEvents extends State<ViewEvents> {
}else{
debugPrint('No PENDING event(s)');
}
_currentlySyncing = false;
}
@override
......@@ -301,13 +330,13 @@ class _ViewEvents extends State<ViewEvents> {
icon: null,
label: const Text('Sync'),
onPressed: () {
syncEvents();
//syncEvents();
//showDialog(
// barrierDismissible: false,
// context: context,
// builder: (BuildContext context) => _buildSyncDialog(context),
//);
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) => _buildSyncDialog(context),
);
},
......
......@@ -226,6 +226,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1"
getwidget:
dependency: "direct main"
description:
name: getwidget
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
http:
dependency: "direct main"
description:
......
......@@ -39,7 +39,7 @@ dependencies:
flutter_login: ^3.1.0
datetime_picker_formfield: ^2.0.0
flutter_datetime_picker: ^1.5.1
#getwidget: ^3.0.1
getwidget: ^3.0.1
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment