Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/Components/quick_send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,41 @@ class _QuickSendRowState extends State<QuickSendRow> {

Future<List<String>> fetchCategoriesSortedByUsage() async {
final user = FirebaseAuth.instance.currentUser;
if (user == null) {
return [];
}
if (user == null) return [];

final txRef = FirebaseFirestore.instance
.collection('users')
.doc(user.email) // or user.uid
.doc(user.email)
.collection('transactions');

try {
final snapshot = await txRef.get();

Map<String, int> categoryCounts = {};

for (var doc in snapshot.docs) {
final category = doc['category'] as String?;
final data = doc.data();

// Safe category reading
final category = data.containsKey('category')
? data['category'] as String?
: null;

if (category != null && category.isNotEmpty) {
categoryCounts[category] = (categoryCounts[category] ?? 0) + 1;
}
}

final sortedCategories = categoryCounts.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));

return sortedCategories.map((e) => e.key).toList();
} catch (e) {
debugPrint('Error fetching categories sorted by usage: $e');
return [];
}
}


Future<void> _loadCategoriesSortedByUsage() async {
setState(() {
loading = true;
Expand Down
Loading