@@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
15
15
import kotlinx.coroutines.withContext
16
16
import kotlinx.datetime.Clock
17
17
import kotlinx.datetime.DatePeriod
18
+ import kotlinx.datetime.DateTimeUnit
18
19
import kotlinx.datetime.LocalDate
19
20
import kotlinx.datetime.TimeZone
20
21
import kotlinx.datetime.atStartOfDayIn
@@ -79,33 +80,40 @@ class UpcomingPaymentsViewModel(
79
80
var paymentsSumThisMonth = 0f
80
81
var atLeastOneWasExchanged = false
81
82
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() },
100
96
)
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
+ )
101
107
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
108
113
}
114
+ nextPaymentDay =
115
+ expense.getNextPaymentDayAfter(nextPaymentDay.plus(1 , DateTimeUnit .DAY ))
116
+ ? : return @forEach
109
117
}
110
118
}
111
119
if (paymentsThisMonth.isNotEmpty()) {
0 commit comments