-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
accepting decimal values as amount input. changed behaviour in Prepar…
…eTransactionViewModel to only change the amount textfields text value in the view when receiving an amount externally from QR scan or deeplink (reducing complexity logic and fixing issues with decimal strings with trailing '.0' etc)
- Loading branch information
Showing
27 changed files
with
4,071 additions
and
3,711 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
.../Models/Manual/ExpressibleByAmount/ExpressibleByAmount+CustomDebugStringConvertible.swift
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Source/Application/InputValidators/Validators/AmountFromText.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2018-2019 Open Zesame (https://github.com/OpenZesame) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
import Zesame | ||
|
||
enum AmountFromText<Amount: ExpressibleByAmount> { | ||
case amount(Amount, in: Zesame.Unit) | ||
case string(String) | ||
} | ||
|
||
extension AmountFromText { | ||
var amount: Amount? { | ||
guard case let .amount(amount, _) = self else { return nil } | ||
return amount | ||
} | ||
|
||
var string: String? { | ||
guard case let .string(text) = self else { return nil } | ||
return text | ||
} | ||
} | ||
|
||
extension AmountFromText { | ||
|
||
init(string amountString: String, unit: Zesame.Unit) throws { | ||
let correctSeparator = Locale.current.decimalSeparatorForSure | ||
let wrongSeparator = correctSeparator == "." ? "," : "." | ||
|
||
let incorrectDecimalSeparatorRemoved = amountString.replacingOccurrences(of: wrongSeparator, with: correctSeparator) | ||
do { | ||
let amount: Amount | ||
switch unit { | ||
case .zil: | ||
amount = try Amount(zil: try Zil(trimming: incorrectDecimalSeparatorRemoved)) | ||
case .li: | ||
amount = try Amount(li: try Li(trimming: incorrectDecimalSeparatorRemoved)) | ||
case .qa: | ||
amount = try Amount(qa: try Qa(trimming: incorrectDecimalSeparatorRemoved)) | ||
} | ||
self = .amount(amount, in: unit) | ||
} catch { | ||
if let zilAmountError = error as? Zesame.AmountError<Amount>, zilAmountError == .endsWithDecimalSeparator { | ||
self = .string(incorrectDecimalSeparatorRemoved) | ||
} else if let zilError = error as? Zesame.AmountError<Zil>, zilError == .endsWithDecimalSeparator { | ||
self = .string(incorrectDecimalSeparatorRemoved) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
var display: String { | ||
switch self { | ||
case .amount(let amount, let unit): | ||
return AmountFormatter().format(amount: amount, in: unit) | ||
case .string(let string): | ||
return string | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.