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

add: working alert

parent 04a9d20e
No related branches found
No related tags found
2 merge requests!9Clhp map,!8Clhp map
......@@ -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> {
);
}
}
No preview for this file type
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