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

cleanup

parent 30973c57
No related branches found
No related tags found
No related merge requests found
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
......@@ -6,6 +8,7 @@ import 'datamodel.dart';
import 'databaseconnector.dart';
import 'dart:async';
import 'package:geolocator/geolocator.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AddEvent extends StatefulWidget {
const AddEvent({Key? key}) : super(key: key);
......@@ -16,6 +19,7 @@ class AddEvent extends StatefulWidget {
class _AddEventPageState extends State<AddEvent> {
bool syncGNSSData = true;
bool _addButtonEnabled = true;
late String long = "";
late String lat = "";
late String alt = "";
......@@ -90,14 +94,18 @@ class _AddEventPageState extends State<AddEvent> {
}
@override
void dispose() {
void dispose() async {
try {
streamHandler.cancel();
debugPrint('Cancel location stream');
}catch(e){
debugPrint('Canceling location stream failed');
}
/*Async update current event configuration to shared preferences*/
final EventStoreInstance event = EventStoreInstance();
event.storeToSharedPrefs();
super.dispose();
}
......@@ -133,23 +141,21 @@ class _AddEventPageState extends State<AddEvent> {
}
var number = num.tryParse(value);
if(number != null){
return true; // Any numerical value is valid for elevation
return true; // Any numerical value is valid for elevation
}
return false;
}
bool _validateInput(){
final EventStoreInstance eventStore = EventStoreInstance();
final EventStoreInstance event = EventStoreInstance();
if (RegExp(r'^[a-z A-Z . \- 0-9 , ( ) + - _ :]+$').hasMatch(
eventStore.currentEvent.label)) {
event.currentEvent.label)) {
if (RegExp(r'^[a-z A-Z . \- 0-9 , ( ) + - _ :]+$').hasMatch(
eventStore.currentEvent.description) || eventStore.currentEvent.description == '') {
if(_validateLatitude(eventStore.currentEvent.latitude)){
if(_validateLongitude(eventStore.currentEvent.longitude)){
if(_validateElevation(eventStore.currentEvent.elevation)){
debugPrint('All inputs valid');
event.currentEvent.description) || event.currentEvent.description == '') {
if(_validateLatitude(event.currentEvent.latitude)){
if(_validateLongitude(event.currentEvent.longitude)){
if(_validateElevation(event.currentEvent.elevation)){
return true;
}
}
}
......@@ -158,15 +164,32 @@ class _AddEventPageState extends State<AddEvent> {
return false;
}
void _storeCurrentEvent() {
final EventStoreInstance eventStore = EventStoreInstance();
bool _addButtonStatus(){
if((_validateInput() == true) && (_addButtonEnabled == true)){
return true;
}
return false;
}
Future<void> _storeCurrentEvent() async {
final EventStoreInstance event = EventStoreInstance();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
eventStore.currentEvent.typeId = configuration.getEventIdFromName(eventStore.currentEvent.type);
eventStore.currentEvent.status = "PENDING";
database.addEvent(eventStore.currentEvent); //TODO: display feedback after this async function
event.currentEvent.typeId = configuration.getEventIdFromName(event.currentEvent.type);
event.currentEvent.status = "PENDING";
await database.addEvent(event.currentEvent); //TODO: display feedback after this async function
HapticFeedback.vibrate(); //Feedback that adding event succeeded
_addButtonEnabled = true; //Activate button for add more events
setState(() {});
//Update timestamp in UI
var date = DateTime.now().toUtc();
var isoDate = date.toIso8601String();
event.currentEvent.startDate = isoDate;
event.currentEvent.endDate = isoDate;
}
@override
Widget build(BuildContext context) {
/* Get singletons to access relevant data here.*/
......@@ -456,20 +479,13 @@ class _AddEventPageState extends State<AddEvent> {
const SizedBox(width: 50),
Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: _validateInput() ?
child: _addButtonStatus() ?
FloatingActionButton(
onPressed: () {
if (_validateInput()) {
_addButtonEnabled = false; //Disable button until event is stored
_storeCurrentEvent();
HapticFeedback.vibrate();
//Update time for next event
var date = DateTime.now().toUtc();
var isoDate = date.toIso8601String();
eventStore.currentEvent.startDate = isoDate;
eventStore.currentEvent.endDate = isoDate;
debugPrint(date.toIso8601String());
}
setState(() {});
},
......@@ -484,7 +500,7 @@ class _AddEventPageState extends State<AddEvent> {
//refresh the UI
});
},
tooltip: 'Correct Inputs before adding the Event',
tooltip: 'Complete / correct inputs before adding the Event',
//child: const Icon(Icons.check),
)
),
......@@ -500,7 +516,7 @@ class _AddEventPageState extends State<AddEvent> {
crossAxisAlignment: CrossAxisAlignment.end,
children: const <Widget>[
Text(
' Visit Configuration first.',
' Visit Configuration first',
style: TextStyle(fontSize: 20)
),
],
......
......@@ -100,9 +100,8 @@ class _MyHomePageState extends State<Configuration> {
throw Exception('Something went wrong try again');
}
final prefs = await SharedPreferences.getInstance();
prefs.setString('configuration', jsonEncode(configuration.toMap()));
prefs.setString('currentEvent', jsonEncode(event.toMap()));
await event.storeToSharedPrefs();
await configuration.storeToSharedPrefs();
debugPrint('Configuration stored!');
});
});
......@@ -135,13 +134,15 @@ class _MyHomePageState extends State<Configuration> {
appBar: AppBar(
title: const Text('Configuration'),
),
body: Container(
body: SingleChildScrollView(
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 5.0),
child:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(height: 50),
const Text(
'You must be online to do something here!',
style: TextStyle(fontSize: 14)
......@@ -222,6 +223,7 @@ class _MyHomePageState extends State<Configuration> {
),
),
),
),
bottomNavigationBar: Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: Row(
......@@ -236,18 +238,18 @@ class _MyHomePageState extends State<Configuration> {
onPressed: () {
dumpToFile();
//events.reset(); //TODO: remove before release this is not a real use case!
//var db = DatabaseInstance();
//db.delete();
//TODO: remove before release this is not a real use case!
var db = DatabaseInstance();
db.delete();
//configuration.reset();
},
),
const SizedBox(width: 50),
FloatingActionButton.extended(
heroTag: null,
tooltip: 'Select Collection and download corresponding devices',
tooltip: 'Download corresponding devices and store locally',
icon: const Icon(Icons.save),
label: const Text('Store'),
label: const Text('Update'),
onPressed: () {
updateConfiguration();
//TODO: display failed as user feedback somehow
......@@ -261,5 +263,4 @@ class _MyHomePageState extends State<Configuration> {
}
}
//TODO: add scrowlable view here for landscape compatibility.
//TODO: write configuration on app dispose!
\ No newline at end of file
......@@ -2,6 +2,7 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Collection {
int id;
......@@ -335,6 +336,24 @@ class ConfigurationStoreInstance extends ConfigurationStoreBase {
static final ConfigurationStoreInstance _instance = ConfigurationStoreInstance
._internal();
Future<void> storeToSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('configuration', jsonEncode(toMap()));
debugPrint('Stored current configuration to shared prefs');
}
Future<void> loadFromSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
final String? configuration = prefs.getString('configuration');
if (configuration != null){
debugPrint('Configuration Current Event String: ' + configuration);
fromMap(jsonDecode(configuration));
debugPrint('Configuration Current Event loaded from shared preferences');
}else{
debugPrint('Failed to load Current Event configuration from shared preferences.');
}
}
factory ConfigurationStoreInstance() {
return _instance;
}
......@@ -382,6 +401,24 @@ abstract class EventStoreBase{
class EventStoreInstance extends EventStoreBase {
static final EventStoreInstance _instance = EventStoreInstance._internal();
Future<void> storeToSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('currentEvent', jsonEncode(toMap()));
debugPrint('Stored current event to shared prefs');
}
Future<void> loadFromSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
final String? currentEvent = prefs.getString('currentEvent');
if (currentEvent != null){
debugPrint('Configuration Current Event String: ' + currentEvent);
fromMap(jsonDecode(currentEvent));
debugPrint('Configuration Current Event loaded from shared preferences');
}else{
debugPrint('Failed to load Current Event configuration from shared preferences.');
}
}
factory EventStoreInstance() {
return _instance;
}
......
......@@ -14,24 +14,10 @@ Future<void> loadConfiguration() async {
final EventStoreInstance event = EventStoreInstance();
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
final String? storedConfiguration = prefs.getString('configuration');
if (storedConfiguration != null){
debugPrint('Configuration String: ' + storedConfiguration);
configuration.fromMap(jsonDecode(storedConfiguration));
debugPrint('Configuration loaded from shared preferences');
}else{
debugPrint('Failed to load configuration from shared preferences.');
}
final String? currentEvent = prefs.getString('currentEvent');
if (currentEvent != null){
debugPrint('Configuration Current Event String: ' + currentEvent);
event.fromMap(jsonDecode(currentEvent));
debugPrint('Configuration Current Event loaded from shared preferences');
}else{
debugPrint('Failed to load Current Event configuration from shared preferences.');
}
await configuration.loadFromSharedPrefs();
await event.loadFromSharedPrefs();
//const storage = FlutterSecureStorage(); //TODO: move login information to secure storage
//Map<String, String> allValues = await storage.readAll();
......
......@@ -268,4 +268,4 @@ class _ViewEvents extends State<ViewEvents> {
);
}
}
//TODO: allow editing fields here. Check validation of input value!
//TODO: allow editing fields here.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment