Commit b9c169bd authored by Jü's avatar

Merge remote-tracking branch 'origin/development'

parents d57be09e 741e9621
...@@ -11,7 +11,15 @@ import Foundation ...@@ -11,7 +11,15 @@ import Foundation
extension Double { extension Double {
private static let exactNumberOfFractionDigits = 2 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 { public func toCurrency() -> String {
guard let currency = Double.currencyFormatter.string(from: NSNumber(value: self)) else { guard let currency = Double.currencyFormatter.string(from: NSNumber(value: self)) else {
return "" return ""
...@@ -19,10 +27,6 @@ extension Double { ...@@ -19,10 +27,6 @@ extension Double {
return currency return currency
} }
public func toXMR() -> UInt64 {
return UInt64(self * Double(Constants.atomicUnitsPerMonero))
}
private static let currencyFormatter: NumberFormatter = { private static let currencyFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter() let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = true numberFormatter.allowsFloats = true
......
...@@ -475,25 +475,26 @@ extension WalletCoordinator: AmountVCDelegate { ...@@ -475,25 +475,26 @@ extension WalletCoordinator: AmountVCDelegate {
viewController.refresh() viewController.refresh()
viewController.nextAllowed() viewController.nextAllowed()
} }
func amountVCAmountValueChanged(amount: Double?, viewController: AmountVC) { func amountVCAmountValueChanged(amount: Double?, viewController: AmountVC) {
guard let requestedXmrDouble = amount else { guard let requestedXmrDouble = amount else {
viewController.showFiatValue("---", forCurrency: self.propertyStore.currency) viewController.showFiatValue("---", forCurrency: self.propertyStore.currency)
viewController.nextNotAllowed() viewController.nextNotAllowed()
return return
} }
let requestedXmrInAtomicUnits = UInt64(requestedXmrDouble * Double(Constants.atomicUnitsPerMonero)) let requestedXmrInAtomicUnits = (requestedXmrDouble).toXMR()
let fiatValue = self.otherAmount(forXMRValue: requestedXmrInAtomicUnits) let fiatValue = self.otherAmount(forXMRValue: requestedXmrInAtomicUnits)
viewController.showFiatValue(fiatValue, forCurrency: self.propertyStore.currency) viewController.showFiatValue(fiatValue, forCurrency: self.propertyStore.currency)
let available = self.moneroBag.wallet?.balance ?? 0 let available = self.moneroBag.wallet?.balance ?? 0
if requestedXmrInAtomicUnits > 0 && requestedXmrInAtomicUnits <= available { if requestedXmrInAtomicUnits != nil && requestedXmrInAtomicUnits! > 0 && requestedXmrInAtomicUnits! <= available {
viewController.nextAllowed() viewController.nextAllowed()
} else { } else {
viewController.nextNotAllowed() viewController.nextNotAllowed()
} }
} }
private func showPaymentIdViewController() { private func showPaymentIdViewController() {
let vc = self.storyboard.instantiateViewController(withIdentifier: PaymentIdSceneName) as! PaymentIdVC let vc = self.storyboard.instantiateViewController(withIdentifier: PaymentIdSceneName) as! PaymentIdVC
......
...@@ -202,8 +202,7 @@ extension AmountVC: UITextFieldDelegate { ...@@ -202,8 +202,7 @@ extension AmountVC: UITextFieldDelegate {
} }
if let amountValue = textField.text { if let amountValue = textField.text {
self.delegate?.amountVCAmountValueChanged(amount: amountValue.toDouble(), self.delegate?.amountVCAmountValueChanged(amount: amountValue.toDouble(), viewController: self)
viewController: self)
} }
return false return false
......
...@@ -18,14 +18,22 @@ class Double_ExtensionTests: XCTestCase { ...@@ -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. // Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown() super.tearDown()
} }
public func test_toXMR() { public func test_toXMR_Success() {
let expectedXmr: UInt64 = 22080000000000 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