LikedUsersPage: Sort View (2)
parent
cddb2078ed
commit
25b76e6274
|
@ -33,6 +33,10 @@ class Constants {
|
||||||
static const String dbFieldMessageSenderEmail = 'senderEmail';
|
static const String dbFieldMessageSenderEmail = 'senderEmail';
|
||||||
static const String dbFieldMessageTimestamp = 'timestamp';
|
static const String dbFieldMessageTimestamp = 'timestamp';
|
||||||
|
|
||||||
|
static const String dbFieldSwipesSwipedId = 'swipedId';
|
||||||
|
static const String dbFieldSwipesLike = 'liked';
|
||||||
|
static const String dbFieldSwipesTimestamp = 'timestamp';
|
||||||
|
|
||||||
static const String dbFieldUsersID = 'uid';
|
static const String dbFieldUsersID = 'uid';
|
||||||
static const String dbFieldUsersEmail = 'email';
|
static const String dbFieldUsersEmail = 'email';
|
||||||
static const String dbFieldUsersName = 'name';
|
static const String dbFieldUsersName = 'name';
|
||||||
|
|
|
@ -1,3 +1,17 @@
|
||||||
|
enum MenuSort {
|
||||||
|
nameAsc,
|
||||||
|
nameDesc,
|
||||||
|
timestampAsc,
|
||||||
|
timestampDesc,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ViewOrder {
|
||||||
|
swipedFirst,
|
||||||
|
matchedFirst,
|
||||||
|
swipedOnly,
|
||||||
|
matchedOnly,
|
||||||
|
}
|
||||||
|
|
||||||
enum Gender {
|
enum Gender {
|
||||||
none,
|
none,
|
||||||
male,
|
male,
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
|
import '../constants.dart';
|
||||||
|
|
||||||
|
class Swipe {
|
||||||
|
final String swiperId;
|
||||||
|
final String swipedId;
|
||||||
|
final bool liked;
|
||||||
|
final DateTime timestamp;
|
||||||
|
|
||||||
|
Swipe(
|
||||||
|
{required this.swiperId,
|
||||||
|
required this.swipedId,
|
||||||
|
required this.liked,
|
||||||
|
required this.timestamp});
|
||||||
|
|
||||||
|
factory Swipe.fromDocument(DocumentSnapshot doc) {
|
||||||
|
var data = doc.data() as Map<String, dynamic>;
|
||||||
|
return Swipe(
|
||||||
|
swiperId: doc.id, // Assign document ID
|
||||||
|
swipedId: data[Constants.dbFieldSwipesSwipedId],
|
||||||
|
liked: data[Constants.dbFieldSwipesLike] as bool,
|
||||||
|
timestamp: data[Constants.dbFieldSwipesTimestamp].toDate(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,13 +3,11 @@ import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
|
|
||||||
import '../components/user_tile_likes.dart';
|
import '../components/user_tile_likes.dart';
|
||||||
import '../constants.dart';
|
import '../constants.dart';
|
||||||
|
import '../enumerations.dart';
|
||||||
|
import '../models/swipe.dart';
|
||||||
import '../services/auth/auth_service.dart';
|
import '../services/auth/auth_service.dart';
|
||||||
import '../utils/helper.dart';
|
import '../utils/helper.dart';
|
||||||
|
|
||||||
enum MenuSort { nameAsc, nameDesc, timestampAsc, timestampDesc }
|
|
||||||
|
|
||||||
enum ViewOrder { swipedFirst, matchedFirst }
|
|
||||||
|
|
||||||
class LikedUsersPage extends StatefulWidget {
|
class LikedUsersPage extends StatefulWidget {
|
||||||
const LikedUsersPage({super.key});
|
const LikedUsersPage({super.key});
|
||||||
|
|
||||||
|
@ -19,31 +17,55 @@ class LikedUsersPage extends StatefulWidget {
|
||||||
|
|
||||||
class LikedUsersPageState extends State<LikedUsersPage> {
|
class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
final String currentUserId = AuthService().getCurrentUser()!.uid;
|
final String currentUserId = AuthService().getCurrentUser()!.uid;
|
||||||
|
final Map<String, bool> _cachedMatches = {};
|
||||||
|
List<MapEntry<Swipe, DocumentSnapshot>> _likedUsersWithSwipes = [];
|
||||||
|
bool _isLoading = true;
|
||||||
|
String? _fetchError;
|
||||||
|
|
||||||
ViewOrder _orderPreference = ViewOrder.swipedFirst;
|
ViewOrder _orderPreference = ViewOrder.swipedFirst;
|
||||||
MenuSort _sortPreference = MenuSort.nameAsc;
|
MenuSort _sortPreference = MenuSort.nameAsc;
|
||||||
|
|
||||||
Future<List<DocumentSnapshot>> _fetchLikedUsers() async {
|
@override
|
||||||
QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance
|
void initState() {
|
||||||
.collection(Constants.dbCollectionUsers)
|
super.initState();
|
||||||
.doc(currentUserId)
|
_fetchLikedUsersWithSwipes();
|
||||||
.collection(Constants.dbCollectionSwipes)
|
}
|
||||||
.where('liked', isEqualTo: true)
|
|
||||||
.get();
|
|
||||||
|
|
||||||
List<String> likedUserIds =
|
Future<void> _fetchLikedUsersWithSwipes() async {
|
||||||
likedUsersSnapshot.docs.map((doc) => doc.id).toList();
|
try {
|
||||||
|
QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance
|
||||||
List<DocumentSnapshot> likedUsers = [];
|
|
||||||
for (String userId in likedUserIds) {
|
|
||||||
DocumentSnapshot userDoc = await FirebaseFirestore.instance
|
|
||||||
.collection(Constants.dbCollectionUsers)
|
.collection(Constants.dbCollectionUsers)
|
||||||
.doc(userId)
|
.doc(currentUserId)
|
||||||
|
.collection(Constants.dbCollectionSwipes)
|
||||||
|
.where(Constants.dbFieldSwipesLike, isEqualTo: true)
|
||||||
.get();
|
.get();
|
||||||
likedUsers.add(userDoc);
|
|
||||||
}
|
|
||||||
|
|
||||||
return likedUsers;
|
List<MapEntry<Swipe, DocumentSnapshot>> likedUsersWithSwipes = [];
|
||||||
|
|
||||||
|
for (var doc in likedUsersSnapshot.docs) {
|
||||||
|
Swipe swipe = Swipe.fromDocument(doc);
|
||||||
|
DocumentSnapshot userDoc = await FirebaseFirestore.instance
|
||||||
|
.collection(Constants.dbCollectionUsers)
|
||||||
|
.doc(swipe.swipedId)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
// Initialize _cachedMatches to keep database accesses to a minimum
|
||||||
|
bool hasMatch = await _hasMatch(swipe.swipedId);
|
||||||
|
_cachedMatches[swipe.swipedId] = hasMatch;
|
||||||
|
|
||||||
|
likedUsersWithSwipes.add(MapEntry(swipe, userDoc));
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_likedUsersWithSwipes = likedUsersWithSwipes;
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
setState(() {
|
||||||
|
_fetchError = e.toString();
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> _hasMatch(String userId) async {
|
Future<bool> _hasMatch(String userId) async {
|
||||||
|
@ -63,10 +85,17 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
.doc(currentUserId)
|
.doc(currentUserId)
|
||||||
.collection(Constants.dbCollectionSwipes)
|
.collection(Constants.dbCollectionSwipes)
|
||||||
.doc(userId)
|
.doc(userId)
|
||||||
.delete(); //.update({'liked': false});
|
.delete();
|
||||||
setState(() {}); // Refresh the UI
|
|
||||||
|
// Refresh the UI
|
||||||
|
setState(() {
|
||||||
|
_likedUsersWithSwipes
|
||||||
|
.removeWhere((entry) => entry.key.swipedId == userId);
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
showMsg(context, 'Error unlike', e.toString());
|
if (mounted) {
|
||||||
|
showMsg(context, 'Error during unlike', e.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +119,7 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _showConfirmationDialog(String userId, String userName) async {
|
Future<bool?> _showConfirmationDialog(String userId, String userName) async {
|
||||||
bool? confirm = await showDialog<bool>(
|
bool? confirm = await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (BuildContext context) {
|
builder: (BuildContext context) {
|
||||||
|
@ -111,9 +140,7 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (confirm == true) {
|
return confirm;
|
||||||
await _unlikeUser(userId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showUserInfo(DocumentSnapshot user) {
|
void _showUserInfo(DocumentSnapshot user) {
|
||||||
|
@ -142,39 +169,65 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<DocumentSnapshot>> _fetchSortedLikedUsers() async {
|
List<MapEntry<Swipe, DocumentSnapshot>> _getSortedLikedUsersWithSwipes() {
|
||||||
List<DocumentSnapshot> likedUsers = await _fetchLikedUsers();
|
List<MapEntry<Swipe, DocumentSnapshot>> likedOnlyUsers = [];
|
||||||
List<DocumentSnapshot> likedOnlyUsers = [];
|
List<MapEntry<Swipe, DocumentSnapshot>> matchedUsers = [];
|
||||||
List<DocumentSnapshot> matchedUsers = [];
|
|
||||||
|
for (var entry in _likedUsersWithSwipes) {
|
||||||
|
bool hasMatch = _cachedMatches.containsKey(entry.key.swipedId)
|
||||||
|
? _cachedMatches[entry.key.swipedId]!
|
||||||
|
: false;
|
||||||
|
|
||||||
for (DocumentSnapshot user in likedUsers) {
|
|
||||||
bool hasMatch = await _hasMatch(user.id);
|
|
||||||
if (hasMatch) {
|
if (hasMatch) {
|
||||||
matchedUsers.add(user);
|
matchedUsers.add(entry);
|
||||||
} else {
|
} else {
|
||||||
likedOnlyUsers.add(user);
|
likedOnlyUsers.add(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_sortPreference == MenuSort.nameAsc) {
|
if (_sortPreference == MenuSort.timestampAsc) {
|
||||||
likedOnlyUsers.sort((a, b) => (a[Constants.dbFieldUsersName] as String)
|
likedOnlyUsers.sort((a, b) => a.key.timestamp.compareTo(b.key.timestamp));
|
||||||
.compareTo(b[Constants.dbFieldUsersName] as String));
|
matchedUsers.sort((a, b) => a.key.timestamp.compareTo(b.key.timestamp));
|
||||||
matchedUsers.sort((a, b) => (a[Constants.dbFieldUsersName] as String)
|
} else if (_sortPreference == MenuSort.timestampDesc) {
|
||||||
.compareTo(b[Constants.dbFieldUsersName] as String));
|
likedOnlyUsers.sort((a, b) => b.key.timestamp.compareTo(a.key.timestamp));
|
||||||
|
matchedUsers.sort((a, b) => b.key.timestamp.compareTo(a.key.timestamp));
|
||||||
|
} else if (_sortPreference == MenuSort.nameAsc) {
|
||||||
|
likedOnlyUsers.sort((a, b) => _compareNames(a, b));
|
||||||
|
matchedUsers.sort((a, b) => _compareNames(a, b));
|
||||||
} else if (_sortPreference == MenuSort.nameDesc) {
|
} else if (_sortPreference == MenuSort.nameDesc) {
|
||||||
likedOnlyUsers.sort((a, b) => (b[Constants.dbFieldUsersName] as String)
|
likedOnlyUsers.sort((a, b) => _compareNames(b, a));
|
||||||
.compareTo(a[Constants.dbFieldUsersName] as String));
|
matchedUsers.sort((a, b) => _compareNames(b, a));
|
||||||
matchedUsers.sort((a, b) => (b[Constants.dbFieldUsersName] as String)
|
|
||||||
.compareTo(a[Constants.dbFieldUsersName] as String));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_orderPreference == ViewOrder.swipedFirst) {
|
if (_orderPreference == ViewOrder.matchedFirst) {
|
||||||
return [...likedOnlyUsers, ...matchedUsers];
|
|
||||||
} else {
|
|
||||||
return [...matchedUsers, ...likedOnlyUsers];
|
return [...matchedUsers, ...likedOnlyUsers];
|
||||||
|
} else if (_orderPreference == ViewOrder.swipedOnly) {
|
||||||
|
return [...likedOnlyUsers];
|
||||||
|
} else if (_orderPreference == ViewOrder.matchedOnly) {
|
||||||
|
return [...matchedUsers];
|
||||||
|
} else {
|
||||||
|
// default: swipesFirst
|
||||||
|
return [...likedOnlyUsers, ...matchedUsers];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int _compareNames(MapEntry<Swipe, DocumentSnapshot> a,
|
||||||
|
MapEntry<Swipe, DocumentSnapshot> b) {
|
||||||
|
final dataA = (a.value.data() as Map<String, dynamic>);
|
||||||
|
final dataB = (b.value.data() as Map<String, dynamic>);
|
||||||
|
|
||||||
|
final nameA =
|
||||||
|
dataA.isNotEmpty && dataA.containsKey(Constants.dbFieldUsersName)
|
||||||
|
? a.value[Constants.dbFieldUsersName]
|
||||||
|
: '';
|
||||||
|
final nameB =
|
||||||
|
dataB.isNotEmpty && dataB.containsKey(Constants.dbFieldUsersName)
|
||||||
|
? b.value[Constants.dbFieldUsersName]
|
||||||
|
: '';
|
||||||
|
|
||||||
|
return nameA.compareTo(nameB);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
@ -192,11 +245,21 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
value: _orderPreference,
|
value: _orderPreference,
|
||||||
items: const [
|
items: const [
|
||||||
DropdownMenuItem(
|
DropdownMenuItem(
|
||||||
value: ViewOrder.swipedFirst,
|
value: ViewOrder.swipedFirst,
|
||||||
child: Text('Swiped First')),
|
child: Text('Swipes First'),
|
||||||
|
),
|
||||||
DropdownMenuItem(
|
DropdownMenuItem(
|
||||||
value: ViewOrder.matchedFirst,
|
value: ViewOrder.matchedFirst,
|
||||||
child: Text('Matched First')),
|
child: Text('Matches First'),
|
||||||
|
),
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: ViewOrder.swipedOnly,
|
||||||
|
child: Text('Swipes Only'),
|
||||||
|
),
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: ViewOrder.matchedOnly,
|
||||||
|
child: Text('Matches Only'),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
// update UI on change only
|
// update UI on change only
|
||||||
|
@ -239,66 +302,82 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
||||||
title: const Text('Name Descending'),
|
title: const Text('Name Descending'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
PopupMenuItem<MenuSort>(
|
||||||
|
value: MenuSort.timestampAsc,
|
||||||
|
child: ListTile(
|
||||||
|
leading: _sortPreference == MenuSort.timestampAsc
|
||||||
|
? const Icon(Icons.check)
|
||||||
|
: const Icon(null),
|
||||||
|
title: const Text('Oldest first'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PopupMenuItem<MenuSort>(
|
||||||
|
value: MenuSort.timestampDesc,
|
||||||
|
child: ListTile(
|
||||||
|
leading: _sortPreference == MenuSort.timestampDesc
|
||||||
|
? const Icon(Icons.check)
|
||||||
|
: const Icon(null),
|
||||||
|
title: const Text('Newest first'),
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
buildLikedUserList(),
|
_isLoading
|
||||||
|
? const Center(child: CircularProgressIndicator())
|
||||||
|
: _fetchError != null
|
||||||
|
? Center(child: Text('Error: $_fetchError'))
|
||||||
|
: _likedUsersWithSwipes.isEmpty
|
||||||
|
? const Center(child: Text('No liked users found.'))
|
||||||
|
: buildLikedUserList(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget buildLikedUserList() {
|
Widget buildLikedUserList() {
|
||||||
return Expanded(
|
var sortedSwipeUsers = _getSortedLikedUsersWithSwipes();
|
||||||
child: FutureBuilder<List<DocumentSnapshot>>(
|
|
||||||
future: _fetchSortedLikedUsers(),
|
|
||||||
builder: (BuildContext context,
|
|
||||||
AsyncSnapshot<List<DocumentSnapshot>> snapshot) {
|
|
||||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
||||||
return const Center(child: CircularProgressIndicator());
|
|
||||||
} else if (snapshot.hasError) {
|
|
||||||
return Center(child: Text('Error: ${snapshot.error}'));
|
|
||||||
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
|
||||||
return const Center(child: Text('No liked users found.'));
|
|
||||||
} else {
|
|
||||||
return ListView.builder(
|
|
||||||
itemCount: snapshot.data!.length,
|
|
||||||
itemBuilder: (BuildContext context, int index) {
|
|
||||||
DocumentSnapshot user = snapshot.data![index];
|
|
||||||
return FutureBuilder<bool>(
|
|
||||||
future: _hasMatch(user.id),
|
|
||||||
builder: (context, snapshot) {
|
|
||||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
||||||
return const CircularProgressIndicator();
|
|
||||||
}
|
|
||||||
bool hasMatch = snapshot.data ?? false;
|
|
||||||
return UserTileLikes(
|
|
||||||
user: user,
|
|
||||||
hasMatch: hasMatch,
|
|
||||||
onUnlike: () {
|
|
||||||
Map<String, dynamic> userMap =
|
|
||||||
user.data() as Map<String, dynamic>;
|
|
||||||
bool hasName =
|
|
||||||
userMap.containsKey(Constants.dbFieldUsersName);
|
|
||||||
|
|
||||||
_showConfirmationDialog(
|
return Expanded(
|
||||||
user.id,
|
child: ListView.builder(
|
||||||
(hasName
|
itemCount: sortedSwipeUsers.length,
|
||||||
? user[Constants.dbFieldUsersName]
|
itemBuilder: (BuildContext context, int index) {
|
||||||
: 'Name: n/a'),
|
var entry = sortedSwipeUsers[index];
|
||||||
);
|
var user = entry.value;
|
||||||
}, //_unlikeUser(user.id),
|
bool hasMatch = _cachedMatches.containsKey(user.id)
|
||||||
onShowMatchMessage: _showMatchMessage,
|
? _cachedMatches[user.id]!
|
||||||
onViewInfo: () => _showUserInfo(user),
|
: false;
|
||||||
);
|
|
||||||
},
|
return UserTileLikes(
|
||||||
);
|
user: user,
|
||||||
},
|
hasMatch: hasMatch,
|
||||||
);
|
onUnlike: () async {
|
||||||
}
|
Map<String, dynamic> userMap =
|
||||||
|
user.data() as Map<String, dynamic>;
|
||||||
|
bool hasName = userMap.containsKey(Constants.dbFieldUsersName);
|
||||||
|
|
||||||
|
bool? confirm = await _showConfirmationDialog(
|
||||||
|
user.id,
|
||||||
|
(hasName ? user[Constants.dbFieldUsersName] : 'Name: n/a'),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (confirm == true) {
|
||||||
|
bool hasMatch = await _hasMatch(user.id);
|
||||||
|
if (!hasMatch) {
|
||||||
|
await _unlikeUser(user.id);
|
||||||
|
} else {
|
||||||
|
// abort, update _cachedMatches and UI
|
||||||
|
_cachedMatches[user.id] = hasMatch;
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, //_unlikeUser(user.id),
|
||||||
|
onShowMatchMessage: _showMatchMessage,
|
||||||
|
onViewInfo: () => _showUserInfo(user),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue