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
Loading
......@@ -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;
......
......@@ -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();
}
......
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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");
......
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