From f7d9cc0fa0711e23431281be63d86da79214f20c Mon Sep 17 00:00:00 2001 From: Sandertp Date: Tue, 3 Sep 2024 13:33:50 +0200 Subject: [PATCH] Show reviews on frontend with colored pins Co-authored-by: Reimar --- Mobile/lib/main.dart | 39 ++++++++++++++++++- Mobile/lib/models.dart | 26 +++++++++++++ Mobile/pubspec.lock | 8 ++-- .../migrations/V3__create_reviews_table.sql | 2 +- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index 332536d..e71f742 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -60,6 +60,7 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { final GlobalKey _scaffoldKey = GlobalKey(); List _favorites = []; + List _reviews = []; LatLng? _selectedPoint; LatLng _currentPosition = LatLng(55.656707, 10.563214); LatLng? _userPosition; @@ -81,6 +82,7 @@ class _MyHomePageState extends State { if (!isLoggedIn || !mounted) return; _fetchFavorites(); + _fetchReviews(); }); } @@ -216,7 +218,16 @@ class _MyHomePageState extends State { }); } - + Future _fetchReviews() async { + final response = await api.request(context, api.ApiService.app, 'GET', '/reviews', null); + if (response == null) return; + + final List reviews = jsonDecode(response); + setState(() { + _reviews = reviews.map((review) => Review.fromJson(review)).toList(); + debugPrint(_reviews.length.toString()); + }); + } Future GetOpenStreetMapArea() async { final dynamic location; @@ -329,7 +340,31 @@ class _MyHomePageState extends State { ) ], )), - ..._favorites.map((favorite) => MarkerLayer( + ..._reviews.map((review) => MarkerLayer( + markers: [ + Marker( + point: LatLng(review.lat, review.lng), + width: 30, + height: 50, + alignment: Alignment.center, + child: Stack( + children: [ + IconButton( + padding: const EdgeInsets.only(bottom: 10), + icon: const Icon(Icons.location_pin, size: 30, color:Colors.purpleAccent), + onPressed: () => _showLocation(LatLng(review.lat, review.lng), review.place_name, review.place_description), + ), + IconButton( + padding: const EdgeInsets.only(bottom: 10), + icon: const Icon(Icons.location_on_outlined, size: 30, color: Colors.purple), + onPressed: () => _showLocation(LatLng(review.lat, review.lng), review.place_name, review.place_description), + ), + ], + ) + ) + ], + )), + ..._favorites.map((favorite) => MarkerLayer( markers: [ Marker( point: LatLng(favorite.lat, favorite.lng), diff --git a/Mobile/lib/models.dart b/Mobile/lib/models.dart index 8739fd1..92b5730 100644 --- a/Mobile/lib/models.dart +++ b/Mobile/lib/models.dart @@ -22,6 +22,32 @@ class Favorite { } } +class Review { + int id; + String userId; + double lat; + double lng; + String place_name; + String place_description; + String title; + int rating; + + Review(this.id, this.userId, this.lat, this.lng, this.place_name, this.place_description, this.title, this.rating); + + factory Review.fromJson(Map json) { + return Review( + json['id'], + json['user_id'], + json['lat'], + json['lng'], + json['place_name'], + json['place_description'], + json['title'], + json['rating'] + ); + } +} + class Login { String token; String id; diff --git a/Mobile/pubspec.lock b/Mobile/pubspec.lock index b5d1008..3061e4c 100644 --- a/Mobile/pubspec.lock +++ b/Mobile/pubspec.lock @@ -396,10 +396,10 @@ packages: dependency: transitive description: name: shared_preferences_android - sha256: a7e8467e9181cef109f601e3f65765685786c1a738a83d7fbbde377589c0d974 + sha256: "480ba4345773f56acda9abf5f50bd966f581dac5d514e5fc4a18c62976bbba7e" url: "https://pub.dev" source: hosted - version: "2.3.1" + version: "2.3.2" shared_preferences_foundation: dependency: transitive description: @@ -521,10 +521,10 @@ packages: dependency: transitive description: name: uuid - sha256: "83d37c7ad7aaf9aa8e275490669535c8080377cfa7a7004c24dfac53afffaa90" + sha256: f33d6bb662f0e4f79dcd7ada2e6170f3b3a2530c28fc41f49a411ddedd576a77 url: "https://pub.dev" source: hosted - version: "4.4.2" + version: "4.5.0" vector_math: dependency: transitive description: diff --git a/rust-backend/migrations/V3__create_reviews_table.sql b/rust-backend/migrations/V3__create_reviews_table.sql index 6ab31b4..6b76a9f 100644 --- a/rust-backend/migrations/V3__create_reviews_table.sql +++ b/rust-backend/migrations/V3__create_reviews_table.sql @@ -7,6 +7,6 @@ CREATE TABLE reviews ( place_description TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, - rating REAL NOT NULL + rating INTEGER NOT NULL );