LikedUsersPage: Sort View (2)
parent
cddb2078ed
commit
25b76e6274
|
@ -33,6 +33,10 @@ class Constants {
|
|||
static const String dbFieldMessageSenderEmail = 'senderEmail';
|
||||
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 dbFieldUsersEmail = 'email';
|
||||
static const String dbFieldUsersName = 'name';
|
||||
|
|
|
@ -1,3 +1,17 @@
|
|||
enum MenuSort {
|
||||
nameAsc,
|
||||
nameDesc,
|
||||
timestampAsc,
|
||||
timestampDesc,
|
||||
}
|
||||
|
||||
enum ViewOrder {
|
||||
swipedFirst,
|
||||
matchedFirst,
|
||||
swipedOnly,
|
||||
matchedOnly,
|
||||
}
|
||||
|
||||
enum Gender {
|
||||
none,
|
||||
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 '../constants.dart';
|
||||
import '../enumerations.dart';
|
||||
import '../models/swipe.dart';
|
||||
import '../services/auth/auth_service.dart';
|
||||
import '../utils/helper.dart';
|
||||
|
||||
enum MenuSort { nameAsc, nameDesc, timestampAsc, timestampDesc }
|
||||
|
||||
enum ViewOrder { swipedFirst, matchedFirst }
|
||||
|
||||
class LikedUsersPage extends StatefulWidget {
|
||||
const LikedUsersPage({super.key});
|
||||
|
||||
|
@ -19,31 +17,55 @@ class LikedUsersPage extends StatefulWidget {
|
|||
|
||||
class LikedUsersPageState extends State<LikedUsersPage> {
|
||||
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;
|
||||
MenuSort _sortPreference = MenuSort.nameAsc;
|
||||
|
||||
Future<List<DocumentSnapshot>> _fetchLikedUsers() async {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fetchLikedUsersWithSwipes();
|
||||
}
|
||||
|
||||
Future<void> _fetchLikedUsersWithSwipes() async {
|
||||
try {
|
||||
QuerySnapshot likedUsersSnapshot = await FirebaseFirestore.instance
|
||||
.collection(Constants.dbCollectionUsers)
|
||||
.doc(currentUserId)
|
||||
.collection(Constants.dbCollectionSwipes)
|
||||
.where('liked', isEqualTo: true)
|
||||
.where(Constants.dbFieldSwipesLike, isEqualTo: true)
|
||||
.get();
|
||||
|
||||
List<String> likedUserIds =
|
||||
likedUsersSnapshot.docs.map((doc) => doc.id).toList();
|
||||
List<MapEntry<Swipe, DocumentSnapshot>> likedUsersWithSwipes = [];
|
||||
|
||||
List<DocumentSnapshot> likedUsers = [];
|
||||
for (String userId in likedUserIds) {
|
||||
for (var doc in likedUsersSnapshot.docs) {
|
||||
Swipe swipe = Swipe.fromDocument(doc);
|
||||
DocumentSnapshot userDoc = await FirebaseFirestore.instance
|
||||
.collection(Constants.dbCollectionUsers)
|
||||
.doc(userId)
|
||||
.doc(swipe.swipedId)
|
||||
.get();
|
||||
likedUsers.add(userDoc);
|
||||
|
||||
// Initialize _cachedMatches to keep database accesses to a minimum
|
||||
bool hasMatch = await _hasMatch(swipe.swipedId);
|
||||
_cachedMatches[swipe.swipedId] = hasMatch;
|
||||
|
||||
likedUsersWithSwipes.add(MapEntry(swipe, userDoc));
|
||||
}
|
||||
|
||||
return likedUsers;
|
||||
setState(() {
|
||||
_likedUsersWithSwipes = likedUsersWithSwipes;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_fetchError = e.toString();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _hasMatch(String userId) async {
|
||||
|
@ -63,10 +85,17 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
|||
.doc(currentUserId)
|
||||
.collection(Constants.dbCollectionSwipes)
|
||||
.doc(userId)
|
||||
.delete(); //.update({'liked': false});
|
||||
setState(() {}); // Refresh the UI
|
||||
.delete();
|
||||
|
||||
// Refresh the UI
|
||||
setState(() {
|
||||
_likedUsersWithSwipes
|
||||
.removeWhere((entry) => entry.key.swipedId == userId);
|
||||
});
|
||||
} 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>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
|
@ -111,9 +140,7 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
|||
},
|
||||
);
|
||||
|
||||
if (confirm == true) {
|
||||
await _unlikeUser(userId);
|
||||
}
|
||||
return confirm;
|
||||
}
|
||||
|
||||
void _showUserInfo(DocumentSnapshot user) {
|
||||
|
@ -142,39 +169,65 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
|||
);
|
||||
}
|
||||
|
||||
Future<List<DocumentSnapshot>> _fetchSortedLikedUsers() async {
|
||||
List<DocumentSnapshot> likedUsers = await _fetchLikedUsers();
|
||||
List<DocumentSnapshot> likedOnlyUsers = [];
|
||||
List<DocumentSnapshot> matchedUsers = [];
|
||||
List<MapEntry<Swipe, DocumentSnapshot>> _getSortedLikedUsersWithSwipes() {
|
||||
List<MapEntry<Swipe, DocumentSnapshot>> likedOnlyUsers = [];
|
||||
List<MapEntry<Swipe, 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) {
|
||||
matchedUsers.add(user);
|
||||
matchedUsers.add(entry);
|
||||
} else {
|
||||
likedOnlyUsers.add(user);
|
||||
likedOnlyUsers.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
if (_sortPreference == MenuSort.nameAsc) {
|
||||
likedOnlyUsers.sort((a, b) => (a[Constants.dbFieldUsersName] as String)
|
||||
.compareTo(b[Constants.dbFieldUsersName] as String));
|
||||
matchedUsers.sort((a, b) => (a[Constants.dbFieldUsersName] as String)
|
||||
.compareTo(b[Constants.dbFieldUsersName] as String));
|
||||
if (_sortPreference == MenuSort.timestampAsc) {
|
||||
likedOnlyUsers.sort((a, b) => a.key.timestamp.compareTo(b.key.timestamp));
|
||||
matchedUsers.sort((a, b) => a.key.timestamp.compareTo(b.key.timestamp));
|
||||
} else if (_sortPreference == MenuSort.timestampDesc) {
|
||||
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) {
|
||||
likedOnlyUsers.sort((a, b) => (b[Constants.dbFieldUsersName] as String)
|
||||
.compareTo(a[Constants.dbFieldUsersName] as String));
|
||||
matchedUsers.sort((a, b) => (b[Constants.dbFieldUsersName] as String)
|
||||
.compareTo(a[Constants.dbFieldUsersName] as String));
|
||||
likedOnlyUsers.sort((a, b) => _compareNames(b, a));
|
||||
matchedUsers.sort((a, b) => _compareNames(b, a));
|
||||
}
|
||||
|
||||
if (_orderPreference == ViewOrder.swipedFirst) {
|
||||
return [...likedOnlyUsers, ...matchedUsers];
|
||||
} else {
|
||||
if (_orderPreference == ViewOrder.matchedFirst) {
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
|
@ -193,10 +246,20 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
|||
items: const [
|
||||
DropdownMenuItem(
|
||||
value: ViewOrder.swipedFirst,
|
||||
child: Text('Swiped First')),
|
||||
child: Text('Swipes First'),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
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) {
|
||||
// update UI on change only
|
||||
|
@ -239,67 +302,83 @@ class LikedUsersPageState extends State<LikedUsersPage> {
|
|||
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() {
|
||||
var sortedSwipeUsers = _getSortedLikedUsersWithSwipes();
|
||||
|
||||
return Expanded(
|
||||
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,
|
||||
child: ListView.builder(
|
||||
itemCount: sortedSwipeUsers.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;
|
||||
var entry = sortedSwipeUsers[index];
|
||||
var user = entry.value;
|
||||
bool hasMatch = _cachedMatches.containsKey(user.id)
|
||||
? _cachedMatches[user.id]!
|
||||
: false;
|
||||
|
||||
return UserTileLikes(
|
||||
user: user,
|
||||
hasMatch: hasMatch,
|
||||
onUnlike: () {
|
||||
onUnlike: () async {
|
||||
Map<String, dynamic> userMap =
|
||||
user.data() as Map<String, dynamic>;
|
||||
bool hasName =
|
||||
userMap.containsKey(Constants.dbFieldUsersName);
|
||||
bool hasName = userMap.containsKey(Constants.dbFieldUsersName);
|
||||
|
||||
_showConfirmationDialog(
|
||||
bool? confirm = await _showConfirmationDialog(
|
||||
user.id,
|
||||
(hasName
|
||||
? user[Constants.dbFieldUsersName]
|
||||
: 'Name: n/a'),
|
||||
(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