Skip to content

Commit

Permalink
Merge pull request #61 from ergoplatform/ios-improvements
Browse files Browse the repository at this point in the history
iOS Build 2117
  • Loading branch information
MrStahlfelge authored Dec 2, 2021
2 parents 4122e90 + 2e7a9c4 commit 88a1f1d
Show file tree
Hide file tree
Showing 33 changed files with 974 additions and 144 deletions.
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "org.ergoplatform.android"
minSdkVersion 24
targetSdkVersion 30
versionCode 2116
versionName "1.4.2116"
versionCode 2117
versionName "1.4.2117"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ fun inflateAndBindTokenView(
itemBinding.labelTokenVal.text =
formatTokenAmounts(
walletToken.amount ?: 0,
walletToken.decimals ?: 0,
walletToken.decimals,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import org.ergoplatform.transactions.PromptSigningResult
import org.ergoplatform.utils.formatFiatToString
import org.ergoplatform.wallet.addresses.getAddressLabel
import org.ergoplatform.wallet.getNumOfAddresses
import kotlin.math.max


/**
Expand Down Expand Up @@ -201,15 +200,7 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
ChooseTokenListDialogFragment().show(childFragmentManager, null)
}
binding.amount.setEndIconOnClickListener {
setAmountEdittext(
ErgoAmount(
max(
0L,
(viewModel.walletBalance.value?.nanoErgs ?: 0L)
- (viewModel.uiLogic.feeAmount.nanoErgs)
)
)
)
setAmountEdittext(viewModel.uiLogic.getMaxPossibleAmountToSend())
}
binding.hintReadonly.setOnClickListener {
openUrlWithBrowser(requireContext(), URL_COLD_WALLET_HELP)
Expand Down Expand Up @@ -288,7 +279,10 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
TokenAmountWatcher(tokenDbEntity)
)
itemBinding.inputTokenAmount.setText(
tokenAmountToText(it.value.value, tokenDbEntity.decimals)
viewModel.uiLogic.tokenAmountToText(
it.value.value,
tokenDbEntity.decimals
)
)
itemBinding.buttonTokenRemove.setOnClickListener {
if (itemBinding.inputTokenAmount.text.isEmpty()) {
Expand All @@ -299,19 +293,17 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
}
itemBinding.buttonTokenAll.setOnClickListener {
itemBinding.inputTokenAmount.setText(
tokenAmountToText(tokenDbEntity.amount!!, tokenDbEntity.decimals)
viewModel.uiLogic.tokenAmountToText(
tokenDbEntity.amount!!,
tokenDbEntity.decimals
)
)
}
}
}
}
}

private fun tokenAmountToText(amount: Long, decimals: Int) =
if (amount > 0)
TokenAmount(amount, decimals).toString()
else ""

private fun setAmountEdittext(amountToSend: ErgoAmount) {
binding.amount.editText?.setText(amountToSend.toStringTrimTrailingZeros())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import android.os.Handler
import android.os.Looper
import android.view.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import kotlinx.coroutines.GlobalScope
Expand Down Expand Up @@ -187,18 +190,18 @@ class WalletDetailsFragment : Fragment(), AddressChooserCallback {

// Fill fiat value
val nodeConnector = NodeConnector.getInstance()
val ergoPrice = nodeConnector.fiatValue.value ?: 0f
val ergoPrice = nodeConnector.fiatValue.value
if (ergoPrice == 0f) {
binding.walletFiat.visibility = View.GONE
} else {
binding.walletFiat.visibility = View.VISIBLE
binding.walletFiat.amount = ergoPrice * binding.walletBalance.amount
binding.walletFiat.setSymbol(nodeConnector.fiatCurrency.toUpperCase())
binding.walletFiat.setSymbol(nodeConnector.fiatCurrency.uppercase())
}

// tokens
val tokensList = address?.let { wallet.getTokensForAddress(address) }
?: wallet.getTokensForAllAddresses()
val tokensList = (address?.let { wallet.getTokensForAddress(address) }
?: wallet.getTokensForAllAddresses()).sortedBy { it.name?.lowercase() }
binding.cardviewTokens.visibility = if (tokensList.size > 0) View.VISIBLE else View.GONE
binding.walletTokenNum.text = tokensList.size.toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.ergoplatform.android.tokens.inflateAndBindTokenView
import org.ergoplatform.android.ui.AndroidStringProvider
import org.ergoplatform.android.ui.navigateSafe
import org.ergoplatform.persistance.Wallet
import org.ergoplatform.tokens.fillTokenOverview
import org.ergoplatform.utils.getTimeSpanString
import org.ergoplatform.wallet.getBalanceForAllAddresses
import org.ergoplatform.wallet.getTokensForAllAddresses
Expand Down Expand Up @@ -330,20 +331,10 @@ class WalletViewHolder(val binding: CardWalletBinding) : RecyclerView.ViewHolder
removeAllViews()

if (wallet.walletConfig.unfoldTokens) {
val maxTokensToShow = 5
val dontShowAll = tokens.size > maxTokensToShow
val tokensToShow =
(if (dontShowAll) tokens.subList(
0,
maxTokensToShow - 1
) else tokens)
val layoutInflater = LayoutInflater.from(context)
tokensToShow.forEach {
fillTokenOverview(tokens, {
inflateAndBindTokenView(it, this, layoutInflater)
}

// in case we don't show all items, add a hint that not all items were shown
if (dontShowAll) {
}, { moreToShowNum ->
val itemBinding =
EntryWalletTokenBinding.inflate(
LayoutInflater.from(itemView.context),
Expand All @@ -353,8 +344,8 @@ class WalletViewHolder(val binding: CardWalletBinding) : RecyclerView.ViewHolder

itemBinding.labelTokenName.setText(R.string.label_more_tokens)
itemBinding.labelTokenVal.text =
"+" + (tokens.size - maxTokensToShow + 1).toString()
}
"+" + moreToShowNum.toString()
})
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions common-jvm/src/main/java/org/ergoplatform/TokenAmount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class TokenAmount(val rawValue: Long, val decimals: Int) {
return rawValue.toBigDecimal().movePointLeft(decimals).toPlainString()
}

fun toStringTrimTrailingZeros(): String {
val stringWithTrailingZeros = toString()
if (decimals > 0) {
return stringWithTrailingZeros.trimEnd('0').trimEnd('.')
} else {
return stringWithTrailingZeros
}
}

fun toDouble(): Double {
return (rawValue.toDouble()) / (10.0.pow(decimals))
}
Expand Down
25 changes: 25 additions & 0 deletions common-jvm/src/main/java/org/ergoplatform/tokens/TokenUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ergoplatform.tokens

import org.ergoplatform.persistance.WalletToken

/**
* logic to fill tokens in wallet overview screen
*/
fun fillTokenOverview(tokens: List<WalletToken>, addToken: (WalletToken) -> Unit, addMoreTokenHint: (Int) -> Unit) {
val maxTokensToShow = 5
val dontShowAll = tokens.size > maxTokensToShow
val tokensSorted = tokens.sortedBy { it.name?.lowercase() }
val tokensToShow =
(if (dontShowAll) tokensSorted.subList(
0,
maxTokensToShow - 1
) else tokensSorted)
tokensToShow.forEach {
addToken.invoke(it)
}

// in case we don't show all items, add a hint that not all items were shown
if (dontShowAll) {
addMoreTokenHint.invoke((tokens.size - maxTokensToShow + 1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.ergoplatform.transactions.PromptSigningResult
import org.ergoplatform.transactions.SendTransactionResult
import org.ergoplatform.transactions.TransactionResult
import org.ergoplatform.wallet.*
import kotlin.math.max

abstract class SendFundsUiLogic() {
abstract val coroutineScope: CoroutineScope
Expand Down Expand Up @@ -128,6 +129,15 @@ abstract class SendFundsUiLogic() {
notifyAmountsChanged()
}

fun getMaxPossibleAmountToSend(): ErgoAmount {
return ErgoAmount(
max(
0L,
(balance.nanoErgs) - (feeAmount.nanoErgs)
)
)
}

fun checkCanMakePayment(): CheckCanPayResponse {
val receiverOk = isValidErgoAddress(receiverAddress)
val amountOk = amountToSend.nanoErgs >= Parameters.MinChangeValue
Expand Down Expand Up @@ -225,7 +235,7 @@ abstract class SendFundsUiLogic() {
fun getTokensToChooseFrom(): List<WalletToken> {
return tokensAvail.filter {
!tokensChosen.containsKey(it.tokenId)
}
}.sortedBy { it.name?.lowercase() }
}

fun newTokenChoosen(tokenId: String) {
Expand All @@ -247,6 +257,11 @@ abstract class SendFundsUiLogic() {
}
}

fun tokenAmountToText(amount: Long, decimals: Int) =
if (amount > 0)
TokenAmount(amount, decimals).toStringTrimTrailingZeros()
else ""

fun addTokensFromQr(tokens: HashMap<String, String>) {
var changed = false
tokens.forEach {
Expand All @@ -255,7 +270,7 @@ abstract class SendFundsUiLogic() {

// we need to check for existence here, QR code might have any String, not an ID
tokensAvail.filter { it.tokenId.equals(tokenId) }.firstOrNull()?.let {
val longAmount = amount.toTokenAmount(it.decimals ?: 0)?.rawValue ?: 0
val longAmount = amount.toTokenAmount(it.decimals)?.rawValue ?: 0
tokensChosen.put(tokenId, ErgoToken(tokenId, longAmount))
changed = true
}
Expand Down
23 changes: 16 additions & 7 deletions common-jvm/src/main/java/org/ergoplatform/utils/TimeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,36 @@ package org.ergoplatform.utils

import org.ergoplatform.uilogic.*

private const val MAX_SECONDS_JUST_NOW = 30
private const val MAX_SECONDS_MOMENTS_AGO = 120
private const val MAX_MINUTES_TO_SHOW = 120
private const val MAX_HOURS_TO_SHOW = 48

private const val SECONDS_PER_MINUTE = 60
private const val MINUTES_PER_HOUR = 60
private const val HOURS_PER_DAY = 24

fun getTimeSpanString(
seconds: Long,
stringProvider: StringProvider
): String {
val timeSpanString: String
if (seconds < 30) {
if (seconds < MAX_SECONDS_JUST_NOW) {
timeSpanString = stringProvider.getString(STRING_LABEL_TIME_SPAN_JUST_NOW)
} else if (seconds < 120) {
} else if (seconds < MAX_SECONDS_MOMENTS_AGO) {
timeSpanString = stringProvider.getString(STRING_LABEL_TIME_SPAN_MOMENTS_AGO)
} else {
val minuteTimeSpan = seconds / 60
if (minuteTimeSpan < 120) {
val minuteTimeSpan = seconds / SECONDS_PER_MINUTE
if (minuteTimeSpan < MAX_MINUTES_TO_SHOW) {
timeSpanString =
stringProvider.getString(STRING_LABEL_TIME_SPAN_MINUTES_AGO, minuteTimeSpan)
} else {
val hourTimeSpan = minuteTimeSpan / 24
if (hourTimeSpan < 48)
val hourTimeSpan = minuteTimeSpan / MINUTES_PER_HOUR
if (hourTimeSpan < MAX_HOURS_TO_SHOW)
timeSpanString =
stringProvider.getString(STRING_LABEL_TIME_SPAN_HOURS_AGO, hourTimeSpan)
else {
val dayTimeSpan = hourTimeSpan / 24
val dayTimeSpan = hourTimeSpan / HOURS_PER_DAY
timeSpanString =
stringProvider.getString(STRING_LABEL_TIME_SPAN_DAYS_AGO, dayTimeSpan)
}
Expand Down
17 changes: 17 additions & 0 deletions common-jvm/src/test/java/org/ergoplatform/TokenAmountTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ergoplatform

import junit.framework.TestCase
import org.junit.Assert

class TokenAmountTest : TestCase() {

fun testTestToString() {

Assert.assertEquals("1", TokenAmount(1, 0).toStringTrimTrailingZeros())
Assert.assertEquals("10", TokenAmount(100, 1).toStringTrimTrailingZeros())
Assert.assertEquals("10.0", TokenAmount(100, 1).toString())
Assert.assertEquals("10.5", TokenAmount(1050, 2).toStringTrimTrailingZeros())
Assert.assertEquals("10", TokenAmount(1000, 2).toStringTrimTrailingZeros())
Assert.assertEquals("0", TokenAmount(0, 2).toStringTrimTrailingZeros())
}
}
2 changes: 1 addition & 1 deletion ios/resources/i18n/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ generic_error_no_keyguard=This device does not support PIN, pattern, or password
generic_error_user_canceled=Authentication canceled by user.
hide_bottom_view_on_scroll_behavior=com.google.android.material.behavior.HideBottomViewOnScrollBehavior
hint_password=Please enter your spending password
hint_read_only=This is a read only wallet. For sending funds, you need a device to sign your transaction. <a href="">Learn more.</a>
hint_read_only=This is a read only wallet. For sending funds, you need to restore it with your mnemonic. Cold wallet capabilities coming soon.
hint_wallet_addr_label=Descriptive label (optional)
intro_add_readonly=Enter your public wallet address to add the wallet in read-only mode.
intro_confirm_create_wallet=To confirm that your handwritten mnemonic is correct, enter two words of the phrase now:
Expand Down
4 changes: 2 additions & 2 deletions ios/robovm.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app.version=0.3.2116
app.version=0.6.2117
app.id=org.ergoplatform.ios
app.mainclass=org.ergoplatform.ios.Main
app.executable=ErgoWallet
app.build=2116
app.build=2117
app.name=Ergo Wallet
Loading

0 comments on commit 88a1f1d

Please sign in to comment.