Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.54 KB | None | 0 0
  1.  
  2. public struct PalletHelper {
  3.    
  4.     static func findClosestFactor(conversionFactors: [ConversionFactor], baseUnit: String) -> ConversionFactorUnit? {
  5.         let conversionUnitParser = CFUnitParser(conversionFactors)
  6.         return conversionUnitParser.convertFromPalle(to: baseUnit)
  7.     }
  8.    
  9.     static func findDenominatorByAmount(amount: Int, numerator: Double) -> Double {
  10.         let value: Double = Double(amount) / numerator
  11.         let denominator = value >= 1 ? value : 1
  12.         let remainder = denominator.remainder(dividingBy: 1)
  13.         return Double(denominator - remainder + ((remainder <= 0.30 || remainder >= 0.70) ? round(remainder) : 0.0))
  14.     }
  15.    
  16.     static func isWithinRange(amount: Int, convertedAmount: Double, conversionFactor: Double) -> Bool {
  17.         let baseValue: Double = Double(convertedAmount)
  18.         let numeratorFraction: Double = conversionFactor * 0.25
  19.         let lower = Int(floor(baseValue - numeratorFraction))
  20.         let upper = Int(ceil(baseValue + numeratorFraction))
  21.         return (lower..<upper).contains(amount)
  22.     }
  23.     static func isValidAmount(amount: Int, conversionFactorUnit: ConversionFactorUnit?) -> Bool {
  24.         guard let weight = conversionFactorUnit?.weight, amount != 1 else {return false}
  25.         let numerator = Double(weight)
  26.         let multipliedValue : Double = round((Double(amount) / numerator)) * numerator
  27.         if multipliedValue != floor(multipliedValue) || multipliedValue == 0 {
  28.             return false
  29.         }
  30.         return isWithinRange(amount: amount, convertedAmount : multipliedValue, conversionFactor: numerator) && amount != Int(multipliedValue)
  31.     }
  32.    
  33.     // If a pallet popup silence date has been set, then check if the 30 days have past
  34.     static func isPalletPopupIsSilenced() -> Bool {
  35.         guard let past = UserDefaultsHelper().getPalletPopupSilenceDate(), let diff = Calendar.current.dateComponents([.day], from: past, to: Date()).day else {
  36.             return false
  37.         }
  38.        
  39.         return (diff < 30)
  40.     }
  41. }
  42.  
  43. public enum PalletState {
  44.     case change(quantity: Int)
  45.     case insufficient
  46.     case close
  47. }
  48.  
  49. public extension UIViewController {
  50.     func showPalletPopup(_ conversionFactor: ConversionFactorUnit?, _ amount: Int, _ completion: @escaping ((PalletState) -> ())) {
  51.         guard let vc = PalletPopupConfig.regular(amount: amount, conversionFactor: conversionFactor, completion: completion) as? UIViewController & SheetControllerPresentable else { return }
  52.         if UIDevice.dlg_isPad() {
  53.             vc.modalPresentationStyle = .custom
  54.             present(vc, animated: true, completion: nil)
  55.         } else {
  56.             let sheetVC = SheetViewController(controller: vc, configuration: .init(canBeDismissed: false, showControllerTitle: false, dismissableButton: false, hiddenBar: true))
  57.             present(sheetVC, animated: true, completion: nil)
  58.         }
  59.     }
  60.    
  61.     func handlePallet(amount: Int, conversionFactors: [ConversionFactor], baseUnit: String, completion: @escaping ((PalletState) -> ())) {
  62.         if PalletHelper.isPalletPopupIsSilenced() {
  63.             completion(.close)
  64.             return
  65.         }
  66.        
  67.         let closestFactor = PalletHelper.findClosestFactor(conversionFactors: conversionFactors, baseUnit: baseUnit)
  68.         let valid = PalletHelper.isValidAmount(amount: amount, conversionFactorUnit: closestFactor)
  69.        
  70.         if valid {
  71.             showPalletPopup(closestFactor, amount, completion)
  72.         } else {
  73.             completion(.insufficient)
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement