Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct PalletHelper {
- static func findClosestFactor(conversionFactors: [ConversionFactor], baseUnit: String) -> ConversionFactorUnit? {
- let conversionUnitParser = CFUnitParser(conversionFactors)
- return conversionUnitParser.convertFromPalle(to: baseUnit)
- }
- static func findDenominatorByAmount(amount: Int, numerator: Double) -> Double {
- let value: Double = Double(amount) / numerator
- let denominator = value >= 1 ? value : 1
- let remainder = denominator.remainder(dividingBy: 1)
- return Double(denominator - remainder + ((remainder <= 0.30 || remainder >= 0.70) ? round(remainder) : 0.0))
- }
- static func isWithinRange(amount: Int, convertedAmount: Double, conversionFactor: Double) -> Bool {
- let baseValue: Double = Double(convertedAmount)
- let numeratorFraction: Double = conversionFactor * 0.25
- let lower = Int(floor(baseValue - numeratorFraction))
- let upper = Int(ceil(baseValue + numeratorFraction))
- return (lower..<upper).contains(amount)
- }
- static func isValidAmount(amount: Int, conversionFactorUnit: ConversionFactorUnit?) -> Bool {
- guard let weight = conversionFactorUnit?.weight, amount != 1 else {return false}
- let numerator = Double(weight)
- let multipliedValue : Double = round((Double(amount) / numerator)) * numerator
- if multipliedValue != floor(multipliedValue) || multipliedValue == 0 {
- return false
- }
- return isWithinRange(amount: amount, convertedAmount : multipliedValue, conversionFactor: numerator) && amount != Int(multipliedValue)
- }
- // If a pallet popup silence date has been set, then check if the 30 days have past
- static func isPalletPopupIsSilenced() -> Bool {
- guard let past = UserDefaultsHelper().getPalletPopupSilenceDate(), let diff = Calendar.current.dateComponents([.day], from: past, to: Date()).day else {
- return false
- }
- return (diff < 30)
- }
- }
- public enum PalletState {
- case change(quantity: Int)
- case insufficient
- case close
- }
- public extension UIViewController {
- func showPalletPopup(_ conversionFactor: ConversionFactorUnit?, _ amount: Int, _ completion: @escaping ((PalletState) -> ())) {
- guard let vc = PalletPopupConfig.regular(amount: amount, conversionFactor: conversionFactor, completion: completion) as? UIViewController & SheetControllerPresentable else { return }
- if UIDevice.dlg_isPad() {
- vc.modalPresentationStyle = .custom
- present(vc, animated: true, completion: nil)
- } else {
- let sheetVC = SheetViewController(controller: vc, configuration: .init(canBeDismissed: false, showControllerTitle: false, dismissableButton: false, hiddenBar: true))
- present(sheetVC, animated: true, completion: nil)
- }
- }
- func handlePallet(amount: Int, conversionFactors: [ConversionFactor], baseUnit: String, completion: @escaping ((PalletState) -> ())) {
- if PalletHelper.isPalletPopupIsSilenced() {
- completion(.close)
- return
- }
- let closestFactor = PalletHelper.findClosestFactor(conversionFactors: conversionFactors, baseUnit: baseUnit)
- let valid = PalletHelper.isValidAmount(amount: amount, conversionFactorUnit: closestFactor)
- if valid {
- showPalletPopup(closestFactor, amount, completion)
- } else {
- completion(.insufficient)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement