This commit is contained in:
2026-03-24 15:24:59 +03:00
parent 5888e0d196
commit bb4580ec49
4 changed files with 51 additions and 56 deletions
+16 -4
View File
@@ -135,7 +135,10 @@ final accountFilteredTransactionsProvider = Provider<List<Transaction>>((ref) {
final totalBalanceProvider = Provider<double>((ref) { final totalBalanceProvider = Provider<double>((ref) {
final txs = ref.watch(accountFilteredTransactionsProvider); final txs = ref.watch(accountFilteredTransactionsProvider);
final exchangeService = ref.watch(exchangeRateServiceProvider); 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) { return txs.fold(0.0, (sum, t) {
final converted = exchangeService.convert( final converted = exchangeService.convert(
@@ -151,7 +154,10 @@ final totalIncomeProvider = Provider<double>((ref) {
final txs = ref.watch(accountFilteredTransactionsProvider); final txs = ref.watch(accountFilteredTransactionsProvider);
final filtered = txs.where((t) => t.type == TransactionType.income); final filtered = txs.where((t) => t.type == TransactionType.income);
final exchangeService = ref.watch(exchangeRateServiceProvider); 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 filtered.fold(0.0, (sum, t) {
return sum + return sum +
@@ -163,7 +169,10 @@ final totalExpenseProvider = Provider<double>((ref) {
final txs = ref.watch(accountFilteredTransactionsProvider); final txs = ref.watch(accountFilteredTransactionsProvider);
final filtered = txs.where((t) => t.type == TransactionType.expense); final filtered = txs.where((t) => t.type == TransactionType.expense);
final exchangeService = ref.watch(exchangeRateServiceProvider); 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 filtered.fold(0.0, (sum, t) {
return sum + return sum +
@@ -181,7 +190,10 @@ final currentMonthExpenseProvider = Provider<double>((ref) {
t.date.month == now.month, t.date.month == now.month,
); );
final exchangeService = ref.watch(exchangeRateServiceProvider); 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 filtered.fold(0.0, (sum, t) {
return sum + return sum +
+8 -35
View File
@@ -139,40 +139,6 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
.read(accountCardColorsProvider(editingAccount!.id).notifier) .read(accountCardColorsProvider(editingAccount!.id).notifier)
.save(tempPrimary, tempSecondary, tempGradientType); .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 // Update account name and currency
final updatedAccount = Account( final updatedAccount = Account(
id: editingAccount!.id, id: editingAccount!.id,
@@ -242,7 +208,14 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
final monthExpense = ref.watch(currentMonthExpenseProvider); final monthExpense = ref.watch(currentMonthExpenseProvider);
final budget = ref.watch(budgetProvider); final budget = ref.watch(budgetProvider);
final recent = ref.watch(recentTransactionsProvider); 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( return Scaffold(
backgroundColor: Theme.of(context).scaffoldBackgroundColor, backgroundColor: Theme.of(context).scaffoldBackgroundColor,
@@ -33,7 +33,6 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
dynamic get dash => widget.dashboardState; dynamic get dash => widget.dashboardState;
late TextEditingController _nameController; late TextEditingController _nameController;
late String _selectedCurrency; late String _selectedCurrency;
late String _originalCurrency;
bool _showCurrencyDropdown = false; bool _showCurrencyDropdown = false;
bool _showLimitError = false; bool _showLimitError = false;
@@ -42,7 +41,6 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
super.initState(); super.initState();
_nameController = TextEditingController(text: dash.tempAccountName); _nameController = TextEditingController(text: dash.tempAccountName);
_selectedCurrency = dash.tempAccountCurrency; _selectedCurrency = dash.tempAccountCurrency;
_originalCurrency = dash.tempAccountCurrency;
_nameController.addListener(() { _nameController.addListener(() {
final text = _nameController.text; final text = _nameController.text;
@@ -91,25 +89,24 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
builder: (context, ref, _) { builder: (context, ref, _) {
final exchangeService = ref.watch(exchangeRateServiceProvider); final exchangeService = ref.watch(exchangeRateServiceProvider);
// Get the original balance from the editing account // Calculate preview balance fresh from raw transactions
double originalBalance = ref.read(totalBalanceProvider); double previewBalance = 0.0;
if (dash.editingAccount != null) { if (dash.editingAccount != null) {
// Get the account's actual balance
final txs = ref.watch(accountFilteredTransactionsProvider); final txs = ref.watch(accountFilteredTransactionsProvider);
final accountTxs = txs.where((t) => t.accountId == dash.editingAccount!.id); final accountTxs = txs.where((t) => t.accountId == dash.editingAccount!.id);
originalBalance = accountTxs.fold<double>( previewBalance = accountTxs.fold(0.0, (sum, t) {
0.0, final converted = exchangeService.convert(
(sum, t) => sum + (t.type == TransactionType.income ? t.amount : -t.amount), 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( return Material(
color: Colors.transparent, color: Colors.transparent,
child: Stack( child: Stack(
@@ -92,8 +92,21 @@ class _BalanceCardCarouselState extends ConsumerState<BalanceCardCarousel> {
final account = accounts[index - 1]; final account = accounts[index - 1];
final accountColors = ref.watch(accountCardColorsProvider(account.id)); 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( cardWidget = BalanceCard(
balance: widget.balance, balance: accountBalance, // Use the dynamically calculated balance!
currencyInfo: CurrencyInfo( currencyInfo: CurrencyInfo(
currencyMap[account.currency]?.symbol ?? '\$', currencyMap[account.currency]?.symbol ?? '\$',
account.currency, account.currency,