Skip to content

Commit cd8baf4

Browse files
authored
Merge pull request #522 from DennisBauer/fix_upcoming_payments_bug
Fix a bug where upcoming expenses were only listed once each month
2 parents 08ae76e + 43cbfef commit cd8baf4

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

app/src/commonMain/kotlin/ui/upcomingexpenses/UpcomingPaymentsScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private fun UpcomingPaymentsOverview(
120120
items(
121121
items = upcomingPaymentsData,
122122
key = { entry ->
123-
"expense_${entry.month}_${entry.payment?.id}"
123+
"expense_${entry.month}_${entry.payment?.id}_${entry.payment?.nextPaymentDate}"
124124
},
125125
span = { entry ->
126126
if (entry.payment == null) {

app/src/commonMain/kotlin/viewmodel/UpcomingPaymentsViewModel.kt

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
1515
import kotlinx.coroutines.withContext
1616
import kotlinx.datetime.Clock
1717
import kotlinx.datetime.DatePeriod
18+
import kotlinx.datetime.DateTimeUnit
1819
import kotlinx.datetime.LocalDate
1920
import kotlinx.datetime.TimeZone
2021
import kotlinx.datetime.atStartOfDayIn
@@ -79,33 +80,40 @@ class UpcomingPaymentsViewModel(
7980
var paymentsSumThisMonth = 0f
8081
var atLeastOneWasExchanged = false
8182
recurringExpenses.forEach { expense ->
82-
expense.getNextPaymentDayAfter(yearMonth)?.let { nextPaymentDay ->
83-
if (nextPaymentDay.isSameMonth(yearMonth) && nextPaymentDay > now) {
84-
val nextPaymentRemainingDays = nextPaymentDay.getNextPaymentDays()
85-
val nextPaymentDate = nextPaymentDay.atStartOfDayIn(TimeZone.UTC).toLocaleString()
86-
val currencyValue =
87-
CurrencyValue(
88-
expense.price!!,
89-
expense.currencyCode.ifBlank { defaultCurrency.getDefaultCurrencyCode() },
90-
)
91-
paymentsThisMonth.add(
92-
UpcomingPaymentData(
93-
id = expense.id,
94-
name = expense.name!!,
95-
price = currencyValue,
96-
nextPaymentRemainingDays = nextPaymentRemainingDays,
97-
nextPaymentDate = nextPaymentDate,
98-
color = ExpenseColor.fromInt(expense.color),
99-
),
83+
var nextPaymentDay = expense.getNextPaymentDayAfter(yearMonth) ?: return@forEach
84+
while (nextPaymentDay < now) {
85+
nextPaymentDay =
86+
expense.getNextPaymentDayAfter(nextPaymentDay.plus(1, DateTimeUnit.DAY))
87+
?: return@forEach
88+
}
89+
while (nextPaymentDay.isSameMonth(yearMonth)) {
90+
val nextPaymentRemainingDays = nextPaymentDay.getNextPaymentDays()
91+
val nextPaymentDate = nextPaymentDay.atStartOfDayIn(TimeZone.UTC).toLocaleString()
92+
val currencyValue =
93+
CurrencyValue(
94+
expense.price!!,
95+
expense.currencyCode.ifBlank { defaultCurrency.getDefaultCurrencyCode() },
10096
)
97+
paymentsThisMonth.add(
98+
UpcomingPaymentData(
99+
id = expense.id,
100+
name = expense.name!!,
101+
price = currencyValue,
102+
nextPaymentRemainingDays = nextPaymentRemainingDays,
103+
nextPaymentDate = nextPaymentDate,
104+
color = ExpenseColor.fromInt(expense.color),
105+
),
106+
)
101107

102-
paymentsSumThisMonth += currencyValue.exchangeToDefaultCurrency()?.value ?: 0f
103-
val currencyCode =
104-
expense.currencyCode.ifBlank { defaultCurrency.getDefaultCurrencyCode() }
105-
if (currencyCode != defaultCurrency.getDefaultCurrencyCode()) {
106-
atLeastOneWasExchanged = true
107-
}
108+
paymentsSumThisMonth += currencyValue.exchangeToDefaultCurrency()?.value ?: 0f
109+
val currencyCode =
110+
expense.currencyCode.ifBlank { defaultCurrency.getDefaultCurrencyCode() }
111+
if (currencyCode != defaultCurrency.getDefaultCurrencyCode()) {
112+
atLeastOneWasExchanged = true
108113
}
114+
nextPaymentDay =
115+
expense.getNextPaymentDayAfter(nextPaymentDay.plus(1, DateTimeUnit.DAY))
116+
?: return@forEach
109117
}
110118
}
111119
if (paymentsThisMonth.isNotEmpty()) {

0 commit comments

Comments
 (0)