Skip to content

Commit

Permalink
feat: display transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jvsena42 committed Oct 28, 2024
1 parent c4dfa32 commit d2ee17d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
)

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)
}

0 comments on commit d2ee17d

Please sign in to comment.