Skip to content

Commit

Permalink
catching AmountError<Zil> in SufficientFundsValidator, catch crash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Jun 20, 2019
1 parent 53d3d86 commit 319803e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extension AmountFromText {

extension AmountFromText {

// swiftlint:disable:next function_body_length
init(string amountString: String, unit: Zesame.Unit) throws {
let correctSeparator = Locale.current.decimalSeparatorForSure
let wrongSeparator = correctSeparator == "." ? "," : "."
Expand All @@ -70,13 +71,4 @@ extension AmountFromText {
}
}
}

var display: String {
switch self {
case .amount(let amount, let unit):
return AmountFormatter().format(amount: amount, in: unit)
case .string(let string):
return string
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ struct SufficientFundsValidator: InputValidator {
return self.error(Error.insufficientFunds)
}
} catch let error as AmountError<ZilAmount> {
return .invalid(.error(.amountError(error)))
} catch {
incorrectImplementation("AmountError should cover all errors")
return self.error(Error.amountError(error))
} catch let otherError {
let zilAmountError: AmountError<ZilAmount> = .init(error: otherError)
return self.error(Error.amountError(zilAmountError))
}

return .valid(amount)
Expand Down
10 changes: 6 additions & 4 deletions Source/Application/OutputFormatting/AmountFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ extension Zesame.Unit {
}

struct AmountFormatter {
func format<Amount>(amount: Amount, in unit: Zesame.Unit, minFractionDigits: Int? = nil, showUnit: Bool = false) -> String where Amount: ExpressibleByAmount {
let amountString = amount.formatted(unit: unit, minFractionDigits: minFractionDigits)
func format<Amount>(amount: Amount, in unit: Zesame.Unit, formatThousands: Bool, minFractionDigits: Int? = nil, showUnit: Bool = false) -> String where Amount: ExpressibleByAmount {
let amountString = amount.formatted(unit: unit, formatThousands: formatThousands, minFractionDigits: minFractionDigits)
guard showUnit else {
return amountString
}
Expand All @@ -51,9 +51,11 @@ struct AmountFormatter {
}

extension ExpressibleByAmount {
func formatted(unit targetUnit: Zesame.Unit, minFractionDigits: Int? = nil) -> String {
func formatted(unit targetUnit: Zesame.Unit, formatThousands: Bool = false, minFractionDigits: Int? = nil) -> String {
let string = asString(in: targetUnit, roundingIfNeeded: .down, roundingNumberOfDigits: AmountFormatter.maxNumberOfDecimalDigits, minFractionDigits: minFractionDigits)

guard formatThousands else {
return string
}
let decimalSeparator = Locale.current.decimalSeparatorForSure
let components = string.components(separatedBy: decimalSeparator)
if components.count == 2 {
Expand Down
2 changes: 1 addition & 1 deletion Source/Scenes/1_Main/1_Main/0_Main/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ final class MainViewModel: BaseViewModel<

return Output(
isFetchingBalance: activityIndicator.asDriver(),
balance: latestBalanceOrZero.map { formatter.format(amount: $0, in: .zil) },
balance: latestBalanceOrZero.map { formatter.format(amount: $0, in: .zil, formatThousands: true) },
refreshControlLastUpdatedTitle: refreshControlLastUpdatedTitle
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final class PrepareTransactionViewModel: BaseViewModel<

let nonce = latestBalanceAndNonce.map { $0.nonce }.startWith(0)
let _startingBalance: ZilAmount = transactionUseCase.cachedBalance ?? 0
let balance: Driver<ZilAmount> = latestBalanceAndNonce.map { $0.balance }.startWith(_startingBalance)
let balance: Driver<ZilAmount> = latestBalanceAndNonce.map { $0.balance }.startWith(_startingBalance)

// MARK: - VALIDATION -> VALUE
let validator = InputValidator()
Expand Down Expand Up @@ -196,21 +196,28 @@ final class PrepareTransactionViewModel: BaseViewModel<

// MARK: FORMATTING
let formatter = AmountFormatter()
let balanceFormatted = balance.map { formatter.format(amount: $0, in: .zil, showUnit: true) }
let balanceFormatted = balance.map { formatter.format(amount: $0, in: .zil, formatThousands: true, showUnit: true) }

// It is deliberate that we do NOT auto checksum the address here. We would like to be able to inform the user that
// she might have pasted an unchecksummed address.
let recipientFormatted = recipient.filterNil().map { $0.asString }

let amountFormatted: Driver<String?> = amountBoundByBalance.filterNil()
.map { formatter.format(amount: $0, in: .zil) }
.ifEmpty(switchTo: amountWithoutSufficientFundsCheckValidationValue.map { $0.value?.display })
.map { formatter.format(amount: $0, in: .zil, formatThousands: false) }
.ifEmpty(switchTo: amountWithoutSufficientFundsCheckValidationValue.map {
guard let value = $0.value else { return nil }
switch value {
case .string(let string): return string
case .amount(let amount, _):
return formatter.format(amount: amount, in: .zil, formatThousands: false)
}
})

let isSendButtonEnabled = payment.map { $0 != nil }

let gasPricePlaceholder = Driver.just(GasPrice.min).map {.Field.gasPrice(formatter.format(amount: $0, in: .li, showUnit: true)) }
let gasPricePlaceholder = Driver.just(GasPrice.min).map {.Field.gasPrice(formatter.format(amount: $0, in: .li, formatThousands: true, showUnit: true)) }

let gasPriceFormatted = gasPrice.filterNil().map { formatter.format(amount: $0, in: .li) }
let gasPriceFormatted = gasPrice.filterNil().map { formatter.format(amount: $0, in: .li, formatThousands: true) }

let balanceWasUpdatedAt = fetchTrigger.map { [unowned self] in
self.transactionUseCase.balanceUpdatedAt
Expand Down Expand Up @@ -251,7 +258,7 @@ final class PrepareTransactionViewModel: BaseViewModel<
return Observable<GasPrice>.just(gasPrice)
.map { try Payment.estimatedTotalTransactionFee(gasPrice: $0) }
.asDriverOnErrorReturnEmpty()
.map { formatter.format(amount: $0, in: .zil, showUnit: true) }
.map { formatter.format(amount: $0, in: .zil, formatThousands: true, showUnit: true) }
.map {.Label.costOfTransactionInZil($0) }
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,9 @@ final class ReceiveViewModel: BaseViewModel<
.drive()
]

// let amountFormatted: Driver<String?> = amountValidationValue.map { $0.value?.display }
// /* ugly fix to fix bug where if you've entered an incorrect decimal separator then the text
// never is formatted using the thousands formatting again.
// */
// .ifNil(switchTo:
// input.fromView.amountToReceive.map {
// if let zilAmount = try? ZilAmount(trimming: $0) {
// let formattedFallback = AmountFormatter().format(amount: zilAmount, in: .zil)
// return formattedFallback
// } else {
// return $0
// }
// }
// )

return Output(
receivingAddress: receivingAddress,
amountPlaceholder: Driver.just(.Field.requestAmount(Unit.zil.name)),
// amount: amountFormatted,
amountValidation: amountValidation,
qrImage: qrImage
)
Expand All @@ -137,7 +121,6 @@ extension ReceiveViewModel {
struct Output {
let receivingAddress: Driver<String>
let amountPlaceholder: Driver<String>
// let amount: Driver<String?>
let amountValidation: Driver<AnyValidation>
let qrImage: Driver<UIImage?>
}
Expand Down

0 comments on commit 319803e

Please sign in to comment.