SuspendResumeTableViewCell.swift 2.2 KB

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