Skip to content
Snippets Groups Projects
Commit a71f637d authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

add: working JSON exportation, i think

parent d4ee25c0
No related branches found
No related tags found
1 merge request!14Clhp map
......@@ -93,4 +93,4 @@ Future<void> initSearchOptions() async {
Future<void> setLastLake() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('lastLake', selectedLake);
}
\ No newline at end of file
}
import 'dart:io';
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'osm_layer.dart';
......@@ -188,10 +191,12 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
),
child: const Text("Export JSON"),
onPressed: () {
Navigator.pop(context);
Navigator.of(context).pop();
// Show progress indicator
showProgressIndicator(context);
},
)
// Add more children here as needed
],
),
),
......@@ -289,3 +294,64 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
}
}
Future<void> _exportIceData() async {
final directory = await getExternalStorageDirectory();
final file = File('${directory?.path}/ice_data_$selectedLake.json');
// Convert JSON data to string
final jsonString = jsonEncode(selectedMarkerList);
// Write JSON data to file
await file.writeAsString(jsonString);
}
// Display a progress indicator while JSON data is being downloaded
void showProgressIndicator(BuildContext context) {
BuildContext? dialogContext;
showDialog(
context: context,
builder: (BuildContext context) {
dialogContext = context;
return WillPopScope(
onWillPop: () async => false, // Prevent dialog from being closed by user
child: const AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(), // Progress indicator
SizedBox(height: 20),
Text('Exporting JSON data...'),
],
),
),
);
},
);
// Ensure that the progress indicator runs for at lest 1 second
// before exporting the data
Future.delayed(const Duration(milliseconds: 1000), () {
try {
_exportIceData();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Downloaded ice data as JSON')),
);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error exporting JSON data: $e')),
);
} finally {
if (dialogContext != null) {
// Add 2 second delay before closing the dialog
Future.delayed(const Duration(seconds: 2), () {
Navigator.of(dialogContext!).pop();
});
}
}
});
}
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