diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 827a55d..9b9250d 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -13,7 +13,7 @@ enum ApiService { Future request(BuildContext context, ApiService service, String method, String path, Object? body) async { - final messenger = ScaffoldMessenger.of(context); + final messenger = ScaffoldMessenger.of(context); final host = switch (service) { ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'), diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index 5b59fe1..c13fffc 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -29,17 +29,14 @@ class _LoginPageState extends State { // Assuming token is a JSON string Map json = jsonDecode(token); - User jsonUser = User.fromJson(json); + Login jsonUser = Login.fromJson(json); final prefs = await SharedPreferences.getInstance(); prefs.setString('token', jsonUser.token); prefs.setString('id', jsonUser.id); - - + setState(() - {user = jsonUser; - loggedIn == true; - }); + {loggedIn == true;}); if (mounted) { diff --git a/Mobile/lib/models.dart b/Mobile/lib/models.dart index f435a3b..9e74f90 100644 --- a/Mobile/lib/models.dart +++ b/Mobile/lib/models.dart @@ -7,18 +7,30 @@ class Favorite { Favorite(this.id, this.userId, this.lat, this.lng); } -class User { +class Login { String token; String id; + + Login(this.token, this.id); + + factory Login.fromJson(Map json) { + return Login( + json['token'], + json['id'], + ); + } +} + +class User { + String id; String email; String username; DateTime createdAt; - User(this.token, this.id, this.email, this.username, this.createdAt); + User( this.id, this.email, this.username, this.createdAt); factory User.fromJson(Map json) { return User( - json['token'], json['id'], json['email'], json['username'], diff --git a/Mobile/lib/profile.dart b/Mobile/lib/profile.dart index 3b83790..e76be05 100644 --- a/Mobile/lib/profile.dart +++ b/Mobile/lib/profile.dart @@ -1,45 +1,63 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:mobile/base/variables.dart'; import 'package:mobile/models.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'base/sidemenu.dart'; -import 'package:mobile/base/variables.dart'; - - - +import 'api.dart' as api; class ProfilePage extends StatefulWidget { - const ProfilePage({super.key}); - //const ProfilePage({super.key, required this.id}); - @override State createState() => _ProfilePageState(); } class _ProfilePageState extends State { - late User userData; + User? userData; @override - void initState() { + void initState() { super.initState(); - - // Check if the user is logged in when the page is entered - if (loggedIn == false) { - WidgetsBinding.instance.addPostFrameCallback((_) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Please log in'))); - Navigator.pushReplacementNamed(context, '/login'); - }); - } - - setState(() { - userData = user!; - }); + getProfile(); } + Future getProfile() async { + if (!loggedIn) { + WidgetsBinding.instance.addPostFrameCallback((_) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Please log in')), + ); + Navigator.pushReplacementNamed(context, '/login'); + }); + return; + } - + if (user != null) { + setState(() { + userData = user!; + }); + } else { + final prefs = await SharedPreferences.getInstance(); + String? id = prefs.getString('id'); + + if (id != null) { + final response = await api + .request(context, api.ApiService.auth, 'GET', '/api/users/$id', null); + + if (response == null) return; + + Map json = jsonDecode(response); + User jsonUser = User.fromJson(json); + + setState(() { + userData = jsonUser; + user = jsonUser; + }); + } + } + } @override Widget build(BuildContext context) { @@ -58,33 +76,41 @@ class _ProfilePageState extends State { ), ), Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Icon( - Icons.account_circle, - size: 100, - color: Colors.grey, - ), - const SizedBox(height: 20), - const Text( - 'Username', - style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), - ), - const SizedBox(height: 10), - const Text( - 'email@example.com', - style: TextStyle(fontSize: 16, color: Colors.grey), - ), - const SizedBox(height: 50), - ElevatedButton( - onPressed: () { - // Add your edit action here - }, - child: const Text('Edit'), - ), - ], - ), + child: userData == null + ? const CircularProgressIndicator() + : Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Icon( + Icons.account_circle, + size: 100, + color: Colors.grey, + ), + const SizedBox(height: 20), + Text( + userData!.username, + style: const TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(height: 10), + Text( + userData!.email, + style: const TextStyle( + fontSize: 16, + color: Colors.grey, + ), + ), + const SizedBox(height: 50), + ElevatedButton( + onPressed: () { + // Add your edit action here + }, + child: const Text('Edit'), + ), + ], + ), ), ], ),