diff --git a/app/lib/controller/network_controller.dart b/app/lib/controller/network_controller.dart new file mode 100644 index 0000000000000000000000000000000000000000..37afe15c164c086a642c03cb8587a7683c944d4f --- /dev/null +++ b/app/lib/controller/network_controller.dart @@ -0,0 +1,43 @@ +import 'package:get/get.dart'; +import 'package:flutter/material.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; + +import '../consts.dart'; + +/// NetworkController checks the network connection of the application globally +class NetworkController extends GetxController { + final Connectivity _connectivity = Connectivity(); + + @override + void onInit() { + super.onInit(); + _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); + } + + void _updateConnectionStatus(ConnectivityResult connectivityResult) { + + // If no network connection, show snack-bar + if (connectivityResult == ConnectivityResult.none) { + Get.rawSnackbar( + messageText: Text( + 'You are not connected to the internet. The displayed information may be inaccurate.', + style: regTextStyle, + ), + isDismissible: false, + duration: const Duration(days: 1), // Display the message until a network connection is established + backgroundColor: Colors.black45, + icon: const Icon( + Icons.wifi_off, + color: Colors.white, + size: 35, + ), + margin: EdgeInsets.zero, + snackStyle: SnackStyle.GROUNDED, + ); + } else { + if (Get.isSnackbarOpen) { // Close snack-bar upon establishing internet connection + Get.closeCurrentSnackbar(); + } + } + } +} \ No newline at end of file diff --git a/app/lib/server_requests/init_state.dart b/app/lib/server_requests/init_state.dart index d4ca66a1e597d6d6ec999f5581a55550ab8988ff..a5c90d44f2d7d2ea6632809909dff89d609d684b 100644 --- a/app/lib/server_requests/init_state.dart +++ b/app/lib/server_requests/init_state.dart @@ -9,37 +9,47 @@ import '../server_requests/fetch_markers.dart'; import '../server_requests/fetch_relation.dart'; -Future<void> initialiseState() async{ +/// initialiseState makes three requests to the server, one requesting +/// measurements for the selected relation, the other requesting the relation, +/// and the last requesting the list of all system lakes +Future<void> initialiseState() async { bool serverConnection = true; late Future<List<Measurement>> markerListFuture; late Future<Uint8List> relationFuture; - // Try to fetch measurement data from server - markerListFuture = fetchMeasurements().then((fetchResult) { - List<Measurement> measurements = fetchResult.measurements; - serverConnection = fetchResult.connected; + try { + // Try to fetch measurement data from server + markerListFuture = fetchMeasurements().then((fetchResult) { + List<Measurement> measurements = fetchResult.measurements; + serverConnection = fetchResult.connected; - // Return the measurements - return measurements; - }).catchError((error) { - serverConnection = false; - throw Exception("Failed to fetch measurements: $error"); - }); + // Return measurements + return measurements; + }).catchError((error) { + serverConnection = false; + throw Exception("Failed to fetch measurements: $error"); + }); - // Attempt to fetch relation from server if app establishes connection - if (serverConnection) { - relationFuture = fetchRelation(); - } else { // Read last saved data - relationFuture = loadSavedRelation(); - } + // If measurements were fetched successfully, request relation + if (serverConnection) { + relationFuture = fetchRelation(); + } /*else { // Read last saved data + relationFuture = loadSavedRelation(); + }*/ - initSearchOptions(); + initSearchOptions(); - selectedRelation = await relationFuture; - selectedMarkerList = await markerListFuture; + //selectedRelation = await relationFuture; + selectedRelation = Uint8List(0); + selectedMarkerList = await markerListFuture; + } catch (e) { + // Handle any errors that occur during the initialization process + print("Error during initialization: $e"); + } } + /// initSearchOptions fetches a list of all lake names in the system /// and initialises lakeSearchOptions Future<void> initSearchOptions() async { diff --git a/app/pubspec.lock b/app/pubspec.lock index 73538620a0a9ffeefd9ba57ea9b53da96d8dabc0..f052b8a782a53e5dfd02c4305034688622205349 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -1,6 +1,14 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + args: + dependency: transitive + description: + name: args + sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596 + url: "https://pub.dev" + source: hosted + version: "2.4.2" async: dependency: transitive description: @@ -41,6 +49,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.18.0" + connectivity_plus: + dependency: "direct main" + description: + name: connectivity_plus + sha256: b74247fad72c171381dbe700ca17da24deac637ab6d43c343b42867acb95c991 + url: "https://pub.dev" + source: hosted + version: "3.0.6" + connectivity_plus_platform_interface: + dependency: transitive + description: + name: connectivity_plus_platform_interface + sha256: cf1d1c28f4416f8c654d7dc3cd638ec586076255d407cef3ddbdaf178272a71a + url: "https://pub.dev" + source: hosted + version: "1.2.4" crypto: dependency: transitive description: @@ -49,6 +73,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.3" + dbus: + dependency: transitive + description: + name: dbus + sha256: "365c771ac3b0e58845f39ec6deebc76e3276aa9922b0cc60840712094d9047ac" + url: "https://pub.dev" + source: hosted + version: "0.7.10" equatable: dependency: transitive description: @@ -128,6 +160,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.1" + get: + dependency: "direct main" + description: + name: get + sha256: e4e7335ede17452b391ed3b2ede016545706c01a02292a6c97619705e7d2a85e + url: "https://pub.dev" + source: hosted + version: "4.6.6" google_fonts: dependency: "direct main" description: @@ -160,6 +200,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.19.0" + js: + dependency: transitive + description: + name: js + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + url: "https://pub.dev" + source: hosted + version: "0.6.7" latlong2: dependency: "direct main" description: @@ -248,6 +296,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.0" + nm: + dependency: transitive + description: + name: nm + sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254" + url: "https://pub.dev" + source: hosted + version: "0.5.0" path: dependency: transitive description: @@ -328,6 +384,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.11.1" + petitparser: + dependency: transitive + description: + name: petitparser + sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27 + url: "https://pub.dev" + source: hosted + version: "6.0.2" platform: dependency: transitive description: @@ -573,6 +637,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.4" + xml: + dependency: transitive + description: + name: xml + sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 + url: "https://pub.dev" + source: hosted + version: "6.5.0" sdks: dart: ">=3.3.0 <4.0.0" flutter: ">=3.19.0" diff --git a/app/pubspec.yaml b/app/pubspec.yaml index c87dfe4687ef61b2a1849409c71d948d5a22c588..84fd4516d63b13f277e6f5a31ed0f71f9752840a 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -9,9 +9,9 @@ environment: dependencies: flutter: sdk: flutter - flutter_map: ^4.0.0 # Various maps + flutter_map: ^4.0.0 # Maps ans map customization http: ^0.13.3 # HTTPS requests - latlong2: ^0.8.2 + latlong2: ^0.8.2 # LatLng object provider: ^5.0.0 fl_chart: ^0.20.0-nullsafety1 # Charts and diagrams google_fonts: any # Fonts @@ -20,6 +20,8 @@ dependencies: path_provider: ^2.0.8 shared_preferences: any # Persistent data storage fuzzy: any # Search algorithm + connectivity_plus: ^3.0.3 # Check internet connection + get: ^4.6.5 dev_dependencies: flutter_test: