From 6a9b112d389700e30a7d4ce3ea11c63160590ce5 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Thu, 4 Apr 2024 13:31:14 +0200
Subject: [PATCH] add: search bar result selection

---
 app/lib/consts.dart                |  1 +
 app/lib/pages/default_page.dart    | 34 +++++++++++++++++++++---------
 server/map_handler/process_lake.py |  4 ++--
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/app/lib/consts.dart b/app/lib/consts.dart
index 041ea98b..4767d5fb 100644
--- a/app/lib/consts.dart
+++ b/app/lib/consts.dart
@@ -9,6 +9,7 @@ const String mapEndpoint = "update_map";
 const int fetchInterval = 60; // Fetch marker data every n minutes
 
 // Map variables
+String selectedLake = 'Mjøsa'; // Initialise to Mjøsa, NB should be initialised to last selected lake
 LatLng mapCenter = LatLng(60.8000, 10.8471);
 DateTime ?lastUpdate; // Last time data was fetched from server
 
diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart
index 8e9d3737..5d64a50b 100644
--- a/app/lib/pages/default_page.dart
+++ b/app/lib/pages/default_page.dart
@@ -101,7 +101,7 @@ class _DefaultPageState extends State<DefaultPage> {
             },
           ),
           title: Text(
-            'Mjøsa',
+            selectedLake, // Display name of current map
             style: regTextStyleBig,
           ),
           actions: [
@@ -112,11 +112,13 @@ class _DefaultPageState extends State<DefaultPage> {
               ),
               onPressed: () {
                 showSearch(
-                    context: context,
-                    delegate: _CustomSearchDelegate());
-                /*setState(() {
-                  showBar = !showBar;
-                });*/
+                  context: context,
+                  delegate: _CustomSearchDelegate((String result) {
+                    setState(() {
+                      selectedLake = result;
+                    });
+                  }),
+                );
               },
             ),
           ],
@@ -146,7 +148,13 @@ class _DefaultPageState extends State<DefaultPage> {
   }
 }
 
+typedef SearchResultCallback = void Function(String result);
+
 class _CustomSearchDelegate extends SearchDelegate {
+  final SearchResultCallback onResultSelected;
+
+  _CustomSearchDelegate(this.onResultSelected);
+
   List<String> searchItems = [ // NB temp values
     "Mjøsa",
     "Bogstadsvannet",
@@ -183,7 +191,7 @@ class _CustomSearchDelegate extends SearchDelegate {
   @override
   Widget buildResults(BuildContext context) {
     List<String> searchResults = [];
-    final options = FuzzyOptions(threshold: 0.3, findAllMatches: true);
+    final options = FuzzyOptions(threshold: 0.4, findAllMatches: true);
     final matcher = Fuzzy(searchItems, options: options);
     final results = matcher.search(query);
     searchResults = results.map((result) => result.item as String).toList();
@@ -192,8 +200,14 @@ class _CustomSearchDelegate extends SearchDelegate {
       itemCount: searchResults.length,
       itemBuilder: (context, index) {
         var result = searchResults[index];
-        return ListTile(
-          title: Text(result),
+        return GestureDetector(
+          onTap: () {
+            onResultSelected(result);
+            close(context, result);
+          },
+          child: ListTile(
+            title: Text(result),
+          ),
         );
       },
     );
@@ -202,7 +216,7 @@ class _CustomSearchDelegate extends SearchDelegate {
   @override
   Widget buildSuggestions(BuildContext context) {
     List<String> searchResults = [];
-    final options = FuzzyOptions(threshold: 0.3, findAllMatches: true);
+    final options = FuzzyOptions(threshold: 0.4, findAllMatches: true);
     final matcher = Fuzzy(searchItems, options: options);
     final results = matcher.search(query);
     searchResults = results.map((result) => result.item as String).toList();
diff --git a/server/map_handler/process_lake.py b/server/map_handler/process_lake.py
index d0189385..dd0d202b 100644
--- a/server/map_handler/process_lake.py
+++ b/server/map_handler/process_lake.py
@@ -1,7 +1,7 @@
 import geopandas as gpd
-from shapely.geometry import Polygon, LineString, MultiLineString
+from shapely.geometry import Polygon, MultiPolygon
 import json
-from server.map.add_lake import write_json_to_file
+from server.map_handler.add_lake import write_json_to_file
 
 
 # Writes contents of a map_handler json file to the response
-- 
GitLab