From cae02bfdedec501eef44263183c335dd8e13b2cd Mon Sep 17 00:00:00 2001 From: Sandertp Date: Thu, 22 Aug 2024 10:20:59 +0200 Subject: [PATCH 1/4] Retrieve Favorites from Backend, Add Favorite Model Co-authored-by: Reimar --- Mobile/lib/api.dart | 4 +++- Mobile/lib/main.dart | 26 ++++++++++++++++++++++++++ Mobile/lib/models.dart | 8 ++++++++ Mobile/lib/register.dart | 4 ++-- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 Mobile/lib/models.dart diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 5f8ad2a..6fc3872 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; @@ -69,7 +71,7 @@ Future isLoggedIn(BuildContext context) async { try { String base64 = token.split('.')[1]; - base64 += List.filled(4 - base64.length % 4, '=').join(); + base64 += List.filled(base64.length % 4 == 0 ? 0 : 4 - base64.length % 4, '=').join(); final payload = jsonDecode(String.fromCharCodes(base64Decode(base64))); diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index 65f5548..1304f96 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -1,3 +1,6 @@ +import 'dart:convert'; +import 'dart:developer'; + import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; @@ -6,6 +9,8 @@ import 'package:mobile/register.dart'; import 'login.dart'; import 'base/sidemenu.dart'; import 'profile.dart'; +import 'api.dart' as api; +import 'models.dart'; void main() { runApp(const MyApp()); @@ -43,6 +48,27 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { final GlobalKey _scaffoldKey = GlobalKey(); + List _favorites = []; + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + + api.isLoggedIn(context).then((isLoggedIn) async { + if (!isLoggedIn || !mounted) return; + + final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null); + if (response == null) return; + log(response); + + final List favorites = jsonDecode(response); + setState(() { + _favorites = favorites.map((favorite) => Favorite(favorite['id'], favorite['user_id'], favorite['lat'], favorite['lng'])).toList(); + }); + }); + + } + @override Widget build(BuildContext context) { return SideMenu( diff --git a/Mobile/lib/models.dart b/Mobile/lib/models.dart new file mode 100644 index 0000000..41a6f8a --- /dev/null +++ b/Mobile/lib/models.dart @@ -0,0 +1,8 @@ +class Favorite { + int id; + String userId; + double lat; + double lng; + + Favorite(this.id, this.userId, this.lat, this.lng); +} \ No newline at end of file diff --git a/Mobile/lib/register.dart b/Mobile/lib/register.dart index f72e788..e6ab956 100644 --- a/Mobile/lib/register.dart +++ b/Mobile/lib/register.dart @@ -17,10 +17,10 @@ class _RegisterPageState extends State { Future _register() async { if (passwordInput.text != confirmPasswordInput.text) { - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Passwords do not match'))); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Passwords do not match'))); return; } - final result = await api.request(context, api.ApiService.auth, 'POST', '/api/Users', { 'username': usernameInput.text, 'email': emailInput.text, From 67f3cd118f8fc51adc1e1667860f914979c8e65c Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 22 Aug 2024 11:46:45 +0200 Subject: [PATCH 2/4] Show favorites on map --- Mobile/lib/main.dart | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index 1304f96..e256729 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -76,23 +76,24 @@ class _MyHomePageState extends State { key: _scaffoldKey, //drawer: navigationMenu, body: FlutterMap( - options: const MapOptions( - initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0), + options: const MapOptions(initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0), children: [ openStreetMapTileLayer, - const MarkerLayer(markers: [ - Marker( - point: LatLng(56.465511, 9.411366), - width: 60, - height: 100, - alignment: Alignment.center, - child: Icon( - Icons.location_pin, - size: 60, - color: Colors.purple, - ), - ), - ]), + ..._favorites.map((favorite) => + MarkerLayer(markers: [ + Marker( + point: LatLng(favorite.lat, favorite.lng), + width: 60, + height: 100, + alignment: Alignment.center, + child: const Icon( + Icons.location_pin, + size: 60, + color: Colors.yellow, + ) + ) + ]) + ), ], ), ), From 158bdd91a9b3dbfe0aec437f39527bbd6141eb6c Mon Sep 17 00:00:00 2001 From: Sandertp Date: Thu, 22 Aug 2024 12:14:06 +0200 Subject: [PATCH 3/4] Add favorite menu functionality Co-authored-by: Reimar --- Mobile/lib/api.dart | 1 + Mobile/lib/base/sidemenu.dart | 4 ++-- Mobile/lib/favorites.dart | 45 +++++++++++++++++++++++++++++++++++ Mobile/lib/favourites.dart | 15 ------------ Mobile/lib/main.dart | 5 ++-- 5 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 Mobile/lib/favorites.dart delete mode 100644 Mobile/lib/favourites.dart diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 6fc3872..6dac053 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -83,6 +83,7 @@ Future isLoggedIn(BuildContext context) async { } catch (e) { messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again'))); prefs.remove('token'); + debugPrint(e.toString()); return false; } diff --git a/Mobile/lib/base/sidemenu.dart b/Mobile/lib/base/sidemenu.dart index 7f74d20..d09c05b 100644 --- a/Mobile/lib/base/sidemenu.dart +++ b/Mobile/lib/base/sidemenu.dart @@ -83,14 +83,14 @@ class _SideMenuState extends State { }, ), ListTile( - title: const Text('Favourites'), + title: const Text('Favorites'), leading: const Icon(Icons.star), selected: _selectedIndex == 1, onTap: () { // Update the state of the app _onItemTapped(1); // Then close the drawer - Navigator.pushReplacementNamed(context, '/favourites'); + Navigator.pushReplacementNamed(context, '/favorites'); }, ), ListTile( diff --git a/Mobile/lib/favorites.dart b/Mobile/lib/favorites.dart new file mode 100644 index 0000000..9d1e128 --- /dev/null +++ b/Mobile/lib/favorites.dart @@ -0,0 +1,45 @@ +import 'dart:convert'; +import 'api.dart' as api; +import 'package:flutter/material.dart'; +import 'base/sidemenu.dart'; // Import the base layout widget +import 'models.dart'; + +class FavoritesPage extends StatefulWidget { + const FavoritesPage({super.key}); + + @override + State createState() => _FavoritesPage(); +} + +class _FavoritesPage extends State { + List _favorites = []; + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + + api.isLoggedIn(context).then((isLoggedIn) async { + if (!isLoggedIn || !mounted) return; + + final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null); + if (response == null) return; + + final List favorites = jsonDecode(response); + setState(() { + _favorites = favorites.map((favorite) => Favorite(favorite['id'], favorite['user_id'], favorite['lat'], favorite['lng'])).toList(); + }); + }); + + } + @override + Widget build(BuildContext context) { + return SideMenu( + body: Container( + padding: const EdgeInsets.all(20.0), + child: Column(children: + _favorites.map((favorite) => Text("${favorite.lat} ${favorite.lng}")).toList(), + ), + ), + ); + } +} diff --git a/Mobile/lib/favourites.dart b/Mobile/lib/favourites.dart deleted file mode 100644 index fd01f2a..0000000 --- a/Mobile/lib/favourites.dart +++ /dev/null @@ -1,15 +0,0 @@ -import 'package:flutter/material.dart'; -import 'base/sidemenu.dart'; // Import the base layout widget - -class FavouritesPage extends StatelessWidget { - const FavouritesPage({super.key}); - - @override - Widget build(BuildContext context) { - return const SideMenu( - body: Center( - child: Text('This is Page 1'), - ), - ); - } -} diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index e256729..56b069b 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -4,7 +4,7 @@ import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; -import 'package:mobile/favourites.dart'; +import 'package:mobile/favorites.dart'; import 'package:mobile/register.dart'; import 'login.dart'; import 'base/sidemenu.dart'; @@ -31,7 +31,7 @@ class MyApp extends StatelessWidget { routes: { '/home': (context) => const MyHomePage(), '/profile': (context) => const ProfilePage(), - '/favourites': (context) => const FavouritesPage(), + '/favorites': (context) => const FavoritesPage(), '/login': (context) => const LoginPage(), '/register': (context) => const RegisterPage(), }, @@ -59,7 +59,6 @@ class _MyHomePageState extends State { final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null); if (response == null) return; - log(response); final List favorites = jsonDecode(response); setState(() { From 13e319157dba4e025647240c0bdb876d1b753e19 Mon Sep 17 00:00:00 2001 From: Sandertp Date: Thu, 22 Aug 2024 12:43:27 +0200 Subject: [PATCH 4/4] Add favorite page design Co-authored-by: Reimar --- Mobile/lib/favorites.dart | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Mobile/lib/favorites.dart b/Mobile/lib/favorites.dart index 9d1e128..8afc327 100644 --- a/Mobile/lib/favorites.dart +++ b/Mobile/lib/favorites.dart @@ -35,9 +35,25 @@ class _FavoritesPage extends State { Widget build(BuildContext context) { return SideMenu( body: Container( + decoration: BoxDecoration(color: Color(0xFFF9F9F9)), + width: MediaQuery.of(context).size.width, padding: const EdgeInsets.all(20.0), child: Column(children: - _favorites.map((favorite) => Text("${favorite.lat} ${favorite.lng}")).toList(), + _favorites.map((favorite) => Container( + width: double.infinity, + padding: const EdgeInsets.all(20.0), + decoration: const BoxDecoration( + boxShadow: [ + BoxShadow( + color: Color(0x20000000), + offset: Offset(0, 1), + blurRadius: 4, + ), + ], + color: Colors.white + ), + child: const Text("Favorite data here"), + )).toList(), ), ), );