SuspendResumeTableViewCell.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // SuspendResumeTableViewCell.swift
  3. // LoopKitUI
  4. //
  5. // Created by Pete Schwamb on 11/16/18.
  6. // Copyright © 2018 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import LoopKit
  10. public class SuspendResumeTableViewCell: TextButtonTableViewCell {
  11. public enum Action {
  12. case suspend
  13. case resume
  14. case inoperable
  15. }
  16. public var shownAction: Action {
  17. switch basalDeliveryState {
  18. case .active, .suspending, .tempBasal, .cancelingTempBasal, .initiatingTempBasal:
  19. return .suspend
  20. case .suspended, .resuming:
  21. return .resume
  22. case .none:
  23. return .inoperable
  24. }
  25. }
  26. private func updateTextLabel() {
  27. switch self.basalDeliveryState {
  28. case .active, .tempBasal:
  29. textLabel?.text = LocalizedString("Suspend Delivery", comment: "Title text for button to suspend insulin delivery")
  30. case .suspending:
  31. self.textLabel?.text = LocalizedString("Suspending", comment: "Title text for button when insulin delivery is in the process of being stopped")
  32. case .suspended:
  33. textLabel?.text = LocalizedString("Resume Delivery", comment: "Title text for button to resume insulin delivery")
  34. case .resuming:
  35. self.textLabel?.text = LocalizedString("Resuming", comment: "Title text for button when insulin delivery is in the process of being resumed")
  36. case .initiatingTempBasal:
  37. self.textLabel?.text = LocalizedString("Starting Temp Basal", comment: "Title text for suspend resume button when temp basal starting")
  38. case .cancelingTempBasal:
  39. self.textLabel?.text = LocalizedString("Canceling Temp Basal", comment: "Title text for suspend resume button when temp basal canceling")
  40. case .none:
  41. self.textLabel?.text = LocalizedString("Pump Inoperable", comment: "Title text for suspend resume button when the basal delivery state is not set")
  42. }
  43. }
  44. private func updateLoadingState() {
  45. self.isLoading = {
  46. switch self.basalDeliveryState {
  47. case .suspending, .resuming, .initiatingTempBasal, .cancelingTempBasal:
  48. return true
  49. default:
  50. return false
  51. }
  52. }()
  53. self.isEnabled = !self.isLoading
  54. }
  55. public var basalDeliveryState: PumpManagerStatus.BasalDeliveryState? = .active(Date()) {
  56. didSet {
  57. updateTextLabel()
  58. updateLoadingState()
  59. }
  60. }
  61. }