SuspendResumeTableViewCell.swift 2.5 KB

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