OverridePresetCollectionViewCell.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // OverridePresetCollectionViewCell.swift
  3. // Loop
  4. //
  5. // Created by Michael Pangburn on 1/2/19.
  6. // Copyright © 2019 LoopKit Authors. All rights reserved.
  7. //
  8. import UIKit
  9. protocol OverridePresetCollectionViewCellDelegate: AnyObject {
  10. func overridePresetCollectionViewCellDidScheduleOverride(_ cell: OverridePresetCollectionViewCell)
  11. func overridePresetCollectionViewCellDidPerformFirstDeletionStep(_ cell: OverridePresetCollectionViewCell)
  12. func overridePresetCollectionViewCellDidDeletePreset(_ cell: OverridePresetCollectionViewCell)
  13. }
  14. final class OverridePresetCollectionViewCell: UICollectionViewCell, IdentifiableClass {
  15. @IBOutlet weak var symbolLabel: UILabel!
  16. @IBOutlet weak var startTimeLabel: UILabel! {
  17. didSet {
  18. startTimeLabel.text?.removeAll()
  19. }
  20. }
  21. @IBOutlet weak var nameLabel: UILabel!
  22. @IBOutlet weak var targetRangeLabel: UILabel! {
  23. didSet {
  24. targetRangeLabel.text?.removeAll()
  25. }
  26. }
  27. @IBOutlet weak var insulinNeedsBar: SegmentedGaugeBarView! {
  28. didSet {
  29. if #available(iOSApplicationExtension 13.0, *) {
  30. insulinNeedsBar.backgroundColor = .systemGray6
  31. } else {
  32. insulinNeedsBar.backgroundColor = .white
  33. }
  34. }
  35. }
  36. @IBOutlet private weak var durationStackView: UIStackView!
  37. @IBOutlet weak var durationLabel: UILabel!
  38. @IBOutlet weak var scheduleButton: UIButton!
  39. @IBOutlet private weak var editingIndicator: UIImageView! {
  40. didSet {
  41. editingIndicator.alpha = 0
  42. }
  43. }
  44. @IBOutlet private weak var deleteButton: UIButton! {
  45. didSet {
  46. deleteButton.layer.cornerRadius = 4
  47. }
  48. }
  49. @IBOutlet private weak var deleteButtonWidthConstraint: NSLayoutConstraint! {
  50. didSet {
  51. deleteButtonWidthConstraint.constant = 0
  52. }
  53. }
  54. weak var delegate: OverridePresetCollectionViewCellDelegate?
  55. private lazy var overlayDimmerView: UIView = {
  56. let view = UIView()
  57. if #available(iOSApplicationExtension 13.0, *) {
  58. view.backgroundColor = .systemBackground
  59. } else {
  60. view.backgroundColor = .white
  61. }
  62. view.alpha = 0
  63. view.translatesAutoresizingMaskIntoConstraints = false
  64. return view
  65. }()
  66. override func awakeFromNib() {
  67. super.awakeFromNib()
  68. let selectedBackgroundView = UIView()
  69. self.selectedBackgroundView = selectedBackgroundView
  70. if #available(iOSApplicationExtension 13.0, iOS 13.0, *) {
  71. selectedBackgroundView.backgroundColor = .tertiarySystemFill
  72. backgroundColor = .secondarySystemGroupedBackground
  73. layer.cornerCurve = .continuous
  74. } else {
  75. selectedBackgroundView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
  76. backgroundColor = .white
  77. }
  78. layer.cornerRadius = 16
  79. scheduleButton.addTarget(self, action: #selector(scheduleButtonTapped), for: .touchUpInside)
  80. addSubview(overlayDimmerView)
  81. NSLayoutConstraint.activate([
  82. overlayDimmerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  83. overlayDimmerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  84. overlayDimmerView.topAnchor.constraint(equalTo: topAnchor),
  85. overlayDimmerView.bottomAnchor.constraint(equalTo: bottomAnchor)
  86. ])
  87. }
  88. override func prepareForReuse() {
  89. startTimeLabel.text?.removeAll()
  90. targetRangeLabel.isHidden = false
  91. insulinNeedsBar.isHidden = false
  92. configureForStandard(animated: false)
  93. removeOverlay(animated: false)
  94. }
  95. func configureForEditing(animated: Bool) {
  96. func makeVisualChanges() {
  97. durationStackView.alpha = 0
  98. scheduleButton.alpha = 0
  99. editingIndicator.alpha = 1
  100. deleteButtonWidthConstraint.constant = 32
  101. if #available(iOSApplicationExtension 13.0, *) {
  102. deleteButton.setImage(UIImage(systemName: "xmark")!, for: .normal)
  103. }
  104. deleteButton.setTitle(nil, for: .normal)
  105. }
  106. if animated {
  107. UIView.animate(withDuration: 0.3, animations: {
  108. makeVisualChanges()
  109. self.layoutIfNeeded()
  110. })
  111. } else {
  112. makeVisualChanges()
  113. }
  114. isShowingFinalDeleteConfirmation = false
  115. }
  116. func configureForStandard(animated: Bool) {
  117. func makeVisualChanges() {
  118. durationStackView.alpha = 1
  119. scheduleButton.alpha = 1
  120. editingIndicator.alpha = 0
  121. deleteButtonWidthConstraint.constant = 0
  122. if #available(iOSApplicationExtension 13.0, *) {
  123. deleteButton.setImage(UIImage(systemName: "xmark")!, for: .normal)
  124. }
  125. deleteButton.setTitle(nil, for: .normal)
  126. }
  127. if animated {
  128. UIView.animate(withDuration: 0.3, animations: {
  129. makeVisualChanges()
  130. self.layoutIfNeeded()
  131. })
  132. } else {
  133. makeVisualChanges()
  134. }
  135. isShowingFinalDeleteConfirmation = false
  136. }
  137. func applyOverlayToFade(animated: Bool) {
  138. if animated {
  139. UIView.animate(withDuration: 0.3, animations: {
  140. self.overlayDimmerView.alpha = 0.5
  141. })
  142. } else {
  143. self.overlayDimmerView.alpha = 0.5
  144. }
  145. }
  146. func removeOverlay(animated: Bool) {
  147. if animated {
  148. UIView.animate(withDuration: 0.3, animations: {
  149. self.overlayDimmerView.alpha = 0
  150. })
  151. } else {
  152. self.overlayDimmerView.alpha = 0
  153. }
  154. }
  155. @objc private func scheduleButtonTapped() {
  156. delegate?.overridePresetCollectionViewCellDidScheduleOverride(self)
  157. }
  158. private(set) var isShowingFinalDeleteConfirmation = false
  159. @IBAction private func deleteButtonTapped(_ sender: UIButton) {
  160. if isShowingFinalDeleteConfirmation {
  161. delegate?.overridePresetCollectionViewCellDidDeletePreset(self)
  162. } else {
  163. UIView.animate(withDuration: 0.3, animations: {
  164. self.deleteButton.setImage(nil, for: .normal)
  165. self.deleteButton.setTitle("Delete", for: .normal)
  166. self.deleteButtonWidthConstraint.constant = 72
  167. self.layoutIfNeeded()
  168. })
  169. isShowingFinalDeleteConfirmation = true
  170. delegate?.overridePresetCollectionViewCellDidPerformFirstDeletionStep(self)
  171. }
  172. }
  173. }