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

[trello.com/c/OSxhcWdy] UI: Balance decimals on Account tab #473

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 36 additions & 1 deletion Adamant/Modules/Account/WalletCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WalletCollectionViewCell: PagingCell {

if let balance = item.balance, item.isBalanceInitialized {
if balance < 1 {
balanceLabel.text = AdamantBalanceFormat.compact.format(balance)
balanceLabel.text = stringFor(balance: balance)
} else {
balanceLabel.text = AdamantBalanceFormat.short.format(balance)
}
Expand All @@ -53,4 +53,39 @@ class WalletCollectionViewCell: PagingCell {
accessoryContainerView.setAccessory(nil, at: .topRight)
}
}

private func stringFor(balance: Decimal) -> String {
let significantDigits = 5
let balanceStr = "\(balance)"
IanaaDvlp marked this conversation as resolved.
Show resolved Hide resolved

let minBalance: Decimal = Decimal(pow(10, -(Double(significantDigits - 1))))

// Check if the number has a decimal part
if balanceStr.contains(".") || balanceStr.contains(",") {
let parts = balanceStr.split(separator: ".")
var wholePart = String(parts[0])
IanaaDvlp marked this conversation as resolved.
Show resolved Hide resolved
let decimalPart = String(parts[1])

if balance < minBalance {
return "0.00…\(decimalPart.last ?? "0")"
}

let additionalDigits = significantDigits - wholePart.count

guard additionalDigits > .zero else { return wholePart }

var formattedDecimal = ""

for digit in decimalPart.prefix(additionalDigits) {
formattedDecimal.append(digit)
}

var value = "\(wholePart).\(formattedDecimal)"
return value

} else {
var value = AdamantBalanceFormat.compact.format(balance)
return "\(value)"
}
}
}
13 changes: 0 additions & 13 deletions Adamant/Modules/Wallets/WalletViewControllerBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,6 @@ class WalletViewControllerBase: FormViewController, WalletViewController {
}
}

private func stringFor(balance: Decimal, symbol: String?) -> String {
var value = AdamantBalanceFormat.full.format(balance, withCurrencySymbol: symbol)

if balance > 0, let symbol = symbol, let rate = currencyInfoService.getRate(for: symbol) {
let fiat = balance * rate
let fiatString = AdamantBalanceFormat.short.format(fiat, withCurrencySymbol: currencyInfoService.currentCurrency.symbol)

value = "\(value) (\(fiatString))"
}

return value
}

// MARK: - Other

func setColors() {
Expand Down