UserTileLikes: Added distance info

master
Rafael 2024-06-17 15:05:41 +02:00
parent 17e097d483
commit 210cfd85bd
2 changed files with 76 additions and 17 deletions

View File

@ -2,10 +2,14 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../constants.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 DocumentSnapshot user;
final bool hasMatch; final bool hasMatch;
final UserProfile currentUser;
final VoidCallback onUnlike; final VoidCallback onUnlike;
final VoidCallback onShowChat; final VoidCallback onShowChat;
final VoidCallback onViewInfo; final VoidCallback onViewInfo;
@ -14,41 +18,83 @@ class UserTileLikes extends StatelessWidget {
super.key, super.key,
required this.user, required this.user,
required this.hasMatch, required this.hasMatch,
required this.currentUser,
required this.onUnlike, required this.onUnlike,
required this.onShowChat, required this.onShowChat,
required this.onViewInfo, required this.onViewInfo,
}); });
@override
State<UserTileLikes> createState() => _UserTileLikesState();
}
class _UserTileLikesState extends State<UserTileLikes> {
UserProfile? _otherUser;
bool _isLoading = true;
@override
void initState() {
super.initState();
_fetchUserProfile();
}
Future<void> _fetchUserProfile() async {
UserProfile userProfile =
await UserService.getUserProfileFromDocument(widget.user);
setState(() {
_otherUser = userProfile;
_isLoading = false;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Map<String, dynamic> userMap = user.data() as Map<String, dynamic>; if (_isLoading) {
return const CircularProgressIndicator();
}
Map<String, dynamic> userMap = widget.user.data() as Map<String, dynamic>;
bool hasPictureUrl = userMap.containsKey(Constants.dbFieldUsersProfilePic); bool hasPictureUrl = userMap.containsKey(Constants.dbFieldUsersProfilePic);
bool hasName = userMap.containsKey(Constants.dbFieldUsersName); bool hasName = userMap.containsKey(Constants.dbFieldUsersName);
bool hasBio = userMap.containsKey(Constants.dbFieldUsersBio); bool hasBio = userMap.containsKey(Constants.dbFieldUsersBio);
String? shortDist = (_otherUser != null
? shortestDistanceBetweenUsers(widget.currentUser, _otherUser!)
.toStringAsFixed(0)
: null);
return Card( return Card(
margin: const EdgeInsets.all(8.0), margin: const EdgeInsets.all(8.0),
child: ListTile( child: ListTile(
leading: hasPictureUrl == true && leading: Column(
user[Constants.dbFieldUsersProfilePic] != null mainAxisSize: MainAxisSize.min,
? CircleAvatar( mainAxisAlignment: MainAxisAlignment.center,
backgroundImage: NetworkImage( children: [
user[Constants.dbFieldUsersProfilePic], hasPictureUrl == true &&
), widget.user[Constants.dbFieldUsersProfilePic] != null
) ? CircleAvatar(
: const CircleAvatar( maxRadius: 16,
child: Icon(Icons.person), backgroundImage: NetworkImage(
), widget.user[Constants.dbFieldUsersProfilePic],
),
)
: const CircleAvatar(
maxRadius: 16,
child: Icon(Icons.person),
),
Text('$shortDist km'),
],
),
title: hasName title: hasName
? Text( ? Text(
'${user[Constants.dbFieldUsersName]}', '${widget.user[Constants.dbFieldUsersName]}',
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
maxLines: 1, maxLines: 1,
) )
: null, : null,
subtitle: hasBio subtitle: hasBio
? Text( ? Text(
user[Constants.dbFieldUsersBio], widget.user[Constants.dbFieldUsersBio],
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
maxLines: 3, maxLines: 3,
) )
@ -58,13 +104,13 @@ class UserTileLikes extends StatelessWidget {
children: [ children: [
IconButton( IconButton(
icon: const Icon(Icons.contact_page_outlined), icon: const Icon(Icons.contact_page_outlined),
onPressed: onViewInfo, onPressed: widget.onViewInfo,
), ),
IconButton( IconButton(
icon: hasMatch icon: widget.hasMatch
? const Icon(Icons.chat_outlined) ? const Icon(Icons.chat_outlined)
: const Icon(Icons.delete_outline, color: Colors.red), : const Icon(Icons.delete_outline, color: Colors.red),
onPressed: hasMatch ? onShowChat : onUnlike, onPressed: widget.hasMatch ? widget.onShowChat : widget.onUnlike,
), ),
], ],
), ),

View File

@ -5,7 +5,9 @@ import '../components/user_tile_likes.dart';
import '../constants.dart'; import '../constants.dart';
import '../enumerations.dart'; import '../enumerations.dart';
import '../models/swipe.dart'; import '../models/swipe.dart';
import '../models/user_profile.dart';
import '../services/auth/auth_service.dart'; import '../services/auth/auth_service.dart';
import '../services/user_service.dart';
import '../utils/helper_dialogs.dart'; import '../utils/helper_dialogs.dart';
import 'chat_page.dart'; import 'chat_page.dart';
import 'user_profile_page.dart'; import 'user_profile_page.dart';
@ -23,6 +25,7 @@ class LikedUsersPageState extends State<LikedUsersPage> {
List<MapEntry<Swipe, DocumentSnapshot>> _likedUsersWithSwipes = []; List<MapEntry<Swipe, DocumentSnapshot>> _likedUsersWithSwipes = [];
bool _isLoading = true; bool _isLoading = true;
String? _fetchError; String? _fetchError;
UserProfile? _currentUser;
ViewOrder _orderPreference = ViewOrder.swipedFirst; ViewOrder _orderPreference = ViewOrder.swipedFirst;
MenuSort _sortPreference = MenuSort.nameAsc; MenuSort _sortPreference = MenuSort.nameAsc;
@ -30,9 +33,18 @@ class LikedUsersPageState extends State<LikedUsersPage> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_loadCurrentUserProfile();
_fetchLikedUsersWithSwipes(); _fetchLikedUsersWithSwipes();
} }
Future<void> _loadCurrentUserProfile() async {
try {
_currentUser = await UserService.getUserProfileById(currentUserId);
} catch (e) {
debugPrint("Error loading current user profile: $e");
}
}
Future<void> _fetchLikedUsersWithSwipes() async { Future<void> _fetchLikedUsersWithSwipes() async {
try { try {
QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance
@ -311,6 +323,7 @@ class LikedUsersPageState extends State<LikedUsersPage> {
return UserTileLikes( return UserTileLikes(
user: user, user: user,
hasMatch: hasMatch, hasMatch: hasMatch,
currentUser: _currentUser!,
onUnlike: () async { onUnlike: () async {
Map<String, dynamic> userMap = Map<String, dynamic> userMap =
user.data() as Map<String, dynamic>; user.data() as Map<String, dynamic>;