diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart index 965d8e45ebd9ae807d709c1a777ea6f9d097af47..a8d21b9214b07e48d8e0abc98df465d668eed6de 100644 --- a/app/lib/pages/default_page.dart +++ b/app/lib/pages/default_page.dart @@ -19,12 +19,13 @@ class _DefaultPageState extends State<DefaultPage> { late Timer _timer; bool showBar = false; bool serverConnection = true; + bool dialogShown = false; late Future<List<Measurement>> markerListFuture; late Future<Uint8List> relationFuture; - /// Fetch measurement and relation data, check server connection, and - /// start a request timer + final GlobalKey<ScaffoldMessengerState> _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>(); + @override void initState() { super.initState(); @@ -36,44 +37,55 @@ class _DefaultPageState extends State<DefaultPage> { // Return the measurements return measurements; }).catchError((error) { + serverConnection = false; throw Exception("Failed to fetch measurements: $error"); }); - if (!serverConnection) { - _showConnectionMessage(); - } - relationFuture = fetchRelation(); - // Schedule fetchMarkerData to run periodically based on fetchInterval from consts + // Schedule fetchMarkerData to run periodically based on fetchInterval const Duration interval = Duration(minutes: fetchInterval); // NB fetchInterval value to be determined _timer = Timer.periodic(interval, (timer) { fetchMeasurements(); }); } - /// Display a message for 5 seconds informing the user - /// of lacking server connection - void _showConnectionMessage() { - const snackBar = SnackBar( - content: Text('Failed to connect to the server. Information about' - 'ice safety could be outdated!'), - duration: Duration(seconds: 5), - ); - ScaffoldMessenger.of(context).showSnackBar(snackBar); - } - /// Timer for resending requests to server @override void dispose() { - // Cancel timer on widget termination _timer.cancel(); super.dispose(); } + /// Display message to user + void showConnectionMessage() { + showDialog( + context: context, + builder: (context) => AlertDialog( + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text("Ok"), + ) + ], + title: const Center( + child: Text("No server connection") + ), + contentPadding: const EdgeInsets.all(10.0), + content: const Text( + "The app may display outdated information. Use with caution!", + textAlign: TextAlign.center, // Align text center + ), + ), + ); + } + @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( + key: _scaffoldMessengerKey, appBar: AppBar( title: const Text('IceMap'), actions: [ @@ -90,7 +102,6 @@ class _DefaultPageState extends State<DefaultPage> { body: FutureBuilder( future: Future.wait([markerListFuture, relationFuture]), builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) { - // Display loading screen until data is fetched if (snapshot.connectionState == ConnectionState.waiting) { return Container( decoration: const BoxDecoration( // Background color for loading screen @@ -107,12 +118,17 @@ class _DefaultPageState extends State<DefaultPage> { } else if (snapshot.hasError) { return Center(child: Text('Error: ${snapshot.error}')); } else { - if (!serverConnection) { // NB: implement dialogue box - print("Failed to connect to server"); // Here... - } - // Display default page once all data is loaded from server + // Alert the user after the build completes if there is no server connection + WidgetsBinding.instance.addPostFrameCallback((_) { + if (serverConnection && !dialogShown) { + dialogShown = true; + showConnectionMessage(); + } + }); + List<Measurement> markerList = snapshot.data![0] as List<Measurement>; Uint8List relation = snapshot.data![1] as Uint8List; + return Container( // Return container with list view and background color decoration: const BoxDecoration( // Set background color gradient: LinearGradient( @@ -134,4 +150,3 @@ class _DefaultPageState extends State<DefaultPage> { ); } } - diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index 1fa84cb826efde8bac6448f030d6da17d236c79f..bcf1ba7349387c193f086a9c6aa7a5d2c9450b23 100644 Binary files a/server/map/__pycache__/get_relation.cpython-311.pyc and b/server/map/__pycache__/get_relation.cpython-311.pyc differ