diff --git a/lib/features/dashboard/provider.dart b/lib/features/dashboard/provider.dart index db5b04d..ba79278 100644 --- a/lib/features/dashboard/provider.dart +++ b/lib/features/dashboard/provider.dart @@ -122,23 +122,38 @@ final accountFilteredTransactionsProvider = Provider>((ref) { final txsAsync = ref.watch(transactionsProvider); final txs = txsAsync.valueOrNull ?? []; final activeAccount = ref.watch(activeAccountProvider); - + // If activeAccount is null (Total Balance page), return all transactions if (activeAccount == null) { return txs; } - + // Filter by account ID return txs.where((t) => t.accountId == activeAccount.id).toList(); }); +final globalTotalBalanceProvider = Provider((ref) { + final txs = ref.watch(transactionsProvider).valueOrNull ?? []; + final exchangeService = ref.watch(exchangeRateServiceProvider); + final targetCurrency = ref.watch(currencyProvider).code; + + return txs.fold(0.0, (sum, t) { + final converted = exchangeService.convert( + t.amount, + t.currencyCode, + targetCurrency, + ); + return t.type == TransactionType.income ? sum + converted : sum - converted; + }); +}); + final totalBalanceProvider = Provider((ref) { final txs = ref.watch(accountFilteredTransactionsProvider); - + final index = ref.watch(activeAccountIndexProvider); final accountsAsync = ref.watch(accountsProvider); final globalCurrency = ref.watch(currencyProvider).code; - + String targetCurrency = globalCurrency; if (index > 0) { final accounts = accountsAsync.valueOrNull ?? []; @@ -146,7 +161,7 @@ final totalBalanceProvider = Provider((ref) { targetCurrency = accounts[index - 1].currency; } } - + final exchangeService = ref.watch(exchangeRateServiceProvider); return txs.fold(0.0, (sum, t) { @@ -163,12 +178,12 @@ final totalIncomeProvider = Provider((ref) { // Watch the filtered transactions directly final txs = ref.watch(accountFilteredTransactionsProvider); final filtered = txs.where((t) => t.type == TransactionType.income); - + // Watch the dependencies that change on swipe! final index = ref.watch(activeAccountIndexProvider); final accountsAsync = ref.watch(accountsProvider); final globalCurrency = ref.watch(currencyProvider).code; - + // Resolve target currency synchronously based on the current swipe index String targetCurrency = globalCurrency; if (index > 0) { @@ -177,22 +192,23 @@ final totalIncomeProvider = Provider((ref) { targetCurrency = accounts[index - 1].currency; } } - + final exchangeService = ref.watch(exchangeRateServiceProvider); - + return filtered.fold(0.0, (sum, t) { - return sum + exchangeService.convert(t.amount, t.currencyCode, targetCurrency); + return sum + + exchangeService.convert(t.amount, t.currencyCode, targetCurrency); }); }); final totalExpenseProvider = Provider((ref) { final txs = ref.watch(accountFilteredTransactionsProvider); final filtered = txs.where((t) => t.type == TransactionType.expense); - + final index = ref.watch(activeAccountIndexProvider); final accountsAsync = ref.watch(accountsProvider); final globalCurrency = ref.watch(currencyProvider).code; - + String targetCurrency = globalCurrency; if (index > 0) { final accounts = accountsAsync.valueOrNull ?? []; @@ -200,11 +216,12 @@ final totalExpenseProvider = Provider((ref) { targetCurrency = accounts[index - 1].currency; } } - + final exchangeService = ref.watch(exchangeRateServiceProvider); - + return filtered.fold(0.0, (sum, t) { - return sum + exchangeService.convert(t.amount, t.currencyCode, targetCurrency); + return sum + + exchangeService.convert(t.amount, t.currencyCode, targetCurrency); }); }); @@ -217,11 +234,11 @@ final currentMonthExpenseProvider = Provider((ref) { t.date.year == now.year && t.date.month == now.month, ); - + final index = ref.watch(activeAccountIndexProvider); final accountsAsync = ref.watch(accountsProvider); final globalCurrency = ref.watch(currencyProvider).code; - + String targetCurrency = globalCurrency; if (index > 0) { final accounts = accountsAsync.valueOrNull ?? []; @@ -229,11 +246,12 @@ final currentMonthExpenseProvider = Provider((ref) { targetCurrency = accounts[index - 1].currency; } } - + final exchangeService = ref.watch(exchangeRateServiceProvider); - + return filtered.fold(0.0, (sum, t) { - return sum + exchangeService.convert(t.amount, t.currencyCode, targetCurrency); + return sum + + exchangeService.convert(t.amount, t.currencyCode, targetCurrency); }); }); @@ -298,9 +316,9 @@ final activeAccountIndexProvider = StateProvider((ref) => 0); final activeAccountProvider = Provider((ref) { final index = ref.watch(activeAccountIndexProvider); final accountsAsync = ref.watch(accountsProvider); - + if (index == 0) return null; // 0 means "Total Balance" - + return accountsAsync.when( data: (accounts) { if (index > 0 && index <= accounts.length) { @@ -330,7 +348,10 @@ final cardColorsProvider = // Account-specific color provider final accountCardColorsProvider = - StateNotifierProvider.family((ref, accountId) { + StateNotifierProvider.family(( + ref, + accountId, + ) { final notifier = CardColorsNotifier(accountId: accountId); notifier.setupThemeListener(ref); return notifier; @@ -369,7 +390,12 @@ class CardColorsNotifier extends StateNotifier { GradientType gradient, ) async { state = CardColors(primary, secondary, gradient); - await CardColorService.save(primary, secondary, gradient, accountId: accountId); + await CardColorService.save( + primary, + secondary, + gradient, + accountId: accountId, + ); } Future reset(bool isDark) async { diff --git a/lib/features/dashboard/widgets/balance_card_carousel.dart b/lib/features/dashboard/widgets/balance_card_carousel.dart index 7b50549..99683e9 100644 --- a/lib/features/dashboard/widgets/balance_card_carousel.dart +++ b/lib/features/dashboard/widgets/balance_card_carousel.dart @@ -87,9 +87,13 @@ class _BalanceCardCarouselState extends ConsumerState { Widget cardWidget; if (index == 0) { + final totalBalance = ref.watch( + globalTotalBalanceProvider, + ); + final globalCurrency = ref.watch(currencyProvider); cardWidget = BalanceCard( - balance: widget.balance, - currencyInfo: widget.currencyInfo, + balance: totalBalance, + currencyInfo: globalCurrency, onLongPress: widget.onLongPress, previewPrimary: widget.previewPrimary, previewSecondary: widget.previewSecondary,