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

update: loading from file if no internet connection

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