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 '../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<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
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 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
leading: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
hasPictureUrl == true &&
widget.user[Constants.dbFieldUsersProfilePic] != null
? CircleAvatar(
maxRadius: 16,
backgroundImage: NetworkImage(
user[Constants.dbFieldUsersProfilePic],
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,
),
],
),

View File

@ -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<LikedUsersPage> {
List<MapEntry<Swipe, DocumentSnapshot>> _likedUsersWithSwipes = [];
bool _isLoading = true;
String? _fetchError;
UserProfile? _currentUser;
ViewOrder _orderPreference = ViewOrder.swipedFirst;
MenuSort _sortPreference = MenuSort.nameAsc;
@ -30,9 +33,18 @@ class LikedUsersPageState extends State<LikedUsersPage> {
@override
void initState() {
super.initState();
_loadCurrentUserProfile();
_fetchLikedUsersWithSwipes();
}
Future<void> _loadCurrentUserProfile() async {
try {
_currentUser = await UserService.getUserProfileById(currentUserId);
} catch (e) {
debugPrint("Error loading current user profile: $e");
}
}
Future<void> _fetchLikedUsersWithSwipes() async {
try {
QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance
@ -311,6 +323,7 @@ class LikedUsersPageState extends State<LikedUsersPage> {
return UserTileLikes(
user: user,
hasMatch: hasMatch,
currentUser: _currentUser!,
onUnlike: () async {
Map<String, dynamic> userMap =
user.data() as Map<String, dynamic>;