WatchSettingsViewController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // WatchSettingsViewController.swift
  3. // LoopFollow
  4. //
  5. // Created by Jose Paredes on 7/16/20.
  6. // Copyright © 2020 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import Eureka
  10. import EventKit
  11. import EventKitUI
  12. class WatchSettingsViewController: FormViewController {
  13. var appStateController: AppStateController?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. if UserDefaultsRepository.forceDarkMode.value {
  17. overrideUserInterfaceStyle = .dark
  18. }
  19. buildWatchSettings()
  20. showHideNSDetails()
  21. }
  22. func showHideNSDetails() {
  23. var isHidden = false
  24. var isEnabled = true
  25. if UserDefaultsRepository.url.value == "" {
  26. isHidden = true
  27. isEnabled = false
  28. }
  29. let tmpArr = ["IOB", "COB", "BASAL", "LOOP", "OVERRIDE"]
  30. for i in 0..<tmpArr.count {
  31. if let row1 = form.rowBy(tag: tmpArr[i]) as? LabelRow {
  32. row1.hidden = .function(["hide"], {form in
  33. return isHidden
  34. })
  35. row1.evaluateHidden()
  36. }
  37. }
  38. }
  39. private func buildWatchSettings(){
  40. struct cal {
  41. var title: String
  42. var identifier: String
  43. }
  44. //array of calendars
  45. let store = EKEventStore()
  46. let ekCalendars = store.calendars(for: EKEntityType.event)
  47. var calendars: [cal] = []
  48. for i in 0..<ekCalendars.count{
  49. let item = cal(title: ekCalendars[i].title, identifier: ekCalendars[i].calendarIdentifier)
  50. calendars.append(item)
  51. }
  52. form
  53. +++ Section(header: "Save BG to Calendar", footer: "Add the Apple calendar complication to your Apple Watch face or Carplay to see BG readings. Create a new calendar called 'Follow' and modify the calendar settings in the iPhone Watch/Carplay App to only display the Follow calendar on your watch or car. It is important to use a new calendar because this will delete other events on the same calendar. Edit Line 1 and Line 2 to be displayed using variables below that will be replaced by the values. Other text entered will not be replaced")
  54. <<< SwitchRow("writeCalendarEvent"){ row in
  55. row.title = "Save BG to Calendar"
  56. row.value = UserDefaultsRepository.writeCalendarEvent.value
  57. }.onChange { [weak self] row in
  58. guard let value = row.value else { return }
  59. UserDefaultsRepository.writeCalendarEvent.value = value
  60. }
  61. <<< PickerInputRow<String>("calendarIdentifier") { row in
  62. row.title = "Calendar"
  63. row.options = calendars.map { $0.identifier }
  64. row.value = UserDefaultsRepository.calendarIdentifier.value
  65. row.displayValueFor = { value in
  66. guard let value = value else { return nil }
  67. let matching = calendars
  68. .flatMap { $0 }
  69. .filter { $0.identifier.range(of: value) != nil || $0.title.range(of: value) != nil }
  70. if matching.count > 0 {
  71. return "\(String(matching[0].title))"
  72. } else {
  73. return " - "
  74. }
  75. }
  76. }.onChange { [weak self] row in
  77. guard let value = row.value else { return }
  78. UserDefaultsRepository.calendarIdentifier.value = value
  79. }
  80. <<< TextRow("watchLine1"){ row in
  81. row.title = "Line 1"
  82. row.value = UserDefaultsRepository.watchLine1.value
  83. }.onChange { row in
  84. guard let value = row.value else { return }
  85. UserDefaultsRepository.watchLine1.value = value
  86. }
  87. <<< TextRow("watchLine2"){ row in
  88. row.title = "Line 2"
  89. row.value = UserDefaultsRepository.watchLine2.value
  90. }.onChange { row in
  91. guard let value = row.value else { return }
  92. UserDefaultsRepository.watchLine2.value = value
  93. }
  94. <<< SwitchRow("saveImage"){ row in
  95. row.title = "Save Graph Image for Watch Face"
  96. row.value = UserDefaultsRepository.saveImage.value
  97. }.onChange { [weak self] row in
  98. guard let value = row.value else { return }
  99. UserDefaultsRepository.saveImage.value = value
  100. }
  101. +++ Section(header: "Available Variables", footer: "")
  102. <<< LabelRow("BG"){ row in
  103. row.title = "%BG% : Blood Glucose Reading"
  104. }
  105. <<< LabelRow("DIRECTION"){ row in
  106. row.title = "%DIRECTION% : Dexcom Trend Arrow"
  107. }
  108. <<< LabelRow("DELTA"){ row in
  109. row.title = "%DELTA% : +/- From Last Reading"
  110. }
  111. <<< LabelRow("IOB"){ row in
  112. row.title = "%IOB% : Insulin on Board"
  113. }
  114. <<< LabelRow("COB"){ row in
  115. row.title = "%COB% : Carbs on Board"
  116. }
  117. <<< LabelRow("BASAL"){ row in
  118. row.title = "%BASAL% : Current Basal u/hr"
  119. }
  120. <<< LabelRow("LOOP"){ row in
  121. row.title = "%LOOP% : Loop Status Symbol"
  122. }
  123. <<< LabelRow("OVERRIDE"){ row in
  124. row.title = "%OVERRIDE% : Active Override %"
  125. }
  126. <<< LabelRow("MINAGO"){ row in
  127. row.title = "%MINAGO% : Only displays for old readings"
  128. }
  129. +++ ButtonRow() {
  130. $0.title = "DONE"
  131. }.onCellSelection { (row, arg) in
  132. self.dismiss(animated:true, completion: nil)
  133. }
  134. }
  135. }