RepeatingScheduleValueTableViewCell.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // RepeatingScheduleValueTableViewCell.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 2/6/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import UIKit
  9. protocol RepeatingScheduleValueTableViewCellDelegate: DatePickerTableViewCellDelegate {
  10. func repeatingScheduleValueTableViewCellDidUpdateValue(_ cell: RepeatingScheduleValueTableViewCell)
  11. }
  12. class RepeatingScheduleValueTableViewCell: DatePickerTableViewCell, UITextFieldDelegate {
  13. weak var delegate: RepeatingScheduleValueTableViewCellDelegate?
  14. var timeZone: TimeZone! {
  15. didSet {
  16. dateFormatter.timeZone = timeZone
  17. datePicker.timeZone = timeZone
  18. }
  19. }
  20. private lazy var dateFormatter: DateFormatter = {
  21. let dateFormatter = DateFormatter()
  22. dateFormatter.dateStyle = .none
  23. dateFormatter.timeStyle = .short
  24. return dateFormatter
  25. }()
  26. override func updateDateLabel() {
  27. dateLabel.text = dateFormatter.string(from: date)
  28. }
  29. override func dateChanged(_ sender: UIDatePicker) {
  30. super.dateChanged(sender)
  31. delegate?.datePickerTableViewCellDidUpdateDate(self)
  32. }
  33. var value: Double = 0 {
  34. didSet {
  35. textField.text = valueNumberFormatter.string(from: value)
  36. }
  37. }
  38. var datePickerInterval: TimeInterval {
  39. return TimeInterval(minutes: Double(datePicker.minuteInterval))
  40. }
  41. var isReadOnly = false {
  42. didSet {
  43. if isReadOnly, textField.isFirstResponder {
  44. textField.resignFirstResponder()
  45. }
  46. }
  47. }
  48. lazy var valueNumberFormatter: NumberFormatter = {
  49. let formatter = NumberFormatter()
  50. formatter.numberStyle = .decimal
  51. formatter.minimumFractionDigits = 1
  52. return formatter
  53. }()
  54. @IBOutlet weak var dateLabel: UILabel!
  55. @IBOutlet weak var unitLabel: UILabel! {
  56. didSet {
  57. // Setting this color in code because the nib isn't being applied correctly
  58. if #available(iOSApplicationExtension 13.0, *) {
  59. unitLabel.textColor = .secondaryLabel
  60. }
  61. }
  62. }
  63. @IBOutlet weak var textField: UITextField! {
  64. didSet {
  65. // Setting this color in code because the nib isn't being applied correctly
  66. if #available(iOSApplicationExtension 13.0, *) {
  67. textField.textColor = .label
  68. }
  69. }
  70. }
  71. var unitString: String? {
  72. get {
  73. return unitLabel.text
  74. }
  75. set {
  76. unitLabel.text = newValue
  77. }
  78. }
  79. // MARK: - UITextFieldDelegate
  80. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  81. return !isReadOnly
  82. }
  83. func textFieldDidBeginEditing(_ textField: UITextField) {
  84. DispatchQueue.main.async {
  85. textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
  86. }
  87. }
  88. func textFieldDidEndEditing(_ textField: UITextField) {
  89. value = valueNumberFormatter.number(from: textField.text ?? "")?.doubleValue ?? 0
  90. delegate?.repeatingScheduleValueTableViewCellDidUpdateValue(self)
  91. }
  92. }