Merge branch 'upload_screen_improvement' into 'main'

close add page when pressing the check button and upload to firebase in background

See merge request Gruber.Nicolas/oggauer_gemeindetrommler!7
This commit is contained in:
2023-05-04 20:18:38 +00:00
+24 -19
View File
@@ -54,7 +54,7 @@ class _AddScreenState extends State<AddScreen> {
} }
} }
Future<void> addNewElementToFirestore(String title, String description, File imageFile) async { Future<void> addNewElementToFirestore(String title, String description, File imageFile, {required Function(bool) onComplete}) async {
String collectionName; String collectionName;
if (_selectedIndex == 0) { if (_selectedIndex == 0) {
collectionName = "CitizensForum"; collectionName = "CitizensForum";
@@ -65,13 +65,21 @@ class _AddScreenState extends State<AddScreen> {
} }
String imageUrl = await uploadImageToFirebaseStorage(imageFile, collectionName); String imageUrl = await uploadImageToFirebaseStorage(imageFile, collectionName);
try
{
await FirebaseFirestore.instance.collection(collectionName).add({
"title": title,
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
});
onComplete(true);
}
catch(e)
{
onComplete(false);
}
await FirebaseFirestore.instance.collection(collectionName).add({
"title": title,
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
});
} }
Future<String> uploadImageToFirebaseStorage(File imageFile, String collectionName) async { Future<String> uploadImageToFirebaseStorage(File imageFile, String collectionName) async {
@@ -115,24 +123,21 @@ class _AddScreenState extends State<AddScreen> {
child: IconButton( child: IconButton(
icon: const Icon(Icons.check), icon: const Icon(Icons.check),
onPressed: () async { onPressed: () async {
if (_formKey.currentState!.validate() && _imageFile != null && if (_formKey.currentState!.validate() && _imageFile != null && !_uploading) {
!_uploading) {
setState(() { setState(() {
_uploading = true; _uploading = true;
}); });
try { Navigator.pop(context); // Fenster direkt schließen, nachdem der Upload-Button gedrückt wurde.
await addNewElementToFirestore( await addNewElementToFirestore(_titleController.text, _descriptionController.text, _imageFile!, onComplete: (bool success) {
_titleController.text, _descriptionController.text,
_imageFile!);
Navigator.pop(context);
} catch (e) {
print(e);
// Fehlerbehandlung hier
} finally {
setState(() { setState(() {
_uploading = false; _uploading = false;
}); });
} ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success ? 'Upload erfolgreich.' : 'Upload fehlgeschlagen.'),
),
);
});
} }
}, },
), ),