165 lines
6.9 KiB
Kotlin
165 lines
6.9 KiB
Kotlin
package com.financialviewer.ui
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.app.Activity
|
|
import android.content.Intent
|
|
import android.icu.util.Calendar
|
|
import android.os.Bundle
|
|
import android.widget.Button
|
|
import android.widget.EditText
|
|
import android.widget.TextView
|
|
import android.widget.Toast
|
|
import androidx.activity.OnBackPressedCallback
|
|
import androidx.appcompat.app.AlertDialog
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.financialviewer.R
|
|
import com.financialviewer.data.LoanMonthlyDetails
|
|
import com.financialviewer.db.Loan
|
|
import com.financialviewer.repository.LoanRepository
|
|
import com.financialviewer.ui.adapter.LoanDetailsAdapter
|
|
import com.financialviewer.utils.calculateNextInterest
|
|
import com.financialviewer.utils.calculateNextPrincipal
|
|
import com.financialviewer.utils.calculateNextRemaining
|
|
import com.financialviewer.utils.convertDoubleToString
|
|
import com.financialviewer.utils.convertInputStringToDouble
|
|
import com.financialviewer.utils.convertStringToDouble
|
|
import com.financialviewer.utils.getNextMonth
|
|
import kotlinx.coroutines.launch
|
|
|
|
class LoanDetailsActivity: AppCompatActivity() {
|
|
private lateinit var loanRepository: LoanRepository
|
|
private lateinit var loanDetailsAdapter: LoanDetailsAdapter
|
|
private var remainingUpdated: Boolean = false
|
|
private var sumInterest: Double = 0.0
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_loan_details)
|
|
|
|
loanRepository = LoanRepository(this)
|
|
|
|
val loanId = intent.getStringExtra("loanId") ?: "R1"
|
|
|
|
val titleTextView: TextView = findViewById(R.id.loanTitle)
|
|
val paymentTextView: TextView = findViewById(R.id.payment)
|
|
val remainingTextView: TextView = findViewById(R.id.remaining)
|
|
val rateTextView: TextView = findViewById(R.id.rate)
|
|
val sumInterestTextView: TextView = findViewById(R.id.sumInterest)
|
|
|
|
lifecycleScope.launch {
|
|
val loan = loanRepository.getLoanById(loanId)
|
|
if (loan != null) {
|
|
titleTextView.text = loan.title
|
|
remainingTextView.text = "当前欠款:" + loan.remainingLoan
|
|
paymentTextView.text = "月供: " + loan.payment
|
|
rateTextView.text = "利率:" + loan.rate
|
|
|
|
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
|
|
recyclerView.layoutManager = LinearLayoutManager(this@LoanDetailsActivity)
|
|
loanDetailsAdapter = LoanDetailsAdapter(generateMonthlyDetails(loan))
|
|
recyclerView.adapter = loanDetailsAdapter
|
|
|
|
sumInterestTextView.text = "预计还需支付利息:" + convertDoubleToString(sumInterest)
|
|
}
|
|
}
|
|
|
|
val refreshButton: Button = findViewById(R.id.refresh)
|
|
refreshButton.setOnClickListener {
|
|
lifecycleScope.launch {
|
|
val loan = loanRepository.getLoanById(loanId)
|
|
if (loan != null) {
|
|
loanDetailsAdapter.updateLoanDetailsList(generateMonthlyDetails(loan))
|
|
sumInterestTextView.text = "预计还需支付利息:" + convertDoubleToString(sumInterest)
|
|
}
|
|
}
|
|
}
|
|
|
|
val specialRepaymentButton: Button = findViewById(R.id.specialRepayment)
|
|
specialRepaymentButton.setOnClickListener {
|
|
// 创建一个EditText作为输入框
|
|
val input = EditText(this@LoanDetailsActivity)
|
|
|
|
// 创建并显示AlertDialog
|
|
val dialog = AlertDialog.Builder(this@LoanDetailsActivity)
|
|
.setTitle("提前还款金额")
|
|
.setView(input) // 将EditText添加到对话框
|
|
.setPositiveButton("Confirm") { _, _ ->
|
|
// 获取用户输入并更新TextView的内容
|
|
val inputString = input.text.toString()
|
|
try {
|
|
val inputDouble = convertInputStringToDouble(inputString) // 成功转换
|
|
lifecycleScope.launch {
|
|
val loan = loanRepository.getLoanById(loanId)
|
|
|
|
if (loan != null) {
|
|
val newRemainingLoan = convertStringToDouble(loan.remainingLoan) - inputDouble
|
|
remainingTextView.text = "当前欠款:" + convertDoubleToString(newRemainingLoan)
|
|
|
|
loan.remainingLoan = convertDoubleToString(newRemainingLoan)
|
|
loanRepository.updateLoan(loan)
|
|
remainingUpdated = true
|
|
}
|
|
}
|
|
} catch (e: NumberFormatException) {
|
|
// 如果输入不是有效的数字,弹出错误提示
|
|
Toast.makeText(this@LoanDetailsActivity, "Invalid input. Please enter a valid number.", Toast.LENGTH_SHORT).show()
|
|
}
|
|
|
|
}
|
|
.setNegativeButton("Cancel", null)
|
|
.create()
|
|
|
|
dialog.show()
|
|
}
|
|
|
|
onBackPressedDispatcher.addCallback(this, object: OnBackPressedCallback(true) {
|
|
override fun handleOnBackPressed() {
|
|
if (remainingUpdated) {
|
|
val resultIntent = Intent().apply {
|
|
putExtra("loanId", loanId)
|
|
}
|
|
setResult(Activity.RESULT_OK, resultIntent)
|
|
}
|
|
finish()
|
|
}
|
|
})
|
|
}
|
|
|
|
private fun generateMonthlyDetails(loan: Loan): MutableList<LoanMonthlyDetails> {
|
|
sumInterest = 0.0
|
|
val monthlyDetails: MutableList<LoanMonthlyDetails> = mutableListOf()
|
|
val now = Calendar.getInstance()
|
|
var currentYear = now.get(Calendar.YEAR)
|
|
var currentMonth = now.get(Calendar.MONTH) + 1
|
|
var base = convertStringToDouble(loan.remainingLoan)
|
|
var remaining = 1.0
|
|
while (remaining > 0) {
|
|
val month = getNextMonth(currentYear, currentMonth)
|
|
currentYear = month.split(".")[1].toInt()
|
|
currentMonth = month.split(".")[0].toInt()
|
|
|
|
val interest = calculateNextInterest(loan, base)
|
|
var principal = calculateNextPrincipal(loan, base)
|
|
remaining = calculateNextRemaining(loan, base)
|
|
|
|
if (remaining < 0) {
|
|
remaining = 0.0
|
|
principal = base
|
|
}
|
|
base = remaining
|
|
|
|
monthlyDetails.add(LoanMonthlyDetails(
|
|
month,
|
|
convertDoubleToString(interest),
|
|
convertDoubleToString(principal),
|
|
convertDoubleToString(remaining)
|
|
))
|
|
sumInterest += interest
|
|
}
|
|
return monthlyDetails
|
|
}
|
|
} |