Commit b9c169bd authored by Jü's avatar

Merge remote-tracking branch 'origin/development'

parents d57be09e 741e9621
......@@ -11,7 +11,15 @@ import Foundation
extension Double {
private static let exactNumberOfFractionDigits = 2
public func toXMR() -> UInt64? {
if self >= Double(UInt64.min) && self * Double(Constants.atomicUnitsPerMonero) < Double(UInt64.max) {
return UInt64(self * Double(Constants.atomicUnitsPerMonero))
} else {
return nil
}
}
public func toCurrency() -> String {
guard let currency = Double.currencyFormatter.string(from: NSNumber(value: self)) else {
return ""
......@@ -19,10 +27,6 @@ extension Double {
return currency
}
public func toXMR() -> UInt64 {
return UInt64(self * Double(Constants.atomicUnitsPerMonero))
}
private static let currencyFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = true
......
......@@ -475,25 +475,26 @@ extension WalletCoordinator: AmountVCDelegate {
viewController.refresh()
viewController.nextAllowed()
}
func amountVCAmountValueChanged(amount: Double?, viewController: AmountVC) {
guard let requestedXmrDouble = amount else {
viewController.showFiatValue("---", forCurrency: self.propertyStore.currency)
viewController.nextNotAllowed()
return
}
let requestedXmrInAtomicUnits = UInt64(requestedXmrDouble * Double(Constants.atomicUnitsPerMonero))
let requestedXmrInAtomicUnits = (requestedXmrDouble).toXMR()
let fiatValue = self.otherAmount(forXMRValue: requestedXmrInAtomicUnits)
viewController.showFiatValue(fiatValue, forCurrency: self.propertyStore.currency)
let available = self.moneroBag.wallet?.balance ?? 0
if requestedXmrInAtomicUnits > 0 && requestedXmrInAtomicUnits <= available {
if requestedXmrInAtomicUnits != nil && requestedXmrInAtomicUnits! > 0 && requestedXmrInAtomicUnits! <= available {
viewController.nextAllowed()
} else {
viewController.nextNotAllowed()
}
}
private func showPaymentIdViewController() {
let vc = self.storyboard.instantiateViewController(withIdentifier: PaymentIdSceneName) as! PaymentIdVC
......
......@@ -202,8 +202,7 @@ extension AmountVC: UITextFieldDelegate {
}
if let amountValue = textField.text {
self.delegate?.amountVCAmountValueChanged(amount: amountValue.toDouble(),
viewController: self)
self.delegate?.amountVCAmountValueChanged(amount: amountValue.toDouble(), viewController: self)
}
return false
......
......@@ -18,14 +18,22 @@ class Double_ExtensionTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
public func test_toXMR() {
let expectedXmr: UInt64 = 22080000000000
public func test_toXMR_Success() {
let givenDouble: Double = 11.01
let expectedUInt64: UInt64 = 11010000000000
let result = 22.08.toXMR()
let result = givenDouble.toXMR()
XCTAssertEqual(expectedXmr, result)
XCTAssertEqual(expectedUInt64, result)
}
public func test_toXMR_Error() {
let givenDouble: Double = 99999999
let result = givenDouble.toXMR()
XCTAssertEqual(nil, result)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment