CarbEntryValidationNavigationDelegate.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // CarbEntryNavigationDelegate.swift
  3. // LoopKit
  4. //
  5. // Created by Jaim Zuber on 2/7/17.
  6. // Copyright © 2017 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. public class CarbEntryNavigationDelegate {
  10. private lazy var validationTitle = LocalizedString("Warning", comment: "Title of an alert containing a validation warning")
  11. private lazy var dismissActionTitle = LocalizedString("com.loudnate.LoopKit.errorAlertActionTitle", value: "OK", comment: "The title of the action used to dismiss an error alert")
  12. public init() {}
  13. public func showAbsorptionTimeValidationWarning(for viewController: UIViewController, maxAbsorptionTime: TimeInterval) {
  14. let formatter = DateComponentsFormatter()
  15. formatter.allowedUnits = [.minute]
  16. formatter.unitsStyle = .full
  17. let message = String(
  18. format: LocalizedString("The maximum absorption time is %@", comment: "Alert body displayed absorption time greater than max (1: maximum absorption time)"),
  19. formatter.string(from: maxAbsorptionTime) ?? String(describing: maxAbsorptionTime))
  20. let alert = UIAlertController(title: validationTitle, message: message, preferredStyle: .alert)
  21. let action = UIAlertAction(title: dismissActionTitle, style: .default)
  22. alert.addAction(action)
  23. alert.preferredAction = action
  24. viewController.present(alert, animated: true)
  25. }
  26. public func showMaxQuantityValidationWarning(for viewController: UIViewController, maxQuantityGrams: Double) {
  27. let message = String(
  28. format: LocalizedString("The maximum allowed amount is %@ grams", comment: "Alert body displayed for quantity greater than max (1: maximum quantity in grams)"),
  29. NumberFormatter.localizedString(from: NSNumber(value: maxQuantityGrams), number: .none)
  30. )
  31. let alert = UIAlertController(title: validationTitle, message: message, preferredStyle: .alert)
  32. let action = UIAlertAction(title: dismissActionTitle, style: .default)
  33. alert.addAction(action)
  34. alert.preferredAction = action
  35. viewController.present(alert, animated: true)
  36. }
  37. }