From 158bdd91a9b3dbfe0aec437f39527bbd6141eb6c Mon Sep 17 00:00:00 2001 From: Sandertp Date: Thu, 22 Aug 2024 12:14:06 +0200 Subject: [PATCH] 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(() {