diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart index 269cae4fd1b840dfdb6448a825b569791f37e475..d37d1d6ace3569e506dc7b5aa2657b0d4d17e72a 100644 --- a/app/lib/pages/default_page.dart +++ b/app/lib/pages/default_page.dart @@ -64,10 +64,10 @@ class _DefaultPageState extends State<DefaultPage> { delegate: CustomSearchDelegate((String result) { // Make request only if the selected lake is different from the current selected lake if (result != selectedLake) { - initialiseState(false); setState(() { selectedLake = result; // NB update lastLake persistent variable + initialiseState(false); }); } }), diff --git a/app/lib/utils/export_data.dart b/app/lib/utils/export_data.dart new file mode 100644 index 0000000000000000000000000000000000000000..deaca423e06c647a8667d5282b5573207119c281 --- /dev/null +++ b/app/lib/utils/export_data.dart @@ -0,0 +1,69 @@ +import 'dart:io'; +import 'dart:convert'; +import 'package:flutter/material.dart'; +import 'package:path_provider/path_provider.dart'; + +import '../../consts.dart'; + + +// Saves all measurements to a file on the users mobile device +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 + Future.delayed(const Duration(seconds: 1), () { + try { // Download JSON data + _exportIceData(); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Downloaded ice data as JSON')), + ); + } catch (e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Failed to export 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(); + Navigator.of(context).pop(); + }); + } + } + }); +} + diff --git a/app/lib/widgets/main_layout.dart b/app/lib/widgets/main_layout.dart index 6f4ce65701cf350ee1895b95b1bbd3bcf2fb3ab2..46cad0e90e99164daf80b6c6e22a19faa1974c7a 100644 --- a/app/lib/widgets/main_layout.dart +++ b/app/lib/widgets/main_layout.dart @@ -1,9 +1,6 @@ -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'; @@ -13,6 +10,7 @@ import 'choropleth_map.dart'; import '../data_classes.dart'; import 'satellite_layer.dart'; import 'quick_view_chart.dart'; +import '../utils/export_data.dart'; import '../utils/format_month.dart'; /// MapContainerWidget is the main widget that contains the map with all @@ -291,66 +289,4 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { }, ); } -} - -// Saves all measurements to a file on the users mobile device -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 - Future.delayed(const Duration(seconds: 1), () { - try { // Download JSON data - _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(); - Navigator.of(context).pop(); - }); - } - } - }); -} - +} \ No newline at end of file