diff --git a/app/lib/consts.dart b/app/lib/consts.dart index d1c7751a71c5564ccd46cbcae484bfc4e276be05..1e79c722356411969a82fc79714b4ebab21477d3 100644 --- a/app/lib/consts.dart +++ b/app/lib/consts.dart @@ -17,7 +17,7 @@ List<Measurement> selectedMarkerList = []; LatLng mapCenter = LatLng(60.8000, 10.8471); DateTime ?lastUpdate; // Last time data was fetched from server List<String> lakeSearchOptions = []; // Init empty -bool internetConnection = true; +bool internetConnection = false; // Font settings const textColor = Colors.white; diff --git a/app/lib/controller/network_controller.dart b/app/lib/controller/network_controller.dart index e0992f06d1f3c622eb4e7d681a11a9ad167a257e..7da8a58bb64191fbc9769ae4774dd38e309fb4b8 100644 --- a/app/lib/controller/network_controller.dart +++ b/app/lib/controller/network_controller.dart @@ -18,6 +18,8 @@ class NetworkController extends GetxController { // If no network connection, show snack-bar if (connectivityResult == ConnectivityResult.none) { + internetConnection = false; + Get.rawSnackbar( messageText: Text( 'No internet connection. The displayed information may be outdated!', @@ -35,6 +37,8 @@ class NetworkController extends GetxController { snackStyle: SnackStyle.GROUNDED, ); } else { + internetConnection = true; + if (Get.isSnackbarOpen) { // Close snack-bar upon establishing internet connection Get.closeCurrentSnackbar(); } diff --git a/app/lib/server_requests/fetch_markers.dart b/app/lib/server_requests/fetch_markers.dart index cd67bc7b348c5ac74c9150c0b6e391b6a7053327..80cbc1726d1dc265c8c97da753c1b44c1efeabd6 100644 --- a/app/lib/server_requests/fetch_markers.dart +++ b/app/lib/server_requests/fetch_markers.dart @@ -62,18 +62,23 @@ Future<FetchResult> fetchMeasurements() async { } Future<FetchResult> loadSavedData() async { - // Get latest saved data from file if the server does not respond - Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); - String filePath = '${appDocumentsDirectory.path}/last_data.json'; + try { + // Get latest saved data from file if the server does not respond + Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); + String filePath = '${appDocumentsDirectory.path}/last_data.json'; - // Read file contents - File file = File(filePath); - if (await file.exists()) { - String contents = await file.readAsString(); - List<dynamic> jsonData = json.decode(contents); // Parse JSON string from file - List<Measurement> measurements = jsonData.map((data) => Measurement.fromJson(data)).toList(); - return FetchResult(measurements, false); - } else { - throw Exception('File does not exist'); + // Read file contents + File file = File(filePath); + if (await file.exists()) { + String contents = await file.readAsString(); + List<dynamic> jsonData = json.decode(contents); // Parse JSON string from file + List<Measurement> measurements = jsonData.map((data) => Measurement.fromJson(data)).toList(); + return FetchResult(measurements, false); + } else { + throw Exception('File does not exist'); + } + } catch (error) { + print('Error in reading measurements from file: $error'); + return FetchResult([], false); } } diff --git a/app/lib/server_requests/fetch_relation.dart b/app/lib/server_requests/fetch_relation.dart index 88e1ca2003a6a872679a983e49f74c53ae5c1e48..3f723fae14e17708212a62fd116ece5c04b1dbc8 100644 --- a/app/lib/server_requests/fetch_relation.dart +++ b/app/lib/server_requests/fetch_relation.dart @@ -46,19 +46,24 @@ Future<Uint8List> fetchRelation() async { } Future<Uint8List> loadSavedRelation() async { - // Get latest saved relation from file if the server does not respond - Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); - String filePath = '${appDocumentsDirectory.path}/last_relation.json'; + try { + // Get latest saved relation from file if the server does not respond + Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); + String filePath = '${appDocumentsDirectory.path}/last_relation.json'; - // Read file contents - File file = File(filePath); - if (await file.exists()) { - String contents = await file.readAsString(); - List<dynamic> jsonData = json.decode(contents); // Parse JSON string from file - Uint8List relation = Uint8List.fromList(utf8.encode(jsonData.toString())); - return relation; - } else { - throw Exception('File does not exist'); + // Read file contents + File file = File(filePath); + if (await file.exists()) { + String contents = await file.readAsString(); + List<dynamic> jsonData = json.decode(contents); // Parse JSON string from file + Uint8List relation = Uint8List.fromList(utf8.encode(jsonData.toString())); + return relation; + } else { + throw Exception('File does not exist'); + } + } catch (error) { + print('Error in reading relation from file: $error'); + return Uint8List(0); } } diff --git a/app/lib/server_requests/init_state.dart b/app/lib/server_requests/init_state.dart index a5c90d44f2d7d2ea6632809909dff89d609d684b..b2e2182b213b3a15fa209bae0cc4827e86586ebf 100644 --- a/app/lib/server_requests/init_state.dart +++ b/app/lib/server_requests/init_state.dart @@ -4,6 +4,7 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:app/consts.dart'; +import '../consts.dart'; import '../data_classes.dart'; import '../server_requests/fetch_markers.dart'; import '../server_requests/fetch_relation.dart'; @@ -19,30 +20,41 @@ Future<void> initialiseState() async { late Future<Uint8List> relationFuture; try { - // Try to fetch measurement data from server - markerListFuture = fetchMeasurements().then((fetchResult) { + + // Read data from files if no internet connection + if (!internetConnection) { + selectedRelation = Uint8List(0); // NB update once fixed + + FetchResult fetchResult = await loadSavedData(); List<Measurement> measurements = fetchResult.measurements; - serverConnection = fetchResult.connected; - - // Return measurements - return measurements; - }).catchError((error) { - serverConnection = false; - throw Exception("Failed to fetch measurements: $error"); - }); - - // If measurements were fetched successfully, request relation - if (serverConnection) { - relationFuture = fetchRelation(); - } /*else { // Read last saved data - relationFuture = loadSavedRelation(); - }*/ - - initSearchOptions(); - - //selectedRelation = await relationFuture; - selectedRelation = Uint8List(0); - selectedMarkerList = await markerListFuture; + selectedMarkerList = measurements; + + lakeSearchOptions = ["Mjøsa"]; + } else { // Try to fetch measurement data from server + markerListFuture = fetchMeasurements().then((fetchResult) { + List<Measurement> measurements = fetchResult.measurements; + serverConnection = fetchResult.connected; + + // Return measurements + return measurements; + }).catchError((error) { + serverConnection = false; + throw Exception("Failed to fetch measurements: $error"); + }); + + // If measurements were fetched successfully, request relation + if (serverConnection) { + relationFuture = fetchRelation(); + } /*else { // Read last saved data + relationFuture = loadSavedRelation(); + }*/ + + initSearchOptions(); + + //selectedRelation = await relationFuture; + selectedRelation = Uint8List(0); // NB update once fixed + selectedMarkerList = await markerListFuture; + } } catch (e) { // Handle any errors that occur during the initialization process print("Error during initialization: $e");