diff --git a/lib/methods/place_db_helper.dart b/lib/methods/place_db_helper.dart index c14d5dd..2dfff93 100644 --- a/lib/methods/place_db_helper.dart +++ b/lib/methods/place_db_helper.dart @@ -36,23 +36,23 @@ class PlaceDBHelper implements IDb{ await placeDB.execute( 'CREATE TABLE place (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum DATE, Adresse1 TEXT, Adresse2 TEXT, Adresse3 TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, OrtInfo TEXT, Status TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, KontDat DATE, Betreuung TEXT, AbbauDat DATE, Auftrag TEXT, KontAbsp TEXT, SonstBem TEXT, FKontakt1 TEXT, FKontakt2 TEXT, FKontakt3 TEXT, KTage1 INTEGER, KTage2 INTEGER, ProtoAm DATE, IntKomm TEXT, DECLNG DECIMALS(4,8), DECLAT DECIMALS(4,8), Sent INTEGER DEFAULT 0)'); await placeDB.execute( - 'CREATE TABLE placeTemplates (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum DATE, Adresse1 TEXT, Adresse2 TEXT, Adresse3 TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, OrtInfo TEXT, Status TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, KontDat DATE, Betreuung TEXT, AbbauDat DATE, Auftrag TEXT, KontAbsp TEXT, SonstBem TEXT, FKontakt1 TEXT, FKontakt2 TEXT, FKontakt3 TEXT, KTage1 INTEGER, KTage2 INTEGER, ProtoAm DATE, IntKomm TEXT, DECLNG DECIMALS(4,8), DECLAT DECIMALS(4,8))'); + 'CREATE TABLE placeTemplates (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum DATE, Adresse1 TEXT, Adresse2 TEXT, Adresse3 TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, OrtInfo TEXT, Status TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, KontDat DATE, Betreuung TEXT, AbbauDat DATE, Auftrag TEXT, KontAbsp TEXT, SonstBem TEXT, FKontakt1 TEXT, FKontakt2 TEXT, FKontakt3 TEXT, KTage1 INTEGER, KTage2 INTEGER, ProtoAm DATE, IntKomm TEXT, DECLNG DECIMALS(4,8), DECLAT DECIMALS(4,8))'); } // Function to add a finished entry and return its ID @override Future addMainEntry(Map place) async { var placeDBClient = await dB; - final existingID = await placeDBClient.query( - 'place', - where: 'ID = ?', - whereArgs: [place['ID']], - ); - - if (existingID.isNotEmpty) { - updateMainEntry(place); - return existingID.first['ID'] as int; // Return existing ID - } + // final existingID = await placeDBClient.query( + // 'place', + // where: 'ID = ?', + // whereArgs: [place['ID']], + // ); + // + // if (existingID.isNotEmpty) { + // updateMainEntry(place); + // return existingID.first['ID'] as int; // Return existing ID + // } int id = await placeDBClient.insert( 'place', diff --git a/lib/screens/addCam/add_cam_main.dart b/lib/screens/addCam/add_cam_main.dart index 955166a..a746d00 100644 --- a/lib/screens/addCam/add_cam_main.dart +++ b/lib/screens/addCam/add_cam_main.dart @@ -23,7 +23,6 @@ import 'widgets/status.dart'; class AddCamMain extends StatefulWidget { final bool isTemplate; - final bool isFinished; final bool isSent; final Map? existingData; @@ -31,7 +30,6 @@ class AddCamMain extends StatefulWidget { super.key, this.isTemplate = false, this.existingData, - this.isFinished = false, this.isSent = false, }); @@ -148,6 +146,8 @@ class _AddCamMainState extends State { // select initial werte rmap["MEZ"]!["controller"]!.text = selectedMEZ; rmap["Status"]!["controller"]!.text = selectedStatus; + rmap["DECLAT"]!["controller"]!.text = currentPosition.latitude.toString(); + rmap["DECLNG"]!["controller"]!.text = currentPosition.longitude.toString(); // If a template is edited this fills in the existing values if (widget.existingData?.isNotEmpty ?? false) { @@ -286,8 +286,8 @@ class _AddCamMainState extends State { children: [ Column( children: [ - Text(currentPosition.latitude.toString()), - Text(currentPosition.longitude.toString()), + Text(rmap["DECLAT"]!["controller"]!.text), + Text(rmap["DECLNG"]!["controller"]!.text), ], ), const SizedBox(width: 15), diff --git a/lib/screens/sharedMethods/delete_templates.dart b/lib/screens/sharedMethods/delete_templates.dart index 4e7c68e..ec1be48 100644 --- a/lib/screens/sharedMethods/delete_templates.dart +++ b/lib/screens/sharedMethods/delete_templates.dart @@ -4,6 +4,19 @@ import 'package:fforte/methods/excursion_db_helper.dart'; import 'package:fforte/methods/place_db_helper.dart'; class DeleteTemplates { + + static void deleteSingle(DatabasesEnum dbType, String id) { + IDb? db; + + if (dbType == DatabasesEnum.place) { + db = PlaceDBHelper(); + } else if (dbType == DatabasesEnum.excursion) { + db = ExcursionDBHelper(); + } + + db!.deleteTemplateById(id); + } + static void deleteAll(DatabasesEnum dbType) { IDb? db; diff --git a/lib/screens/sharedMethods/save_main_entry.dart b/lib/screens/sharedMethods/save_main_entry.dart index 88a60dc..c8040b2 100644 --- a/lib/screens/sharedMethods/save_main_entry.dart +++ b/lib/screens/sharedMethods/save_main_entry.dart @@ -19,10 +19,10 @@ class SaveMainEntryMethod { placeDB = ExcursionDBHelper(); } - int entryId; + if (isTemplate) placeDB!.deleteTemplateById(entryData["ID"]!); - // Get the ID of the newly added or updated place - if (entryData["ID"] == "") { + int entryId; + if (isTemplate || entryData["ID"] == "") { entryData.remove("ID"); entryId = await placeDB!.addMainEntry(entryData); } else { diff --git a/lib/screens/sharedMethods/save_template.dart b/lib/screens/sharedMethods/save_template.dart index 05d514e..4af9fb9 100644 --- a/lib/screens/sharedMethods/save_template.dart +++ b/lib/screens/sharedMethods/save_template.dart @@ -14,6 +14,9 @@ void saveTemplate(Map templateData, DatabasesEnum dbType, return; } + templateData.remove("Sent"); + templateData.remove("ID"); + if (update) { await dbHelper.updateTemplate(templateData); } else { diff --git a/lib/screens/viewCam/view_cams.dart b/lib/screens/viewCam/view_cams.dart index 28098b9..09f58f3 100644 --- a/lib/screens/viewCam/view_cams.dart +++ b/lib/screens/viewCam/view_cams.dart @@ -5,6 +5,7 @@ import 'package:fforte/screens/addCam/add_cam_main.dart'; import 'package:fforte/methods/place_db_helper.dart'; import 'package:fforte/screens/helper/view_entries_dialog_helper.dart'; import 'package:fforte/screens/sharedMethods/delete_main_entries.dart'; +import 'package:fforte/screens/sharedMethods/delete_templates.dart'; import 'package:flutter/material.dart'; import 'package:fforte/l10n/app_localizations.dart'; import 'package:flutter_map/flutter_map.dart'; @@ -25,11 +26,11 @@ class _ViewCamsState extends State { late Future>> mainEntries; late Future>> templates; late List markers; + IDb? db; // loads the entries @override void initState() { - IDb? db; super.initState(); if (widget.dbType == DatabasesEnum.place) { db = PlaceDBHelper(); @@ -37,8 +38,12 @@ class _ViewCamsState extends State { db = ExcursionDBHelper(); } + reloadAllEntries(); + } + + Future reloadAllEntries() async { mainEntries = db!.getAllMainEntries(); - templates = db.getAllTemplates(); + templates = db!.getAllTemplates(); } // The widet tree with taps to differentiate between templates and finished entries @@ -89,24 +94,24 @@ class _ViewCamsState extends State { children: [ Row( children: [ - const SizedBox( - width: 10, - ), + const SizedBox(width: 10), Text( - style: const TextStyle( - decoration: TextDecoration.underline), - AppLocalizations.of(context)!.placedata), + style: const TextStyle( + decoration: TextDecoration.underline, + ), + AppLocalizations.of(context)!.placedata, + ), ], ), Row( children: [ Text( - style: const TextStyle( - decoration: TextDecoration.underline), - AppLocalizations.of(context)!.sent), - const SizedBox( - width: 10, + style: const TextStyle( + decoration: TextDecoration.underline, + ), + AppLocalizations.of(context)!.sent, ), + const SizedBox(width: 10), ], ), ], @@ -128,6 +133,9 @@ class _ViewCamsState extends State { widget.dbType, place['ID'.toString()], ); + setState(() { + reloadAllEntries(); + }); }, backgroundColor: Colors.red, foregroundColor: Colors.white, @@ -158,7 +166,6 @@ class _ViewCamsState extends State { place['Sent'] == 1 ? true : false, - isFinished: true, existingData: place, ), ), @@ -203,23 +210,47 @@ class _ViewCamsState extends State { itemBuilder: (context, index) { Map templates = snapshot.data![index]; - return ListTile( - onTap: () { - Navigator.push( - context, - MaterialPageRoute( - builder: - (context) => AddCamMain( - isTemplate: true, - isFinished: false, - existingData: templates, - ), + return Slidable( + startActionPane: ActionPane( + motion: const ScrollMotion(), + children: [ + SlidableAction( + onPressed: (context) { + DeleteTemplates.deleteSingle( + widget.dbType, + templates['ID'].toString(), + ); + setState(() { + reloadAllEntries(); + }); + }, + backgroundColor: Colors.red, + foregroundColor: Colors.white, + icon: Icons.delete, + label: + AppLocalizations.of( + context, + )!.justdelete, ), - ); - }, - title: Text('Place ${index + 1}'), - subtitle: Text( - 'ID: ${templates['CID']} DATUM: ${templates['Datum']} RUDEL: ${templates['Rudel']} STATUS: ${templates['Status']}', + ], + ), + child: ListTile( + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: + (context) => AddCamMain( + isTemplate: true, + existingData: templates, + ), + ), + ); + }, + title: Text('Place ${index + 1}'), + subtitle: Text( + 'ID: ${templates['CID']} DATUM: ${templates['Datum']} RUDEL: ${templates['Rudel']} STATUS: ${templates['Status']}', + ), ), ); }, @@ -241,29 +272,31 @@ class _ViewCamsState extends State { } else if (snapshot.hasError) { return Text("Error ${snapshot.error}"); } else { - markers = - snapshot.data!.map((e) { - return Marker( - width: 80.0, - height: 80.0, - point: LatLng( - double.parse(e['DECLAT'].toString()), - double.parse(e['DECLNG'].toString()), - ), - child: Column( - children: [ - const Icon( - Icons.location_on, - color: Colors.red, - ), - Text( - "ID: ${e['ID'].toString()}", - style: const TextStyle(color: Colors.black), - ), - ], - ), - ); - }).toList(); + if (snapshot.data != null) { + markers = + snapshot.data!.map((e) { + return Marker( + width: 80.0, + height: 80.0, + point: LatLng( + double.parse(e['DECLAT'].toString()), + double.parse(e['DECLNG'].toString()), + ), + child: Column( + children: [ + const Icon( + Icons.location_on, + color: Colors.red, + ), + Text( + "ID: ${e['ID'].toString()}", + style: const TextStyle(color: Colors.black), + ), + ], + ), + ); + }).toList(); + } return FlutterMap( options: MapOptions( initialCenter: