From 7946b4883e8dcc3796ee3bd142c6c0b74032416d Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Fri, 5 Apr 2024 13:54:09 +0200 Subject: [PATCH] update: loading from file if no internet connection --- app/lib/consts.dart | 2 +- app/lib/controller/network_controller.dart | 4 ++ app/lib/server_requests/fetch_markers.dart | 29 ++++++----- app/lib/server_requests/fetch_relation.dart | 29 ++++++----- app/lib/server_requests/init_state.dart | 58 +++++++++++++-------- 5 files changed, 74 insertions(+), 48 deletions(-) diff --git a/app/lib/consts.dart b/app/lib/consts.dart index d1c7751a..1e79c722 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 e0992f06..7da8a58b 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 cd67bc7b..80cbc172 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 88e1ca20..3f723fae 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 a5c90d44..b2e2182b 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"); -- GitLab