Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class _CategorySelectorState extends ConsumerState<CategorySelector> {
final categoriesList = ref.watch(
categoriesByTypeProvider(transactionType.categoryType),
);
final frequentCategories = ref.watch(
frequentCategoriesProvider(transactionType.categoryType),
);
final selectedCategory = ref.watch(selectedCategoryProvider);

return Container(
Expand Down Expand Up @@ -71,7 +74,7 @@ class _CategorySelectorState extends ConsumerState<CategorySelector> {
color: Theme.of(context).colorScheme.surface,
height: 74,
width: double.infinity,
child: categoriesList.when(
child: frequentCategories.when(
data: (categories) => ListView.builder(
itemCount: categories.length, // to prevent range error
scrollDirection: Axis.horizontal,
Expand Down
14 changes: 14 additions & 0 deletions lib/providers/categories_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ Future<List<CategoryTransaction>> subcategories(Ref ref, int categoryId) async {
return categories;
}

@riverpod
Future<List<CategoryTransaction>> frequentCategories(
Ref ref,
CategoryTransactionType? type,
) async {
List<CategoryTransaction> categories = [];
if (type != null) {
categories = await ref
.read(categoryRepositoryProvider)
.selectFrequentCategories(type);
}
return categories;
}

@Riverpod(keepAlive: true)
Future<Map<CategoryTransaction, double>> categoryMap(Ref ref) async {
final categoryType = ref.watch(categoryTypeProvider);
Expand Down
28 changes: 28 additions & 0 deletions lib/services/database/repositories/category_repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../../model/category_transaction.dart';
import '../../../model/transaction.dart';
import '../sossoldi_database.dart';

part 'category_repository.g.dart';
Expand Down Expand Up @@ -112,6 +113,33 @@ class CategoryRepository {
}
}

Future<List<CategoryTransaction>> selectFrequentCategories(
CategoryTransactionType type,
) async {
final db = await _sossoldiDB.database;
// Select the last 100 transactions, group by category and return the
// top 5 most used categories ordered by usage count desc.
final result = await db.rawQuery(
'''
SELECT c.*
FROM "$categoryTransactionTable" c
JOIN (
SELECT * FROM "$transactionTable"
WHERE "${TransactionFields.type}" = ?
ORDER BY "${TransactionFields.date}" DESC
LIMIT 100
) t ON t."${TransactionFields.idCategory}" = c."${CategoryTransactionFields.id}"
WHERE c."${CategoryTransactionFields.type}" = ?
GROUP BY c."${CategoryTransactionFields.id}"
ORDER BY COUNT(t."${TransactionFields.id}") DESC
LIMIT 5
''',
[type.transactionType.code, type.code],
);

return result.map((json) => CategoryTransaction.fromJson(json)).toList();
}

Future<int> updateItem(CategoryTransaction item) async {
final db = await _sossoldiDB.database;

Expand Down