Skip to content

Commit

Permalink
Merge pull request #969 from novasamatech/feature/display-whole-total…
Browse files Browse the repository at this point in the history
…-balance

Display whole total balance
  • Loading branch information
ERussel authored Feb 7, 2024
2 parents 98cd0eb + 992dbc4 commit 4a2c152
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
63 changes: 41 additions & 22 deletions novawallet/Common/Helpers/AssetBalanceFormatterFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protocol AssetBalanceFormatterFactoryProtocol {

func createAssetPriceFormatter(for info: AssetBalanceDisplayInfo) -> LocalizableResource<TokenFormatter>

func createTotalPriceFormatter(for info: AssetBalanceDisplayInfo) -> LocalizableResource<TokenFormatter>

func createInputTokenFormatter(
for info: AssetBalanceDisplayInfo
) -> LocalizableResource<TokenFormatter>
Expand All @@ -40,12 +42,14 @@ class AssetBalanceFormatterFactory {
private func createTokenFormatterCommon(
for info: AssetBalanceDisplayInfo,
roundingMode: NumberFormatter.RoundingMode,
preferredPrecisionOffset: UInt8 = 0
preferredPrecisionOffset: UInt8 = 0,
usesSuffixForBigNumbers: Bool = true
) -> LocalizableResource<TokenFormatter> {
let formatter = createCompoundFormatter(
for: info.displayPrecision,
roundingMode: roundingMode,
prefferedPrecisionOffset: preferredPrecisionOffset
prefferedPrecisionOffset: preferredPrecisionOffset,
usesSuffixForBigNumber: usesSuffixForBigNumbers
)

let tokenFormatter = TokenFormatter(
Expand All @@ -65,9 +69,10 @@ class AssetBalanceFormatterFactory {
private func createCompoundFormatter(
for preferredPrecision: UInt16,
roundingMode: NumberFormatter.RoundingMode = .down,
prefferedPrecisionOffset: UInt8 = 0
prefferedPrecisionOffset: UInt8 = 0,
usesSuffixForBigNumber: Bool = true
) -> LocalizableDecimalFormatting {
let abbreviations: [BigNumberAbbreviation] = [
var abbreviations: [BigNumberAbbreviation] = [
BigNumberAbbreviation(
threshold: 0,
divisor: 1.0,
Expand All @@ -93,27 +98,32 @@ class AssetBalanceFormatterFactory {
divisor: 1.0,
suffix: "",
formatter: nil
),
BigNumberAbbreviation(
threshold: 1_000_000,
divisor: 1_000_000.0,
suffix: "M",
formatter: nil
),
BigNumberAbbreviation(
threshold: 1_000_000_000,
divisor: 1_000_000_000.0,
suffix: "B",
formatter: nil
),
BigNumberAbbreviation(
threshold: 1_000_000_000_000,
divisor: 1_000_000_000_000.0,
suffix: "T",
formatter: nil
)
]

if usesSuffixForBigNumber {
abbreviations.append(contentsOf: [
BigNumberAbbreviation(
threshold: 1_000_000,
divisor: 1_000_000.0,
suffix: "M",
formatter: nil
),
BigNumberAbbreviation(
threshold: 1_000_000_000,
divisor: 1_000_000_000.0,
suffix: "B",
formatter: nil
),
BigNumberAbbreviation(
threshold: 1_000_000_000_000,
divisor: 1_000_000_000_000.0,
suffix: "T",
formatter: nil
)
])
}

return BigNumberFormatter(
abbreviations: abbreviations,
precision: 2,
Expand Down Expand Up @@ -154,6 +164,15 @@ extension AssetBalanceFormatterFactory: AssetBalanceFormatterFactoryProtocol {
createTokenFormatterCommon(for: info, roundingMode: .down, preferredPrecisionOffset: 2)
}

func createTotalPriceFormatter(for info: AssetBalanceDisplayInfo) -> LocalizableResource<TokenFormatter> {
createTokenFormatterCommon(
for: info,
roundingMode: .down,
preferredPrecisionOffset: 2,
usesSuffixForBigNumbers: false
)
}

func createInputTokenFormatter(
for info: AssetBalanceDisplayInfo
) -> LocalizableResource<TokenFormatter> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ final class AssetListViewModelFactory: AssetListAssetViewModelFactory {

private lazy var iconGenerator = NovaIconGenerator()

private func formatTotalPrice(from prices: [AssetListAssetAccountPrice], locale: Locale) -> String {
let totalPrice = prices.reduce(Decimal(0)) { result, item in
private func calculateTotalPrice(from prices: [AssetListAssetAccountPrice]) -> Decimal {
prices.reduce(Decimal(0)) { result, item in
let balance = Decimal.fromSubstrateAmount(
item.balance,
precision: item.assetInfo.assetPrecision
Expand All @@ -66,8 +66,18 @@ final class AssetListViewModelFactory: AssetListAssetViewModelFactory {

return result + balance * price
}
}

private func createTotalPriceString(
from price: Decimal,
priceData: PriceData?,
locale: Locale
) -> String {
let currencyId = priceData?.currencyId ?? currencyManager.selectedCurrency.id
let assetDisplayInfo = priceAssetInfoFactory.createAssetBalanceDisplayInfo(from: currencyId)
let priceFormatter = assetFormatterFactory.createTotalPriceFormatter(for: assetDisplayInfo)

return formatPrice(amount: totalPrice, priceData: prices.first?.price, locale: locale)
return priceFormatter.value(for: locale).stringFromDecimal(price) ?? ""
}

private func createTotalPrice(
Expand All @@ -78,10 +88,20 @@ final class AssetListViewModelFactory: AssetListAssetViewModelFactory {
case .loading:
return .loading
case let .cached(value):
let formattedPrice = formatTotalPrice(from: value, locale: locale)
let formattedPrice = createTotalPriceString(
from: calculateTotalPrice(from: value),
priceData: value.first?.price,
locale: locale
)

return .cached(value: .init(amount: formattedPrice, decimalSeparator: locale.decimalSeparator))
case let .loaded(value):
let formattedPrice = formatTotalPrice(from: value, locale: locale)
let formattedPrice = createTotalPriceString(
from: calculateTotalPrice(from: value),
priceData: value.first?.price,
locale: locale
)

return .loaded(value: .init(amount: formattedPrice, decimalSeparator: locale.decimalSeparator))
}
}
Expand All @@ -107,7 +127,13 @@ extension AssetListViewModelFactory: AssetListViewModelFactoryProtocol {
walletConnectSessionsCount: formattedWalletConnectSessionsCount,
title: params.title,
amount: totalPrice,
locksAmount: params.locks.map { formatTotalPrice(from: $0, locale: locale) },
locksAmount: params.locks.map { lock in
formatPrice(
amount: calculateTotalPrice(from: lock),
priceData: lock.first?.price,
locale: locale
)
},
walletSwitch: walletSwitch,
hasSwaps: params.hasSwaps
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ final class DAppBrowserStateDataSource {
return nil
}

let addressPrefix = UInt16(addressPrefixValue)

return chainStore.values.first { model in
model.isEthereumBased && model.addressPrefix == addressPrefix
model.isEthereumBased && BigUInt(model.addressPrefix) == addressPrefixValue
}
}

Expand Down

0 comments on commit 4a2c152

Please sign in to comment.