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> { ...@@ -19,12 +19,13 @@ class _DefaultPageState extends State<DefaultPage> {
late Timer _timer; late Timer _timer;
bool showBar = false; bool showBar = false;
bool serverConnection = true; bool serverConnection = true;
bool dialogShown = false;
late Future<List<Measurement>> markerListFuture; late Future<List<Measurement>> markerListFuture;
late Future<Uint8List> relationFuture; late Future<Uint8List> relationFuture;
/// Fetch measurement and relation data, check server connection, and final GlobalKey<ScaffoldMessengerState> _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
/// start a request timer
@override @override
void initState() { void initState() {
super.initState(); super.initState();
...@@ -36,44 +37,55 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -36,44 +37,55 @@ class _DefaultPageState extends State<DefaultPage> {
// Return the measurements // Return the measurements
return measurements; return measurements;
}).catchError((error) { }).catchError((error) {
serverConnection = false;
throw Exception("Failed to fetch measurements: $error"); throw Exception("Failed to fetch measurements: $error");
}); });
if (!serverConnection) {
_showConnectionMessage();
}
relationFuture = fetchRelation(); 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 const Duration interval = Duration(minutes: fetchInterval); // NB fetchInterval value to be determined
_timer = Timer.periodic(interval, (timer) { _timer = Timer.periodic(interval, (timer) {
fetchMeasurements(); 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 @override
void dispose() { void dispose() {
// Cancel timer on widget termination
_timer.cancel(); _timer.cancel();
super.dispose(); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
home: Scaffold( home: Scaffold(
key: _scaffoldMessengerKey,
appBar: AppBar( appBar: AppBar(
title: const Text('IceMap'), title: const Text('IceMap'),
actions: [ actions: [
...@@ -90,7 +102,6 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -90,7 +102,6 @@ class _DefaultPageState extends State<DefaultPage> {
body: FutureBuilder( body: FutureBuilder(
future: Future.wait([markerListFuture, relationFuture]), future: Future.wait([markerListFuture, relationFuture]),
builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) { builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
// Display loading screen until data is fetched
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return Container( return Container(
decoration: const BoxDecoration( // Background color for loading screen decoration: const BoxDecoration( // Background color for loading screen
...@@ -107,12 +118,17 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -107,12 +118,17 @@ class _DefaultPageState extends State<DefaultPage> {
} else if (snapshot.hasError) { } else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}')); return Center(child: Text('Error: ${snapshot.error}'));
} else { } else {
if (!serverConnection) { // NB: implement dialogue box // Alert the user after the build completes if there is no server connection
print("Failed to connect to server"); // Here... WidgetsBinding.instance.addPostFrameCallback((_) {
} if (serverConnection && !dialogShown) {
// Display default page once all data is loaded from server dialogShown = true;
showConnectionMessage();
}
});
List<Measurement> markerList = snapshot.data![0] as List<Measurement>; List<Measurement> markerList = snapshot.data![0] as List<Measurement>;
Uint8List relation = snapshot.data![1] as Uint8List; Uint8List relation = snapshot.data![1] as Uint8List;
return Container( // Return container with list view and background color return Container( // Return container with list view and background color
decoration: const BoxDecoration( // Set background color decoration: const BoxDecoration( // Set background color
gradient: LinearGradient( gradient: LinearGradient(
...@@ -134,4 +150,3 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -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