From 54b7d1c848a2e2be63c85b2904c4d4fc8ef5f646 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Tue, 19 Mar 2024 11:13:23 +0100
Subject: [PATCH] update: save last relation, buggy

---
 .../pages/marker_handler/get_relation.dart    |  36 ++++++++++---
 .../pages/marker_handler/load_saved_data.dart |  51 ------------------
 .../__pycache__/get_relation.cpython-311.pyc  | Bin 5761 -> 5761 bytes
 3 files changed, 30 insertions(+), 57 deletions(-)
 delete mode 100644 app/lib/pages/marker_handler/load_saved_data.dart

diff --git a/app/lib/pages/marker_handler/get_relation.dart b/app/lib/pages/marker_handler/get_relation.dart
index 77d3297d..2a4abf5d 100644
--- a/app/lib/pages/marker_handler/get_relation.dart
+++ b/app/lib/pages/marker_handler/get_relation.dart
@@ -1,8 +1,10 @@
 import 'dart:async';
 import 'dart:convert';
 import 'dart:io';
-import '../consts.dart';
 import 'dart:typed_data';
+import 'package:path_provider/path_provider.dart';
+
+import '../consts.dart';
 
 /// Fetch relation data from server
 Future<Uint8List> fetchRelation() async {
@@ -21,16 +23,38 @@ Future<Uint8List> fetchRelation() async {
       var responseBody = await response.transform(utf8.decoder).join();
 
       if (responseBody.isNotEmpty) {
+        Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
+        String filePath = '${appDocumentsDirectory.path}/last_relation.json';
+
+        try { // Write most recent time of update to file
+          await File(filePath).writeAsString(responseBody, mode: FileMode.write);
+          print('Relation written to file');
+        } catch (error) { print('Error in writing to file: $error');}
+
         // Return relation data from the response body
         return Uint8List.fromList(utf8.encode(responseBody));
-      } else {
-        throw Exception('Response body is empty');
       }
-    } else {
-      throw Exception('Failed to fetch relation data: Status code ${response.statusCode}');
     }
+    return loadSavedRelation();
   } catch (e) {
-    throw Exception('Failed to fetch relation data: ${e.toString()}');
+    return loadSavedRelation();
+  }
+}
+
+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';
+
+  // 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');
   }
 }
 
diff --git a/app/lib/pages/marker_handler/load_saved_data.dart b/app/lib/pages/marker_handler/load_saved_data.dart
deleted file mode 100644
index b9ea25e3..00000000
--- a/app/lib/pages/marker_handler/load_saved_data.dart
+++ /dev/null
@@ -1,51 +0,0 @@
-import 'dart:async';
-import 'dart:convert';
-import 'dart:io';
-import 'marker_data.dart';
-import '../consts.dart';
-import 'package:path_provider/path_provider.dart';
-import 'dart:typed_data';
-
-/// loadSavedData parses json data from a saved file, either to a list
-/// of Measurement objects or a Uint8List object depending if it reads
-/// measurement data or relation data.
-Future<FetchResult> loadSavedData() async {
-  Uint8List relation;
-  List<Measurement> measurements = [];
-
-  // Get latest saved data from file if the server does not respond
-  Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
-  String filePath = '${appDocumentsDirectory.path}/last_data.sjon';
-
-  // Read measurement 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
-    measurements = jsonData.map((data) => Measurement.fromJson(data)).toList();
-  } else {
-    throw Exception('File does not exist');
-  }
-
-  filePath = '${appDocumentsDirectory.path}/relation.json';
-
-  // Read relation file contents
-  file = File(filePath);
-  if (await file.exists()) {
-    String contents = await file.readAsString();
-    List<dynamic> jsonData = json.decode(contents); // Parse JSON string from file
-    relation = Uint8List.fromList(utf8.encode(jsonData.toString()));
-  } else {
-    throw Exception('File does not exist');
-  }
-
-  return FetchResult(relation, measurements, false);
-}
-
-class FetchResult {
-  final Uint8List relation;
-  final List<Measurement> measurements;
-  final bool connected;
-
-  FetchResult(this.relation, this.measurements, this.connected);
-}
diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc
index 5fa72e24fbe220c417ab4c9ccf18c4749e3298ba..1fa84cb826efde8bac6448f030d6da17d236c79f 100644
GIT binary patch
delta 23
dcmZqFZPevk&dbZi00d{cf295u+{jlf1^`Yw2S5M-

delta 23
dcmZqFZPevk&dbZi00is1f21-BZR9H!0{}-h1}Fdk

-- 
GitLab