|
| 1 | +// |
| 2 | +// NPDecimalConfig.swift |
| 3 | +// |
| 4 | +// Copyright 2023 OpenAlloc LLC |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +import Foundation |
| 20 | + |
| 21 | + |
| 22 | +public final class NPDecimalConfig: NPBaseConfig<Decimal> |
| 23 | +{ |
| 24 | + // MARK: - Parameters |
| 25 | + |
| 26 | + public let precision: Int |
| 27 | + |
| 28 | + public init(_ val: Decimal, |
| 29 | + precision: Int = NumberPadEnum.defaultPrecision, |
| 30 | + upperBound: Decimal = Decimal.greatestFiniteMagnitude) |
| 31 | + { |
| 32 | + self.precision = precision |
| 33 | + |
| 34 | + let formatter = { |
| 35 | + let nf = NumberFormatter() |
| 36 | + nf.locale = Locale.current |
| 37 | + nf.numberStyle = .decimal |
| 38 | + nf.usesGroupingSeparator = false |
| 39 | + nf.isLenient = true |
| 40 | + nf.minimumFractionDigits = 0 |
| 41 | + nf.maximumFractionDigits = precision |
| 42 | + nf.generatesDecimalNumbers = true |
| 43 | + return nf |
| 44 | + }() |
| 45 | + |
| 46 | + let sVal = Self.toString(val, upperBound: upperBound, formatter: formatter) |
| 47 | + |
| 48 | + super.init(sValue: sVal, upperBound: upperBound, formatter: formatter) |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: - Type-specific Actions |
| 52 | + |
| 53 | + override public var showDecimalPoint: Bool { precision > 0 } |
| 54 | + |
| 55 | + override public func decimalPointAction() -> Bool { |
| 56 | + guard decimalPointIndex == nil else { return false } |
| 57 | + sValue.append(".") |
| 58 | + |
| 59 | + return true |
| 60 | + } |
| 61 | + |
| 62 | + // MARK: - Internal |
| 63 | + |
| 64 | + internal static func toString(_ val: Decimal, upperBound: Decimal, formatter: NumberFormatter) -> String { |
| 65 | + let clampedValue = max(0, min(val, upperBound)) |
| 66 | + return formatter.string(from: clampedValue as NSDecimalNumber) ?? "0" |
| 67 | + } |
| 68 | + |
| 69 | + override internal func validateDigit(_: NumberPadEnum) -> Bool { |
| 70 | + let cp = currentPrecision |
| 71 | + if cp > 0, cp == precision { return false } // ignore additional input |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + override internal func toValue(_ str: String) -> Decimal? { |
| 76 | + guard let val: NSNumber = formatter.number(from: str) |
| 77 | + else { return nil } |
| 78 | + return Decimal(val.doubleValue) |
| 79 | + } |
| 80 | + |
| 81 | + internal var currentPrecision: Int { |
| 82 | + guard let di = decimalPointIndex else { return 0 } |
| 83 | + return sValue.distance(from: di, to: sValue.endIndex) - 1 |
| 84 | + } |
| 85 | + |
| 86 | + internal var decimalPointIndex: String.Index? { |
| 87 | + sValue.firstIndex(of: ".") |
| 88 | + } |
| 89 | +} |
0 commit comments