mirror of
https://github.com/koloideal/Casha.git
synced 2026-06-10 10:25:28 +03:00
update
This commit is contained in:
@@ -135,7 +135,10 @@ final accountFilteredTransactionsProvider = Provider<List<Transaction>>((ref) {
|
||||
final totalBalanceProvider = Provider<double>((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<double>((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<double>((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<double>((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 +
|
||||
|
||||
@@ -139,40 +139,6 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
|
||||
.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<DashboardScreen> {
|
||||
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,
|
||||
|
||||
@@ -33,7 +33,6 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
|
||||
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<AccountEditorOverlay> {
|
||||
super.initState();
|
||||
_nameController = TextEditingController(text: dash.tempAccountName);
|
||||
_selectedCurrency = dash.tempAccountCurrency;
|
||||
_originalCurrency = dash.tempAccountCurrency;
|
||||
_nameController.addListener(() {
|
||||
final text = _nameController.text;
|
||||
|
||||
@@ -91,25 +89,24 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
|
||||
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<double>(
|
||||
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,
|
||||
child: Stack(
|
||||
|
||||
@@ -92,8 +92,21 @@ class _BalanceCardCarouselState extends ConsumerState<BalanceCardCarousel> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user