From 210cfd85bd5be276ac7abbb5f1cec3fe3189f646 Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Mon, 17 Jun 2024 15:05:41 +0200 Subject: [PATCH] UserTileLikes: Added distance info --- lib/components/user_tile_likes.dart | 80 +++++++++++++++++++++++------ lib/pages/liked_users_page.dart | 13 +++++ 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/lib/components/user_tile_likes.dart b/lib/components/user_tile_likes.dart index 4854b83..8050273 100644 --- a/lib/components/user_tile_likes.dart +++ b/lib/components/user_tile_likes.dart @@ -2,10 +2,14 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; import '../constants.dart'; +import '../models/user_profile.dart'; +import '../services/user_service.dart'; +import '../utils/math.dart'; -class UserTileLikes extends StatelessWidget { +class UserTileLikes extends StatefulWidget { final DocumentSnapshot user; final bool hasMatch; + final UserProfile currentUser; final VoidCallback onUnlike; final VoidCallback onShowChat; final VoidCallback onViewInfo; @@ -14,41 +18,83 @@ class UserTileLikes extends StatelessWidget { super.key, required this.user, required this.hasMatch, + required this.currentUser, required this.onUnlike, required this.onShowChat, required this.onViewInfo, }); + @override + State createState() => _UserTileLikesState(); +} + +class _UserTileLikesState extends State { + UserProfile? _otherUser; + bool _isLoading = true; + + @override + void initState() { + super.initState(); + _fetchUserProfile(); + } + + Future _fetchUserProfile() async { + UserProfile userProfile = + await UserService.getUserProfileFromDocument(widget.user); + setState(() { + _otherUser = userProfile; + _isLoading = false; + }); + } + @override Widget build(BuildContext context) { - Map userMap = user.data() as Map; + if (_isLoading) { + return const CircularProgressIndicator(); + } + + Map userMap = widget.user.data() as Map; bool hasPictureUrl = userMap.containsKey(Constants.dbFieldUsersProfilePic); bool hasName = userMap.containsKey(Constants.dbFieldUsersName); bool hasBio = userMap.containsKey(Constants.dbFieldUsersBio); + String? shortDist = (_otherUser != null + ? shortestDistanceBetweenUsers(widget.currentUser, _otherUser!) + .toStringAsFixed(0) + : null); + return Card( margin: const EdgeInsets.all(8.0), child: ListTile( - leading: hasPictureUrl == true && - user[Constants.dbFieldUsersProfilePic] != null - ? CircleAvatar( - backgroundImage: NetworkImage( - user[Constants.dbFieldUsersProfilePic], - ), - ) - : const CircleAvatar( - child: Icon(Icons.person), - ), + leading: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + hasPictureUrl == true && + widget.user[Constants.dbFieldUsersProfilePic] != null + ? CircleAvatar( + maxRadius: 16, + backgroundImage: NetworkImage( + widget.user[Constants.dbFieldUsersProfilePic], + ), + ) + : const CircleAvatar( + maxRadius: 16, + child: Icon(Icons.person), + ), + Text('$shortDist km'), + ], + ), title: hasName ? Text( - '${user[Constants.dbFieldUsersName]}', + '${widget.user[Constants.dbFieldUsersName]}', overflow: TextOverflow.ellipsis, maxLines: 1, ) : null, subtitle: hasBio ? Text( - user[Constants.dbFieldUsersBio], + widget.user[Constants.dbFieldUsersBio], overflow: TextOverflow.ellipsis, maxLines: 3, ) @@ -58,13 +104,13 @@ class UserTileLikes extends StatelessWidget { children: [ IconButton( icon: const Icon(Icons.contact_page_outlined), - onPressed: onViewInfo, + onPressed: widget.onViewInfo, ), IconButton( - icon: hasMatch + icon: widget.hasMatch ? const Icon(Icons.chat_outlined) : const Icon(Icons.delete_outline, color: Colors.red), - onPressed: hasMatch ? onShowChat : onUnlike, + onPressed: widget.hasMatch ? widget.onShowChat : widget.onUnlike, ), ], ), diff --git a/lib/pages/liked_users_page.dart b/lib/pages/liked_users_page.dart index 1581975..75491bb 100644 --- a/lib/pages/liked_users_page.dart +++ b/lib/pages/liked_users_page.dart @@ -5,7 +5,9 @@ import '../components/user_tile_likes.dart'; import '../constants.dart'; import '../enumerations.dart'; import '../models/swipe.dart'; +import '../models/user_profile.dart'; import '../services/auth/auth_service.dart'; +import '../services/user_service.dart'; import '../utils/helper_dialogs.dart'; import 'chat_page.dart'; import 'user_profile_page.dart'; @@ -23,6 +25,7 @@ class LikedUsersPageState extends State { List> _likedUsersWithSwipes = []; bool _isLoading = true; String? _fetchError; + UserProfile? _currentUser; ViewOrder _orderPreference = ViewOrder.swipedFirst; MenuSort _sortPreference = MenuSort.nameAsc; @@ -30,9 +33,18 @@ class LikedUsersPageState extends State { @override void initState() { super.initState(); + _loadCurrentUserProfile(); _fetchLikedUsersWithSwipes(); } + Future _loadCurrentUserProfile() async { + try { + _currentUser = await UserService.getUserProfileById(currentUserId); + } catch (e) { + debugPrint("Error loading current user profile: $e"); + } + } + Future _fetchLikedUsersWithSwipes() async { try { QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance @@ -311,6 +323,7 @@ class LikedUsersPageState extends State { return UserTileLikes( user: user, hasMatch: hasMatch, + currentUser: _currentUser!, onUnlike: () async { Map userMap = user.data() as Map;