Newer
Older
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

Maximilian Betz
committed
import 'package:package_info_plus/package_info_plus.dart';
class Collection {
int id;
String description;
String collectionName;
Collection({
required this.id,
required this.description,
required this.collectionName,
});
factory Collection.fromJson(Map<String, dynamic> json) {
return Collection(
id: json['id'],
description: json['description'],
);
}
Map<String, dynamic> toJson() => {
'id': id,
'description': description,
'collectionName' : collectionName
};
@override
String toString(){
}
}
class Device{
int id;
String urn;
Device(this.id, this.urn);
factory Device.fromJson(Map<String, dynamic> json){
return Device( json['id'] as int,
json['urn'] as String);
Map<String, dynamic> toJson() => {
'id': id,
'urn': urn
};
int id; //This shall be the mysql primary key database id
int urnId;
String type; // Event type name TODO: this should be an EventType variable
int typeId;
String startDate;
String endDate;
String latitude;
String longitude;
String elevation;
required this.urnId,
required this.urn,
required this.label,
required this.type,
required this.typeId,
required this.description,
required this.status,
required this.startDate,
required this.endDate,
required this.latitude,
required this.longitude,
required this.elevation,
});
factory Event.fromEvent(Event event){
return Event(
id: event.id,
urnId: event.urnId,
urn: event.urn,
label: event.label,
type: event.type,
typeId: event.typeId,
description: event.description,
status: event.status,
startDate: event.startDate,
endDate: event.endDate,
latitude: event.latitude,
longitude: event.longitude,
elevation: event.elevation,
);
}
factory Event.fromJson(Map<String, dynamic> json){
return Event(
id: json['id'],
urnId: json['urnId'],
urn: json['urn'],
label: json['label'],
type: json['type'],
typeId: json['typeId'],
description: json['description'],
status: json['status'],
startDate: json['startDate'],
endDate: json['endDate'],
latitude: json['latitude'],
longitude: json['longitude'],
elevation: json['elevation'],
Map<String, dynamic> toMap() {
return {
'id' : id,
'urnId': urnId,
'urn': urn,
'label': label,
'type' : type,
'typeId' : typeId,
'description' : description,
'status' : status,
'startDate' : startDate,
'endDate' : endDate,
'latitude' : latitude,
'longitude' : longitude,
'elevation' : elevation
};
}
Map<String, dynamic> toMapNoId() {
return {
'urnId': urnId,
'urn': urn,
'label': label,
'type' : type,
'typeId' : typeId,
'description' : description,
'status' : status,
'startDate' : startDate,
'endDate' : endDate,
'latitude' : latitude,
'longitude' : longitude,
'elevation' : elevation
};
}
'id' : id,
'urnId': urnId,
'urn': urn,
'label': label,
'type' : type,
'typeId' : typeId,
'description' : description,
'status' : status,
'startDate' : startDate,
'endDate' : endDate,
'latitude' : latitude,
'longitude' : longitude,
'elevation' : elevation
Map<String, dynamic> toSensorJson() {
return {
"\"itemID\"":"\"$urnId\"",
"\"inheritToAllChildren\"":"\"false\"",
"\"inheritToChildren\"":[],
"\"event\"":{
"\"startDate\"":"\"$startDate\"",
"\"endDate\"":"\"$endDate\"",
"\"label\"":"\"$label\"",
"\"description\"":"\"$description\"",
"\"eventType\"":"\"$typeId\"",
"\"latitude\"":"\"$latitude\"",
"\"longitude\"":"\"$longitude\"",
"\"elevationInMeter\"":"\"$elevation\"",
"\"id\"":"\"0\""
}
@override
String toString(){
return '{ '
'${this.id}, '
'${this.urnId}, '
'${this.urn}, '
'${this.label}, '
'${this.type}, '
'${this.description}, '
'${this.startDate}, '
'${this.endDate}, '
'${this.latitude},'
'${this.longitude},'
'${this.elevation},'
'${this.status} }';
EventType({
required this.id,
required this.name
});
EventType.fromSensorJson(Map<String, dynamic> json)
: id = json['id'],
EventType.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'];
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
};
@override
String toString(){
return name;
}
Maximilian Betz
committed
class LabelConfiguration{
int cnt;
Maximilian Betz
committed
String prefix;
LabelConfiguration(
this.cnt,
Maximilian Betz
committed
this.prefix
);
Map<String, dynamic> toJson() => {
"cntUp" : cntUp,
"cnt": cnt,
"prefix": prefix
Maximilian Betz
committed
};
LabelConfiguration.fromJson(Map<String, dynamic> json)
: cntUp = json['cntUp'],
cnt = json['cnt'],
prefix = json['prefix'];
Maximilian Betz
committed
}
class SensorLogin{
String mail;
String password;
SensorLogin(
this.mail,
this.password,
);
Map<String, dynamic> toJson() => {
SensorLogin.fromJson(Map<String, dynamic> json)
: mail = json['mail'],
password = json['password'];
// Storage classes
// ConfigurationStoreBase for Collection, devices, event types, login information
// EventStoreBase for created event information.
abstract class ConfigurationStoreBase {
List<Collection> collections = [];
Collection currentCollection = Collection(id: -1, description: '', collectionName: '');
List<Device> devices = [];
List<EventType> eventTypes = [];
LabelConfiguration labelConfig = LabelConfiguration(0, true, '');
Maximilian Betz
committed
String restRequestUrl = '';
bool initialized = false;
Map<String, dynamic> toMap() {
return {
'collections' : jsonEncode(collections),
'currentCollection': currentCollection.toJson(),
'devices': jsonEncode(devices),
'eventTypes': jsonEncode(eventTypes),
'initialized' : initialized,
Maximilian Betz
committed
'labelConfiguration' : jsonEncode(labelConfig),
'restRequestUrl' : restRequestUrl,
Map<String, dynamic> loginDataToMap() {
return {
'loginInformation' : loginInformation.toJson(),
};
}
void loginDataFromMap(Map<String, dynamic> map){
loginInformation = SensorLogin.fromJson(map['loginInformation']);
}
void fromMap(Map<String, dynamic> map) {
List<dynamic> dynList = jsonDecode(map['collections']);
collections = [];
for (var element in dynList){
collections.add(Collection.fromJson(element));
}
currentCollection = Collection.fromJson(map['currentCollection']);
dynList = jsonDecode(map['devices']);
devices = [];
for (var element in dynList){
devices.add(Device.fromJson(element));
}
dynList = jsonDecode(map['eventTypes']);
eventTypes = [];
for (var element in dynList){
eventTypes.add(EventType.fromJson(element));
}
Maximilian Betz
committed
labelConfig = LabelConfiguration.fromJson(json.decode(map['labelConfiguration']));
Maximilian Betz
committed
restRequestUrl = map['restRequestUrl'];
initialized = map['initialized'];
}
Collection getCollectionFromName(String name) {
for (var collection in collections) {
if (collection.collectionName == name) {
return collection;
}
}
throw Exception('Event with name :$name was not found.');
if (device.urn == urn) {
return device.id;
}
}
int getEventIdFromName(String name) {
if (eventType.name == name) {
return eventType.id;
}
}
throw Exception('Event with name :$name was not found.');
collections = [];
devices = [];
eventTypes = [];
currentCollection = Collection(id: -1, description: '', collectionName: '');
collections.add(currentCollection); //Add a dummy collection. Dropdown menu does not like empty lists
loginInformation = SensorLogin('', '');
initialized = false;
class ConfigurationStoreInstance extends ConfigurationStoreBase {
static final ConfigurationStoreInstance _instance = ConfigurationStoreInstance
._internal();
@override
void reset(){
super.reset();
deleteSharedPrefs();
}
const secureStorage = FlutterSecureStorage();
var writeData = await secureStorage.write(key: 'login', value: jsonEncode(loginDataToMap()));
final prefs = await SharedPreferences.getInstance();
prefs.setString('configuration', jsonEncode(toMap()));
const secureStorage = FlutterSecureStorage();
final String? readData = await secureStorage.read(key: 'login');
if (readData != null) {
loginDataFromMap(jsonDecode(readData));
debugPrint('Login loaded from secure storage');
}else{
debugPrint('Failed to load login details from secure storage');
}
final prefs = await SharedPreferences.getInstance();
final String? configuration = prefs.getString('configuration');
if (configuration != null){
debugPrint('Configuration String: $configuration');
debugPrint('Configuration loaded from shared preferences');
debugPrint('Failed to load configuration from shared preferences.');
Future<void> deleteSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
prefs.clear();
await prefs.remove('configuration');
const secureStorage = FlutterSecureStorage();
secureStorage.delete(key: 'login');
}
factory ConfigurationStoreInstance() {
ConfigurationStoreInstance._internal() {
collections = [];
collections.add(currentCollection);

Maximilian Betz
committed
bool showAllEvents = false;
id: 0,
urnId:-1,
urn:'urn0',
label:'',
type:'',
typeId:-1,
description:'',
status:'PENDING',
startDate:'',
endDate: '',
latitude: '',
longitude: '',
elevation: ''
);
Map<String, dynamic> toMap() {
return {

Maximilian Betz
committed
'gnssSync' : gnssSync,
'showAllEvents' : showAllEvents
void fromMap(Map<String, dynamic> map) {
currentEvent = Event.fromJson(map['currentEvent']);

Maximilian Betz
committed
showAllEvents = map['showAllEvents'];
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.');
}
}
Future<void> deleteSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('currentEvent');
}
return _instance;
}
id:0,
urnId:-1,
urn:'urn0',
label:'',
type:'',
typeId:-1,
description:'',
status:'PENDING',
startDate:'',
endDate: '',
latitude: '',
longitude: '',
elevation: ''
);;
gnssSync = true;
showAllEvents = false;
id:0,
urnId:-1,
urn:'urn0',
label:'',
type:'',
typeId:-1,
description:'',
status:'PENDING',
startDate:'',
endDate: '',
latitude: '',
longitude: '',
elevation: ''
);

Maximilian Betz
committed
String versionNumber = '';
String buildNumber = '';
Future<void> loadVersionInfo() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
versionNumber = packageInfo.version;
buildNumber = packageInfo.buildNumber;
}

Maximilian Betz
committed
String getVersion(){
return versionNumber;

Maximilian Betz
committed
String getBuildNumber(){
return buildNumber;
}