Skip to content

Commit 8caa335

Browse files
feat: frequent category list real data (#513)
1 parent e75d7cf commit 8caa335

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

lib/pages/transactions/create_transaction/widgets/category_selector.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class _CategorySelectorState extends ConsumerState<CategorySelector> {
3030
final categoriesList = ref.watch(
3131
categoriesByTypeProvider(transactionType.categoryType),
3232
);
33+
final frequentCategories = ref.watch(
34+
frequentCategoriesProvider(transactionType.categoryType),
35+
);
3336
final selectedCategory = ref.watch(selectedCategoryProvider);
3437

3538
return Container(
@@ -71,7 +74,7 @@ class _CategorySelectorState extends ConsumerState<CategorySelector> {
7174
color: Theme.of(context).colorScheme.surface,
7275
height: 74,
7376
width: double.infinity,
74-
child: categoriesList.when(
77+
child: frequentCategories.when(
7578
data: (categories) => ListView.builder(
7679
itemCount: categories.length, // to prevent range error
7780
scrollDirection: Axis.horizontal,

lib/providers/categories_provider.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ Future<List<CategoryTransaction>> subcategories(Ref ref, int categoryId) async {
208208
return categories;
209209
}
210210

211+
@riverpod
212+
Future<List<CategoryTransaction>> frequentCategories(
213+
Ref ref,
214+
CategoryTransactionType? type,
215+
) async {
216+
List<CategoryTransaction> categories = [];
217+
if (type != null) {
218+
categories = await ref
219+
.read(categoryRepositoryProvider)
220+
.selectFrequentCategories(type);
221+
}
222+
return categories;
223+
}
224+
211225
@Riverpod(keepAlive: true)
212226
Future<Map<CategoryTransaction, double>> categoryMap(Ref ref) async {
213227
final categoryType = ref.watch(categoryTypeProvider);

lib/services/database/repositories/category_repository.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:riverpod_annotation/riverpod_annotation.dart';
22

33
import '../../../model/category_transaction.dart';
4+
import '../../../model/transaction.dart';
45
import '../sossoldi_database.dart';
56

67
part 'category_repository.g.dart';
@@ -112,6 +113,33 @@ class CategoryRepository {
112113
}
113114
}
114115

116+
Future<List<CategoryTransaction>> selectFrequentCategories(
117+
CategoryTransactionType type,
118+
) async {
119+
final db = await _sossoldiDB.database;
120+
// Select the last 100 transactions, group by category and return the
121+
// top 5 most used categories ordered by usage count desc.
122+
final result = await db.rawQuery(
123+
'''
124+
SELECT c.*
125+
FROM "$categoryTransactionTable" c
126+
JOIN (
127+
SELECT * FROM "$transactionTable"
128+
WHERE "${TransactionFields.type}" = ?
129+
ORDER BY "${TransactionFields.date}" DESC
130+
LIMIT 100
131+
) t ON t."${TransactionFields.idCategory}" = c."${CategoryTransactionFields.id}"
132+
WHERE c."${CategoryTransactionFields.type}" = ?
133+
GROUP BY c."${CategoryTransactionFields.id}"
134+
ORDER BY COUNT(t."${TransactionFields.id}") DESC
135+
LIMIT 5
136+
''',
137+
[type.transactionType.code, type.code],
138+
);
139+
140+
return result.map((json) => CategoryTransaction.fromJson(json)).toList();
141+
}
142+
115143
Future<int> updateItem(CategoryTransaction item) async {
116144
final db = await _sossoldiDB.database;
117145

0 commit comments

Comments
 (0)