diff --git a/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt b/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt index 0accd64..6ebacad 100644 --- a/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt +++ b/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt @@ -7,6 +7,7 @@ import com.github.jvsena42.floresta.data.FlorestaRpc import com.github.jvsena42.floresta.domain.bitcoin.WalletManager import com.github.jvsena42.floresta.domain.bitcoin.WalletRepository import com.github.jvsena42.floresta.domain.floresta.FlorestaDaemon +import com.github.jvsena42.floresta.domain.model.ChainPosition import com.github.jvsena42.floresta.presentation.util.formatInBtc import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -54,11 +55,13 @@ class HomeViewModel( private suspend fun updateUI() { if (walletRepository.doesWalletExist()) { val balanceSats = walletManager.getBalance() + val listTransactions = walletManager.listTransactions().filter { it.chainPosition is ChainPosition.Confirmed }.map { it.toTransactionVM() } Log.d(TAG, "setup: Wallet exists. balance: $balanceSats") _uiState.update { it.copy( balanceBTC = balanceSats.formatInBtc(), - balanceSats = balanceSats.toString() + balanceSats = balanceSats.toString(), + transactions = listTransactions ) } } else { diff --git a/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/TransactionVM.kt b/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/TransactionVM.kt index a378482..4ab7f36 100644 --- a/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/TransactionVM.kt +++ b/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/TransactionVM.kt @@ -1,8 +1,28 @@ package com.github.jvsena42.floresta.presentation.ui.screens.home +import com.github.jvsena42.floresta.domain.model.ChainPosition +import com.github.jvsena42.floresta.domain.model.TransactionDetails +import com.github.jvsena42.floresta.domain.model.TxType +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + data class TransactionVM( val title: String, val date: String, val amount: String, val isReceived: Boolean -) \ No newline at end of file +) + +fun TransactionDetails.toTransactionVM() = TransactionVM( + title = this.txid, + date = if (this.chainPosition is ChainPosition.Confirmed) convertMillisecondsToDateString(this.chainPosition.timestamp.toLong()) else "", + amount = (if (txType == TxType.RECEIVE) this.received.toSat() else this.sent.toSat()).toString(), + isReceived = txType == TxType.RECEIVE +) + +private fun convertMillisecondsToDateString(milliseconds: Long): String { + val date = Date(milliseconds) + val format = SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault()) + return format.format(date) +} \ No newline at end of file