Added transactions now to UiState to be able to watch them

This commit is contained in:
dankito 2024-08-26 19:21:37 +02:00
parent 75f73164aa
commit 3c4a2232e8
4 changed files with 24 additions and 29 deletions

View File

@ -26,8 +26,6 @@ private val typography = Typography(
body1 = TextStyle(fontSize = 14.sp, color = Colors.Zinc700)
)
private val bankService = DI.bankingService
@Composable
@Preview
fun App() {
@ -36,10 +34,9 @@ fun App() {
var showAddAccountDialog by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
val (transactions, setTransaction) = remember { mutableStateOf<List<AccountTransaction>>(emptyList()) }
coroutineScope.launch {
setTransaction(bankService.getTransactions())
DI.init()
}
@ -49,7 +46,7 @@ fun App() {
Modifier.fillMaxWidth().fillMaxHeight().background(color = Colors.Zinc100),
horizontalAlignment = Alignment.CenterHorizontally
) {
TransactionsList(transactions)
TransactionsList(DI.uiState)
}
Row(Modifier.align(Alignment.BottomEnd)) {

View File

@ -7,10 +7,7 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
@ -24,12 +21,15 @@ import net.codinux.banking.ui.extensions.toBigDecimal
import net.codinux.banking.ui.forms.RoundedCornersCard
import net.codinux.banking.ui.config.Colors
import net.codinux.banking.ui.config.DI
import net.codinux.banking.ui.state.UiState
import org.jetbrains.compose.ui.tooling.preview.Preview
private val formatUtil = DI.formatUtil
@Composable
fun TransactionsList(transactions: List<AccountTransaction>) {
fun TransactionsList(uiState: UiState) {
val transactions by uiState.transactions.collectAsState()
val groupedByMonth by remember(transactions) {
derivedStateOf { transactions.groupBy { LocalDate(it.valueDate.year, it.valueDate.monthNumber, 1) } }
}
@ -86,6 +86,6 @@ fun TransactionsList(transactions: List<AccountTransaction>) {
@Composable
fun TransactionsListPreview() {
MaterialTheme {
TransactionsList(emptyList())
TransactionsList(UiState())
}
}

View File

@ -19,4 +19,9 @@ object DI {
val formatUtil = FormatUtil()
suspend fun init() {
bankingService.init()
}
}

View File

@ -27,25 +27,18 @@ class BankingService(
private val bankFinder: BankFinder
) {
private var cachedTransactions: List<AccountTransaction>? = null
private val log by logger()
suspend fun init() {
val transactions = readTransactionsFromCsv()
uiState.transactions.value = transactions
}
suspend fun findBanks(query: String): List<BankInfo> =
bankFinder.findBankByNameBankCodeOrCity(query, 25)
suspend fun getTransactions(): List<AccountTransaction> {
cachedTransactions?.let {
return it
}
val transactions = readTransactionsFromCsv()
cachedTransactions = transactions
return transactions
}
suspend fun addAccount(bank: BankInfo, loginName: String, password: String): Boolean {
try {
val config = FinTsClientConfiguration(FinTsClientOptions(true))
@ -73,11 +66,11 @@ class BankingService(
private fun handleSuccessfulGetAccountDataResponse(response: GetAccountDataResponse) {
// TODO: save customer
if (cachedTransactions == null) {
cachedTransactions = response.bookedTransactions
} else {
cachedTransactions = (cachedTransactions!! + response.bookedTransactions).sortedByDescending { it.valueDate }
}
val transactions = uiState.transactions.value.toMutableList()
transactions.addAll(response.bookedTransactions)
uiState.transactions.value = transactions.sortedByDescending { it.valueDate }
}
private fun handleUnsuccessfulBankingClientResponse(action: BankingClientAction, response: Response<*>) {