diff --git a/lib/features/dashboard/provider.dart b/lib/features/dashboard/provider.dart index e82bac1..c660a87 100644 --- a/lib/features/dashboard/provider.dart +++ b/lib/features/dashboard/provider.dart @@ -135,7 +135,10 @@ final accountFilteredTransactionsProvider = Provider>((ref) { final totalBalanceProvider = Provider((ref) { final txs = ref.watch(accountFilteredTransactionsProvider); final exchangeService = ref.watch(exchangeRateServiceProvider); - final targetCurrency = ref.watch(currencyProvider).code; + final activeAccount = ref.watch(activeAccountProvider); + final targetCurrency = activeAccount != null + ? activeAccount.currency + : ref.watch(currencyProvider).code; return txs.fold(0.0, (sum, t) { final converted = exchangeService.convert( @@ -151,7 +154,10 @@ final totalIncomeProvider = Provider((ref) { final txs = ref.watch(accountFilteredTransactionsProvider); final filtered = txs.where((t) => t.type == TransactionType.income); final exchangeService = ref.watch(exchangeRateServiceProvider); - final targetCurrency = ref.watch(currencyProvider).code; + final activeAccount = ref.watch(activeAccountProvider); + final targetCurrency = activeAccount != null + ? activeAccount.currency + : ref.watch(currencyProvider).code; return filtered.fold(0.0, (sum, t) { return sum + @@ -163,7 +169,10 @@ final totalExpenseProvider = Provider((ref) { final txs = ref.watch(accountFilteredTransactionsProvider); final filtered = txs.where((t) => t.type == TransactionType.expense); final exchangeService = ref.watch(exchangeRateServiceProvider); - final targetCurrency = ref.watch(currencyProvider).code; + final activeAccount = ref.watch(activeAccountProvider); + final targetCurrency = activeAccount != null + ? activeAccount.currency + : ref.watch(currencyProvider).code; return filtered.fold(0.0, (sum, t) { return sum + @@ -181,7 +190,10 @@ final currentMonthExpenseProvider = Provider((ref) { t.date.month == now.month, ); final exchangeService = ref.watch(exchangeRateServiceProvider); - final targetCurrency = ref.watch(currencyProvider).code; + final activeAccount = ref.watch(activeAccountProvider); + final targetCurrency = activeAccount != null + ? activeAccount.currency + : ref.watch(currencyProvider).code; return filtered.fold(0.0, (sum, t) { return sum + diff --git a/lib/features/dashboard/screen.dart b/lib/features/dashboard/screen.dart index 542746c..e27e5da 100644 --- a/lib/features/dashboard/screen.dart +++ b/lib/features/dashboard/screen.dart @@ -139,40 +139,6 @@ class _DashboardScreenState extends ConsumerState { .read(accountCardColorsProvider(editingAccount!.id).notifier) .save(tempPrimary, tempSecondary, tempGradientType); - // Check if currency was changed and convert transactions - if (tempAccountCurrency != editingAccount!.currency) { - final exchangeService = ref.read(exchangeRateServiceProvider); - final txRepo = ref.read(transactionRepositoryProvider); - - // Fetch all transactions - final allTxsResult = await txRepo.getAll(); - if (allTxsResult.isSuccess) { - final accountTxs = allTxsResult.dataOrNull! - .where((t) => t.accountId == editingAccount!.id) - .toList(); - - // Convert and update each transaction - for (final tx in accountTxs) { - final convertedAmount = exchangeService.convert( - tx.amount, - editingAccount!.currency, // old currency - tempAccountCurrency, // new currency - ); - - final updatedTx = tx.copyWith( - amount: convertedAmount, - currency: currencyMap[tempAccountCurrency]?.symbol ?? '\$', - currencyCode: tempAccountCurrency, - ); - - await txRepo.update(updatedTx); - } - - // Refresh transactions provider so the UI updates - ref.read(transactionsProvider.notifier).refresh(); - } - } - // Update account name and currency final updatedAccount = Account( id: editingAccount!.id, @@ -242,7 +208,14 @@ class _DashboardScreenState extends ConsumerState { final monthExpense = ref.watch(currentMonthExpenseProvider); final budget = ref.watch(budgetProvider); final recent = ref.watch(recentTransactionsProvider); - final currencyInfo = ref.watch(currencyProvider); + final activeAccount = ref.watch(activeAccountProvider); + final globalCurrencyInfo = ref.watch(currencyProvider); + final currencyInfo = activeAccount != null + ? CurrencyInfo( + currencyMap[activeAccount.currency]?.symbol ?? '\$', + activeAccount.currency, + ) + : globalCurrencyInfo; return Scaffold( backgroundColor: Theme.of(context).scaffoldBackgroundColor, diff --git a/lib/features/dashboard/widgets/account_editor_overlay.dart b/lib/features/dashboard/widgets/account_editor_overlay.dart index f8c9312..ffa05d0 100644 --- a/lib/features/dashboard/widgets/account_editor_overlay.dart +++ b/lib/features/dashboard/widgets/account_editor_overlay.dart @@ -33,7 +33,6 @@ class _AccountEditorOverlayState extends State { dynamic get dash => widget.dashboardState; late TextEditingController _nameController; late String _selectedCurrency; - late String _originalCurrency; bool _showCurrencyDropdown = false; bool _showLimitError = false; @@ -42,7 +41,6 @@ class _AccountEditorOverlayState extends State { super.initState(); _nameController = TextEditingController(text: dash.tempAccountName); _selectedCurrency = dash.tempAccountCurrency; - _originalCurrency = dash.tempAccountCurrency; _nameController.addListener(() { final text = _nameController.text; @@ -91,24 +89,23 @@ class _AccountEditorOverlayState extends State { builder: (context, ref, _) { final exchangeService = ref.watch(exchangeRateServiceProvider); - // Get the original balance from the editing account - double originalBalance = ref.read(totalBalanceProvider); + // Calculate preview balance fresh from raw transactions + double previewBalance = 0.0; if (dash.editingAccount != null) { - // Get the account's actual balance final txs = ref.watch(accountFilteredTransactionsProvider); final accountTxs = txs.where((t) => t.accountId == dash.editingAccount!.id); - originalBalance = accountTxs.fold( - 0.0, - (sum, t) => sum + (t.type == TransactionType.income ? t.amount : -t.amount), - ); + previewBalance = accountTxs.fold(0.0, (sum, t) { + final converted = exchangeService.convert( + t.amount, + t.currencyCode, + dash.tempAccountCurrency, // convert directly from tx currency to selected dropdown currency + ); + return t.type == TransactionType.income ? sum + converted : sum - converted; + }); + } else { + // Fallback just in case, though editingAccount should never be null here + previewBalance = ref.read(totalBalanceProvider); } - - // Convert to the preview currency - final previewBalance = exchangeService.convert( - originalBalance, - _originalCurrency, - dash.tempAccountCurrency, - ); return Material( color: Colors.transparent, diff --git a/lib/features/dashboard/widgets/balance_card_carousel.dart b/lib/features/dashboard/widgets/balance_card_carousel.dart index 2a3fc09..28fa051 100644 --- a/lib/features/dashboard/widgets/balance_card_carousel.dart +++ b/lib/features/dashboard/widgets/balance_card_carousel.dart @@ -92,8 +92,21 @@ class _BalanceCardCarouselState extends ConsumerState { final account = accounts[index - 1]; final accountColors = ref.watch(accountCardColorsProvider(account.id)); + // Calculate this specific account's balance + final txs = ref.watch(transactionsProvider).valueOrNull ?? []; + final accountTxs = txs.where((t) => t.accountId == account.id).toList(); + final exchangeService = ref.watch(exchangeRateServiceProvider); + final accountBalance = accountTxs.fold(0.0, (sum, t) { + final converted = exchangeService.convert( + t.amount, + t.currencyCode, + account.currency, // target is the account's own currency + ); + return t.type == TransactionType.income ? sum + converted : sum - converted; + }); + cardWidget = BalanceCard( - balance: widget.balance, + balance: accountBalance, // Use the dynamically calculated balance! currencyInfo: CurrencyInfo( currencyMap[account.currency]?.symbol ?? '\$', account.currency,