diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt index f9737e7..a5875e5 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt @@ -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>(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)) { diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt index 99a6f69..1598a77 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt @@ -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) { +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) { @Composable fun TransactionsListPreview() { MaterialTheme { - TransactionsList(emptyList()) + TransactionsList(UiState()) } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt index 64f6779..38c793d 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt @@ -19,4 +19,9 @@ object DI { val formatUtil = FormatUtil() + + suspend fun init() { + bankingService.init() + } + } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt index 6c5003c..fb8465f 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt @@ -27,25 +27,18 @@ class BankingService( private val bankFinder: BankFinder ) { - private var cachedTransactions: List? = null - private val log by logger() + suspend fun init() { + val transactions = readTransactionsFromCsv() + + uiState.transactions.value = transactions + } + suspend fun findBanks(query: String): List = bankFinder.findBankByNameBankCodeOrCity(query, 25) - suspend fun getTransactions(): List { - 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<*>) {