Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: available to spend balance recalculation #1053

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"
apply from: "../download-libwallet.gradle"
apply plugin: "io.sentry.android.gradle"
apply plugin: "kotlin-parcelize"

android {
namespace "com.tari.android.wallet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ fun Float.remap(from1: Float, to1: Float, from2: Float, to2: Float): Float {
/**
* Int to MicroTari.
*/
fun Int.toMicroTari(): MicroTari {
return MicroTari(BigInteger.valueOf(this.toLong()))
}
fun Int.toMicroTari() = BigInteger.valueOf(this.toLong()).toMicroTari()

/**
* Long to MicroTari.
*/
fun Long.toMicroTari(): MicroTari {
return MicroTari(BigInteger.valueOf(this))
}
fun Long.toMicroTari() = BigInteger.valueOf(this).toMicroTari()

/**
* BigInteger to MicroTari.
*/
fun BigInteger.toMicroTari() = MicroTari(this)
74 changes: 14 additions & 60 deletions app/src/main/java/com/tari/android/wallet/model/BalanceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,71 +32,25 @@
*/
package com.tari.android.wallet.model

import android.os.Parcel
import android.os.Parcelable
import com.tari.android.wallet.ui.extension.readP
import java.math.BigInteger
import com.tari.android.wallet.extension.toMicroTari
import kotlinx.parcelize.Parcelize

/**
* Wallet balance info.
*
* @author The Tari Development Team
*/
class BalanceInfo() : Parcelable {

var availableBalance = MicroTari(BigInteger("0"))
var pendingIncomingBalance = MicroTari(BigInteger("0"))
var pendingOutgoingBalance = MicroTari(BigInteger("0"))
var timeLocked = MicroTari(BigInteger("0"))

constructor(
availableBalance: MicroTari,
pendingIncomingBalance: MicroTari,
pendingOutgoingBalance: MicroTari,
timeLocked: MicroTari,
) : this() {
this.availableBalance = availableBalance
this.pendingIncomingBalance = pendingIncomingBalance
this.pendingOutgoingBalance = pendingOutgoingBalance
this.timeLocked = timeLocked
}

// region Parcelable

constructor(parcel: Parcel) : this() {
readFromParcel(parcel)
}

companion object CREATOR : Parcelable.Creator<BalanceInfo> {

override fun createFromParcel(parcel: Parcel): BalanceInfo {
return BalanceInfo(parcel)
}

override fun newArray(size: Int): Array<BalanceInfo> {
return Array(size) { BalanceInfo() }
}

}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(availableBalance, flags)
parcel.writeParcelable(pendingIncomingBalance, flags)
parcel.writeParcelable(pendingOutgoingBalance, flags)
parcel.writeParcelable(timeLocked, flags)
}

private fun readFromParcel(inParcel: Parcel) {
availableBalance = inParcel.readP(MicroTari::class.java)
pendingIncomingBalance = inParcel.readP(MicroTari::class.java)
pendingOutgoingBalance = inParcel.readP(MicroTari::class.java)
timeLocked = inParcel.readP(MicroTari::class.java)
}

override fun describeContents(): Int {
return 0
}

// endregion

@Parcelize
data class BalanceInfo(
val availableBalance: MicroTari = 0.toMicroTari(),
val pendingIncomingBalance: MicroTari = 0.toMicroTari(),
val pendingOutgoingBalance: MicroTari = 0.toMicroTari(),
val timeLockedBalance: MicroTari = 0.toMicroTari(),
) : Parcelable {
val totalBalance: MicroTari
get() = availableBalance + pendingIncomingBalance

val availableToSpendBalance: MicroTari
get() = availableBalance - timeLockedBalance
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class AddAmountFragment : CommonFragment<FragmentAddAmountBinding, AddAmountView

val amount = keyboardController.currentAmount
if (error == WalletError.NoError && fee != null) {
if (amount > balanceInfo.availableBalance && !DebugConfig.mockedDataEnabled) {
if (amount > balanceInfo.availableToSpendBalance && !DebugConfig.mockedDataEnabled) {
lifecycleScope.launch(Dispatchers.Main) {
actualBalanceExceeded()
}
Expand Down Expand Up @@ -256,7 +256,7 @@ class AddAmountFragment : CommonFragment<FragmentAddAmountBinding, AddAmountView

private fun updateBalanceInfo() {
balanceInfo = viewModel.walletService.getWithError { error, wallet -> wallet.getBalanceInfo(error) }
availableBalance = balanceInfo.availableBalance
availableBalance = balanceInfo.availableToSpendBalance
ui.availableBalanceContainerView.setupArgs(availableBalance)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class HomeFragment : CommonFragment<FragmentHomeBinding, HomeFragmentViewModel>(
private fun updateBalanceInfoUI(restart: Boolean) {
val balanceInfo = viewModel.balanceInfo.value!!

val availableBalance = WalletUtil.balanceFormatter.format(balanceInfo.availableBalance.tariValue)
val availableBalance = WalletUtil.balanceFormatter.format(balanceInfo.availableToSpendBalance.tariValue)
ui.availableBalance.text = availableBalance

if (restart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BalanceViewController(
private val delayByIndex: Long = 80L

init {
val balance = _balanceInfo.availableBalance + _balanceInfo.pendingIncomingBalance
val balance = _balanceInfo.totalBalance
formattedBalance = WalletUtil.balanceFormatter.format(balance.tariValue)
// decimal tens
viewHolders.add(DecimalDigitViewHolder(context, formattedBalance[formattedBalance.length - 1].toString().toInt()))
Expand Down Expand Up @@ -94,7 +94,7 @@ class BalanceViewController(
set(value) {
/* execute setter logic */
_balanceInfo = value
val balance = _balanceInfo.availableBalance + _balanceInfo.pendingIncomingBalance
val balance = _balanceInfo.totalBalance
formattedBalance = WalletUtil.balanceFormatter.format(balance.tariValue)
val sizeDiff = formattedBalance.length - viewHolders.size
if (sizeDiff <= 0) {
Expand Down