diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart index ca25bf5f83e97d0abcc09d1bc50f05267f8b1324..fe40b00938bb1c8619d68a257b47916e5b247d09 100644 --- a/app/lib/pages/default_page.dart +++ b/app/lib/pages/default_page.dart @@ -1,15 +1,12 @@ import 'dart:async'; -import 'package:app/server_requests/fetch_relation.dart'; -import 'package:app/server_requests/init_state.dart'; +import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:app/server_requests/init_state.dart'; import 'package:liquid_pull_to_refresh/liquid_pull_to_refresh.dart'; import '../consts.dart'; -import '../data_classes.dart'; import '../widgets/main_layout.dart'; import '../utils/custom_search_delegate.dart'; -import '../server_requests/fetch_relation.dart'; -import '../server_requests/fetch_markers.dart'; class DefaultPage extends StatefulWidget { const DefaultPage({Key? key}) : super(key: key); @@ -31,7 +28,7 @@ class _DefaultPageState extends State<DefaultPage> { } Future<void> _handleRefresh() async { - return await initialiseState(); + return await initialiseState(false); } @override @@ -64,10 +61,13 @@ class _DefaultPageState extends State<DefaultPage> { showSearch( // Fetch new relation and measurements on search context: context, delegate: CustomSearchDelegate((String result) { - setState(() { - selectedLake = result; - }); - initialiseState(); + // Make request only if the selected lake is different from the current selected lake + if (result != selectedLake) { + setState(() { + selectedLake = result; + }); + initialiseState(false); + } }), ); }, diff --git a/app/lib/pages/loading_page.dart b/app/lib/pages/loading_page.dart index c58553cd5fa1f81230b919916c22d11795ac97e2..f9d12dc19c17cbae44937f32080e1797aa1a201e 100644 --- a/app/lib/pages/loading_page.dart +++ b/app/lib/pages/loading_page.dart @@ -25,7 +25,7 @@ class _LoadingPageState extends State<LoadingPage> } Future<void> _navigateToDefaultPage() async { - await initialiseState(); + await initialiseState(true); // Navigate to the default page once state is initialised Navigator.of(context).pushReplacement(MaterialPageRoute( diff --git a/app/lib/server_requests/init_state.dart b/app/lib/server_requests/init_state.dart index 5e11cc66861e0344b136888c025a7e6d0ea9c416..77eccc9dc222921032c809ee1ef928bec2901a80 100644 --- a/app/lib/server_requests/init_state.dart +++ b/app/lib/server_requests/init_state.dart @@ -13,7 +13,7 @@ import '../server_requests/fetch_relation.dart'; /// initialiseState makes three requests to the server, one requesting /// measurements for the selected relation, the other requesting the relation, /// and the last requesting the list of all system lakes -Future<void> initialiseState() async { +Future<void> initialiseState(bool fetchSearchOptions) async { bool serverConnection = true; late Future<List<Measurement>> markerListFuture; @@ -46,7 +46,9 @@ Future<void> initialiseState() async { relationFuture = loadSavedRelation(); } - initSearchOptions(); + if (fetchSearchOptions) { + initSearchOptions(); + } //selectedRelation = await relationFuture; selectedRelation = await relationFuture; // NB update once fixed diff --git a/app/lib/widgets/main_layout.dart b/app/lib/widgets/main_layout.dart index 570a8c5318a8ee215a6c2b2e2f7af6aec572d9e1..25e6f0bf1e86a7986a4f628c601adbdd2f9c25b7 100644 --- a/app/lib/widgets/main_layout.dart +++ b/app/lib/widgets/main_layout.dart @@ -1,6 +1,4 @@ import 'dart:typed_data'; -import 'package:flutter_map/flutter_map.dart'; -import 'package:fuzzy/fuzzy.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:shared_preferences/shared_preferences.dart'; diff --git a/server/main.py b/server/main.py index d0570e78c7d8ea734a40ab42090e9d748b2a028c..566e64f3b264417040c9093b41fce3a3ea31c8d9 100644 --- a/server/main.py +++ b/server/main.py @@ -8,7 +8,7 @@ from http.server import HTTPServer, BaseHTTPRequestHandler from map_handler.add_lake import cut_map from server.consts import LAKE_RELATIONS_PATH -from map_handler.get_lake import get_divided_map +from map_handler.get_lake_relation import get_divided_map from map_handler.get_measurements import get_all_markers from map_handler.input_new_data import input_new_Lidar_data diff --git a/server/map_handler/__pycache__/add_lake.cpython-311.pyc b/server/map_handler/__pycache__/add_lake.cpython-311.pyc index 3337d8de7c10d6e98b86ea0835797f3599d8cd47..0d77e9519bd32398199d7cec00dd815e8bd81763 100644 Binary files a/server/map_handler/__pycache__/add_lake.cpython-311.pyc and b/server/map_handler/__pycache__/add_lake.cpython-311.pyc differ diff --git a/server/map_handler/__pycache__/get_lake.cpython-311.pyc b/server/map_handler/__pycache__/get_lake.cpython-311.pyc deleted file mode 100644 index 136f83f681989d911f21f4c715020cd44e8a2066..0000000000000000000000000000000000000000 Binary files a/server/map_handler/__pycache__/get_lake.cpython-311.pyc and /dev/null differ diff --git a/server/map_handler/__pycache__/get_lake_data.cpython-311.pyc b/server/map_handler/__pycache__/get_lake_data.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d1b4d24d13d09a2c4b32ab6de3f73969fb265183 Binary files /dev/null and b/server/map_handler/__pycache__/get_lake_data.cpython-311.pyc differ diff --git a/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc b/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..15407de3dee5ad887f2b1a901bf523fecd0dc368 Binary files /dev/null and b/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc differ diff --git a/server/map_handler/__pycache__/get_measurements.cpython-311.pyc b/server/map_handler/__pycache__/get_measurements.cpython-311.pyc index 04e435195bb84b483975508c3e1f2e1f7c2ac5ab..4428ea3d1cf7782690c3cd887061ae76458f248e 100644 Binary files a/server/map_handler/__pycache__/get_measurements.cpython-311.pyc and b/server/map_handler/__pycache__/get_measurements.cpython-311.pyc differ diff --git a/server/map_handler/get_lake.py b/server/map_handler/get_lake.py deleted file mode 100644 index 02c076a033f7bdf8bee9521b56aa20cde0f117ed..0000000000000000000000000000000000000000 --- a/server/map_handler/get_lake.py +++ /dev/null @@ -1,15 +0,0 @@ -from server.consts import LAKE_RELATIONS_PATH - - -# Writes contents of a lake json file to the response -def get_divided_map(self, file_name): - self.send_response(200) - self.send_header("Content-type", "application/json") - self.end_headers() - - # Extract contents from JSON file - with open(LAKE_RELATIONS_PATH + file_name + "_div.json", "r") as file: - data = file.read() - - # Write contents of the JSON file to response - self.wfile.write(data.encode('utf-8')) diff --git a/server/map_handler/get_lake_relation.py b/server/map_handler/get_lake_relation.py new file mode 100644 index 0000000000000000000000000000000000000000..d24d32c86ef4849975dd61f277fdbac9d421cfc7 --- /dev/null +++ b/server/map_handler/get_lake_relation.py @@ -0,0 +1,26 @@ +from server.consts import LAKE_RELATIONS_PATH + + +# Writes contents of a lake json file to the response +def get_divided_map(self, file_name): + + try: + # Extract contents from JSON file + with open(LAKE_RELATIONS_PATH + file_name + "_div.json", "r") as file: + data = file.read() + + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() + + # Write contents of the JSON file to response + self.wfile.write(data.encode('utf-8')) + except FileNotFoundError: + self.send_response(404) + self.send_header("Content-type", "application/json") + self.end_headers() + except Exception as e: + self.send_response(500) + self.send_header("Content-type", "application/json") + self.end_headers() + diff --git a/server/map_handler/get_measurements.py b/server/map_handler/get_measurements.py index c7a88f816017270a60e53f9a2240427f91615d54..c97becd2044123e9b60725c11bc4a0f17323d2c7 100644 --- a/server/map_handler/get_measurements.py +++ b/server/map_handler/get_measurements.py @@ -133,9 +133,8 @@ def get_all_markers(self, cursor, lake_name): # Convert dictionary values to list of measurements data = list(measurement_data.values()) + test_measurements - if len(rows) == 0 or len(data) == 0: # Return 500 and empty list if no data is found - print(f"No data which meets the condition found") - marker_data = '[]' + if len(data) == 0: + marker_data = json.dumps(['no measurements']) else: # Convert list of dictionaries to JSON marker_data = json.dumps(data, indent=4) @@ -144,6 +143,11 @@ def get_all_markers(self, cursor, lake_name): print(f"Error in querying database: {e}") marker_data = '[]' + # Set headers + self.send_response(500) + self.send_header("Content-type", "application/json") + self.end_headers() + # Set headers self.send_response(200) self.send_header("Content-type", "application/json") diff --git a/server/map_handler/lake_relations/all_lake_names.json b/server/map_handler/lake_relations/all_lake_names.json index fc2472160666eb0b344f18a28c987d99fd4ef756..939b10a49233538e2b3f640e1bfe95d354ad51a7 100644 --- a/server/map_handler/lake_relations/all_lake_names.json +++ b/server/map_handler/lake_relations/all_lake_names.json @@ -1,11 +1,4 @@ [ "Mj\u00c3\u00b8sa", - "Bogstadsvannet", - "Einavatnet", - "Femsj\u00c3\u00b8en", - "Femunden", - "Fjellsj\u00c3\u00b8en", - "Gjende", - "Gjersj\u00c3\u00b8en", "Skumsj\u00c3\u00b8en" ] \ No newline at end of file