OverridePresetCollectionViewCell.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. insulinNeedsBar.isUserInteractionEnabled = false
  35. }
  36. }
  37. @IBOutlet private weak var durationStackView: UIStackView!
  38. @IBOutlet weak var durationLabel: UILabel!
  39. @IBOutlet weak var scheduleButton: UIButton!
  40. @IBOutlet private weak var editingIndicator: UIImageView! {
  41. didSet {
  42. editingIndicator.alpha = 0
  43. }
  44. }
  45. @IBOutlet private weak var deleteButton: UIButton! {
  46. didSet {
  47. deleteButton.layer.cornerRadius = 4
  48. }
  49. }
  50. @IBOutlet private weak var deleteButtonWidthConstraint: NSLayoutConstraint! {
  51. didSet {
  52. deleteButtonWidthConstraint.constant = 0
  53. }
  54. }
  55. weak var delegate: OverridePresetCollectionViewCellDelegate?
  56. private lazy var overlayDimmerView: UIView = {
  57. let view = UIView()
  58. if #available(iOSApplicationExtension 13.0, *) {
  59. view.backgroundColor = .systemBackground
  60. } else {
  61. view.backgroundColor = .white
  62. }
  63. view.alpha = 0
  64. view.translatesAutoresizingMaskIntoConstraints = false
  65. return view
  66. }()
  67. override func awakeFromNib() {
  68. super.awakeFromNib()
  69. let selectedBackgroundView = UIView()
  70. self.selectedBackgroundView = selectedBackgroundView
  71. if #available(iOSApplicationExtension 13.0, iOS 13.0, *) {
  72. selectedBackgroundView.backgroundColor = .tertiarySystemFill
  73. backgroundColor = .secondarySystemGroupedBackground
  74. layer.cornerCurve = .continuous
  75. } else {
  76. selectedBackgroundView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
  77. backgroundColor = .white
  78. }
  79. layer.cornerRadius = 16
  80. scheduleButton.addTarget(self, action: #selector(scheduleButtonTapped), for: .touchUpInside)
  81. addSubview(overlayDimmerView)
  82. NSLayoutConstraint.activate([
  83. overlayDimmerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  84. overlayDimmerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  85. overlayDimmerView.topAnchor.constraint(equalTo: topAnchor),
  86. overlayDimmerView.bottomAnchor.constraint(equalTo: bottomAnchor)
  87. ])
  88. }
  89. override func prepareForReuse() {
  90. startTimeLabel.text?.removeAll()
  91. targetRangeLabel.isHidden = false
  92. insulinNeedsBar.isHidden = false
  93. configureForStandard(animated: false)
  94. removeOverlay(animated: false)
  95. }
  96. func configureForEditing(animated: Bool) {
  97. func makeVisualChanges() {
  98. durationStackView.alpha = 0
  99. scheduleButton.alpha = 0
  100. editingIndicator.alpha = 1
  101. deleteButtonWidthConstraint.constant = 32
  102. if #available(iOSApplicationExtension 13.0, *) {
  103. deleteButton.setImage(UIImage(systemName: "xmark")!, for: .normal)
  104. }
  105. deleteButton.setTitle(nil, for: .normal)
  106. }
  107. if animated {
  108. UIView.animate(withDuration: 0.3, animations: {
  109. makeVisualChanges()
  110. self.layoutIfNeeded()
  111. })
  112. } else {
  113. makeVisualChanges()
  114. }
  115. isShowingFinalDeleteConfirmation = false
  116. }
  117. func configureForStandard(animated: Bool) {
  118. func makeVisualChanges() {
  119. durationStackView.alpha = 1
  120. scheduleButton.alpha = 1
  121. editingIndicator.alpha = 0
  122. deleteButtonWidthConstraint.constant = 0
  123. if #available(iOSApplicationExtension 13.0, *) {
  124. deleteButton.setImage(UIImage(systemName: "xmark")!, for: .normal)
  125. }
  126. deleteButton.setTitle(nil, for: .normal)
  127. }
  128. if animated {
  129. UIView.animate(withDuration: 0.3, animations: {
  130. makeVisualChanges()
  131. self.layoutIfNeeded()
  132. })
  133. } else {
  134. makeVisualChanges()
  135. }
  136. isShowingFinalDeleteConfirmation = false
  137. }
  138. func applyOverlayToFade(animated: Bool) {
  139. if animated {
  140. UIView.animate(withDuration: 0.3, animations: {
  141. self.overlayDimmerView.alpha = 0.5
  142. })
  143. } else {
  144. self.overlayDimmerView.alpha = 0.5
  145. }
  146. }
  147. func removeOverlay(animated: Bool) {
  148. if animated {
  149. UIView.animate(withDuration: 0.3, animations: {
  150. self.overlayDimmerView.alpha = 0
  151. })
  152. } else {
  153. self.overlayDimmerView.alpha = 0
  154. }
  155. }
  156. @objc private func scheduleButtonTapped() {
  157. delegate?.overridePresetCollectionViewCellDidScheduleOverride(self)
  158. }
  159. private(set) var isShowingFinalDeleteConfirmation = false
  160. @IBAction private func deleteButtonTapped(_ sender: UIButton) {
  161. if isShowingFinalDeleteConfirmation {
  162. delegate?.overridePresetCollectionViewCellDidDeletePreset(self)
  163. } else {
  164. UIView.animate(withDuration: 0.3, animations: {
  165. self.deleteButton.setImage(nil, for: .normal)
  166. self.deleteButton.setTitle("Delete", for: .normal)
  167. self.deleteButtonWidthConstraint.constant = 72
  168. self.layoutIfNeeded()
  169. })
  170. isShowingFinalDeleteConfirmation = true
  171. delegate?.overridePresetCollectionViewCellDidPerformFirstDeletionStep(self)
  172. }
  173. }
  174. }