Skip to content

Commit 17be7af

Browse files
authored
Merge pull request #527 from lucaantonelli/feat-frequent-accounts
feat: frequent accounts list
2 parents a08e61b + dcba010 commit 17be7af

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class _AccountSelectorState extends ConsumerState<AccountSelector> {
2727
@override
2828
Widget build(BuildContext context) {
2929
final accountsList = ref.watch(activeAccountsProvider);
30+
final frequentAccounts = ref.watch(frequentAccountsProvider);
3031
final fromAccount = ref.watch(selectedBankAccountProvider);
3132
final toAccount = ref.watch(bankAccountTransferProvider);
3233

@@ -72,7 +73,7 @@ class _AccountSelectorState extends ConsumerState<AccountSelector> {
7273
color: Theme.of(context).colorScheme.surface,
7374
height: 74,
7475
width: double.infinity,
75-
child: accountsList.when(
76+
child: frequentAccounts.when(
7677
data: (accounts) => ListView.builder(
7778
itemCount: (accounts.length > 4) ? 4 : accounts.length,
7879
scrollDirection: Axis.horizontal,

lib/providers/accounts_provider.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,8 @@ Future<List<BankAccount>> activeAccounts(Ref ref) async {
265265
final accounts = ref.watch(accountsProvider).value ?? [];
266266
return accounts.where((account) => account.deletedAt == null).toList();
267267
}
268+
269+
@riverpod
270+
Future<List<BankAccount>> frequentAccounts(Ref ref) async {
271+
return await ref.read(accountRepositoryProvider).selectFrequentAccounts();
272+
}

lib/services/database/repositories/account_repository.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ class AccountRepository {
103103
return result.map((json) => BankAccount.fromJson(json)).toList();
104104
}
105105

106+
Future<List<BankAccount>> selectFrequentAccounts() async {
107+
final db = await _sossoldiDB.database;
108+
// Select the last 100 transactions, group by account and return the
109+
// top 5 most used accounts ordered by usage count desc.
110+
final result = await db.rawQuery('''
111+
SELECT b.*
112+
FROM "$bankAccountTable" b
113+
JOIN (
114+
SELECT * FROM "$transactionTable"
115+
ORDER BY "${TransactionFields.date}" DESC
116+
LIMIT 100
117+
) t ON t."${TransactionFields.idBankAccount}" = b."${BankAccountFields.id}" OR t."${TransactionFields.idBankAccountTransfer}" = b."${BankAccountFields.id}"
118+
WHERE b."${BankAccountFields.active}" = 1 AND ${BankAccountFields.deletedAt} IS NULL
119+
GROUP BY b."${BankAccountFields.id}"
120+
ORDER BY COUNT(t."${TransactionFields.id}") DESC
121+
LIMIT 5
122+
''');
123+
124+
return result.map((json) => BankAccount.fromJson(json)).toList();
125+
}
126+
106127
Future<int> updateItem(BankAccount item) async {
107128
final db = await _sossoldiDB.database;
108129

0 commit comments

Comments
 (0)