Skip to content

Commit

Permalink
feature #818 Use new results.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastkidjp committed Apr 23, 2023
1 parent f7235d7 commit 8f4382b
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions loan/src/main/java/jp/toastkid/loan/view/LoanCalculatorUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@

package jp.toastkid.loan.view

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
Expand All @@ -22,10 +29,12 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
Expand All @@ -35,14 +44,16 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import jp.toastkid.loan.R
import jp.toastkid.loan.model.Factor
import jp.toastkid.loan.model.PaymentDetail
import jp.toastkid.loan.usecase.DebouncedCalculatorUseCase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import java.text.DecimalFormat
import kotlin.math.roundToInt

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun LoanCalculatorUi() {
val context = LocalContext.current
Expand All @@ -68,6 +79,8 @@ fun LoanCalculatorUi() {
mutableStateOf("10,000")
}

val scheduleState = remember { mutableStateListOf<PaymentDetail>() }

val inputChannel: Channel<String> = Channel()

DebouncedCalculatorUseCase(
Expand All @@ -83,7 +96,9 @@ fun LoanCalculatorUi() {
)
},
{
result = context.getString(R.string.message_result_montly_payment, it)
result = context.getString(R.string.message_result_montly_payment, it.monthlyPayment, it.paymentSchedule.map { it.interest }.sum().toLong())
scheduleState.clear()
scheduleState.addAll(it.paymentSchedule)
}
).invoke()

Expand Down Expand Up @@ -190,6 +205,40 @@ fun LoanCalculatorUi() {
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier.fillMaxWidth()
)

if (scheduleState.isNotEmpty()) {
Box (modifier = Modifier.weight(0.5f)) {
val scrollState = rememberLazyListState()
LazyColumn(state = scrollState) {
stickyHeader {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.animateItemPlacement()
.background(if (scrollState.firstVisibleItemIndex != 0) MaterialTheme.colorScheme.surface else Color.Transparent)
) {
Text(
"回数",
modifier = Modifier.weight(0.4f)
)
Text("元本", modifier = Modifier.weight(1f))
Text("利息", modifier = Modifier.weight(1f))
Text("残金", modifier = Modifier.weight(1f))
}
}
itemsIndexed(scheduleState) { index, it ->
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.animateItemPlacement()) {
Text(
"${(index / 12) + 1} ${(index % 12) + 1}(${index + 1})",
modifier = Modifier.weight(0.4f)
)
Text(it.principal.roundToInt().toString(), modifier = Modifier.weight(1f))
Text(it.interest.roundToInt().toString(), modifier = Modifier.weight(1f))
Text(it.amount.toString(), modifier = Modifier.weight(1f))
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit 8f4382b

Please sign in to comment.