ScheduleEditor.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //
  2. // ScheduleEditor.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 4/24/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import HealthKit
  10. import LoopKit
  11. enum SavingMechanism<Value> {
  12. case synchronous((_ value: Value) -> Void)
  13. case asynchronous((_ value: Value, _ completion: @escaping (Error?) -> Void) -> Void)
  14. func pullback<NewValue>(_ transform: @escaping (NewValue) -> Value) -> SavingMechanism<NewValue> {
  15. switch self {
  16. case .synchronous(let save):
  17. return .synchronous { newValue in save(transform(newValue)) }
  18. case .asynchronous(let save):
  19. return .asynchronous { newValue, completion in
  20. save(transform(newValue), completion)
  21. }
  22. }
  23. }
  24. }
  25. enum SaveConfirmation {
  26. case required(AlertContent)
  27. case notRequired
  28. }
  29. struct ScheduleEditor<Value: Equatable, ValueContent: View, ValuePicker: View, ActionAreaContent: View>: View {
  30. fileprivate enum PresentedAlert {
  31. case saveConfirmation(AlertContent)
  32. case saveError(Error)
  33. }
  34. var title: Text
  35. var description: Text
  36. var initialScheduleItems: [RepeatingScheduleValue<Value>]
  37. @Binding var scheduleItems: [RepeatingScheduleValue<Value>]
  38. var defaultFirstScheduleItemValue: Value
  39. var scheduleItemLimit: Int
  40. var saveConfirmation: SaveConfirmation
  41. var valueContent: (_ value: Value, _ isEditing: Bool) -> ValueContent
  42. var valuePicker: (_ item: Binding<RepeatingScheduleValue<Value>>, _ availableWidth: CGFloat) -> ValuePicker
  43. var actionAreaContent: ActionAreaContent
  44. var savingMechanism: SavingMechanism<[RepeatingScheduleValue<Value>]>
  45. var mode: SettingsPresentationMode
  46. var therapySettingType: TherapySetting
  47. @State var editingIndex: Int?
  48. @State var isAddingNewItem = false {
  49. didSet {
  50. if isAddingNewItem {
  51. editingIndex = nil
  52. }
  53. }
  54. }
  55. @State var tableDeletionState: TableDeletionState = .disabled {
  56. didSet {
  57. if tableDeletionState == .enabled {
  58. editingIndex = nil
  59. }
  60. }
  61. }
  62. @State var isSyncing = false
  63. @State private var presentedAlert: PresentedAlert?
  64. @Environment(\.dismiss) var dismiss
  65. @Environment(\.authenticate) var authenticate
  66. init(
  67. title: Text,
  68. description: Text,
  69. scheduleItems: Binding<[RepeatingScheduleValue<Value>]>,
  70. initialScheduleItems: [RepeatingScheduleValue<Value>],
  71. defaultFirstScheduleItemValue: Value,
  72. scheduleItemLimit: Int = 48,
  73. saveConfirmation: SaveConfirmation,
  74. @ViewBuilder valueContent: @escaping (_ value: Value, _ isEditing: Bool) -> ValueContent,
  75. @ViewBuilder valuePicker: @escaping (_ item: Binding<RepeatingScheduleValue<Value>>, _ availableWidth: CGFloat) -> ValuePicker,
  76. @ViewBuilder actionAreaContent: () -> ActionAreaContent,
  77. savingMechanism: SavingMechanism<[RepeatingScheduleValue<Value>]>,
  78. mode: SettingsPresentationMode = .settings,
  79. therapySettingType: TherapySetting = .none
  80. ) {
  81. self.title = title
  82. self.description = description
  83. self.initialScheduleItems = initialScheduleItems
  84. self._scheduleItems = scheduleItems
  85. self.defaultFirstScheduleItemValue = defaultFirstScheduleItemValue
  86. self.scheduleItemLimit = scheduleItemLimit
  87. self.saveConfirmation = saveConfirmation
  88. self.valueContent = valueContent
  89. self.valuePicker = valuePicker
  90. self.actionAreaContent = actionAreaContent()
  91. self.savingMechanism = savingMechanism
  92. self.mode = mode
  93. self.therapySettingType = therapySettingType
  94. }
  95. var body: some View {
  96. ZStack {
  97. configurationPage
  98. .disabled(isSyncing || isAddingNewItem)
  99. .zIndex(0)
  100. if isAddingNewItem {
  101. DarkenedOverlay()
  102. .zIndex(1)
  103. NewScheduleItemEditor(
  104. isPresented: $isAddingNewItem,
  105. initialItem: initialNewScheduleItem,
  106. selectableTimes: scheduleItems.isEmpty
  107. ? .only(.hours(0))
  108. : .allExcept(Set(scheduleItems.map { $0.startTime })),
  109. valuePicker: valuePicker,
  110. onSave: { newItem in
  111. self.scheduleItems.append(newItem)
  112. self.scheduleItems.sort(by: { $0.startTime < $1.startTime })
  113. }
  114. )
  115. .transition(
  116. AnyTransition
  117. .move(edge: .bottom)
  118. .combined(with: .opacity)
  119. )
  120. .zIndex(2)
  121. }
  122. }
  123. }
  124. private var configurationPage: some View {
  125. switch mode {
  126. case .acceptanceFlow:
  127. return AnyView(page)
  128. case .settings:
  129. return AnyView(pageWithCancel)
  130. }
  131. }
  132. private var pageWithCancel: some View {
  133. switch saveButtonState {
  134. case .disabled, .loading:
  135. return AnyView(page
  136. .navigationBarBackButtonHidden(false)
  137. .navigationBarItems(leading: EmptyView(), trailing: trailingNavigationItems)
  138. )
  139. case .enabled:
  140. return AnyView(page
  141. .navigationBarBackButtonHidden(true)
  142. .navigationBarItems(leading: cancelButton, trailing: trailingNavigationItems)
  143. )
  144. }
  145. }
  146. private var page: some View {
  147. ConfigurationPage(
  148. title: title,
  149. actionButtonTitle: Text(mode.buttonText),
  150. actionButtonState: saveButtonState,
  151. cards: {
  152. // TODO: Remove conditional when Swift 5.3 ships
  153. // https://bugs.swift.org/browse/SR-11628
  154. if true {
  155. Card {
  156. SettingDescription(text: description, informationalContent: {self.therapySettingType.helpScreen()})
  157. Splat(Array(scheduleItems.enumerated()), id: \.element.startTime) { index, item in
  158. self.itemView(for: item, at: index)
  159. }
  160. }
  161. }
  162. },
  163. actionAreaContent: {
  164. actionAreaContent
  165. },
  166. action: {
  167. switch self.saveConfirmation {
  168. case .required(let alertContent):
  169. self.presentedAlert = .saveConfirmation(alertContent)
  170. case .notRequired:
  171. self.startSaving()
  172. }
  173. }
  174. )
  175. .alert(item: $presentedAlert, content: alert(for:))
  176. .navigationBarTitle("", displayMode: .inline)
  177. .navigationBarItems(
  178. trailing: trailingNavigationItems
  179. )
  180. }
  181. private var saveButtonState: ConfigurationPageActionButtonState {
  182. if isSyncing {
  183. return .loading
  184. }
  185. let isEnabled = !scheduleItems.isEmpty
  186. && (scheduleItems != initialScheduleItems || mode == .acceptanceFlow)
  187. && tableDeletionState == .disabled
  188. return isEnabled ? .enabled : .disabled
  189. }
  190. private func itemView(for item: RepeatingScheduleValue<Value>, at index: Int) -> some View {
  191. Deletable(
  192. tableDeletionState: $tableDeletionState,
  193. index: index,
  194. isDeletable: index != 0,
  195. onDelete: {
  196. withAnimation {
  197. self.scheduleItems.remove(at: index)
  198. if self.scheduleItems.count == 1 {
  199. self.tableDeletionState = .disabled
  200. }
  201. }
  202. }
  203. ) {
  204. ScheduleItemView(
  205. time: item.startTime,
  206. isEditing: isEditing(index),
  207. valueContent: {
  208. valueContent(item.value, isEditing(index).wrappedValue)
  209. },
  210. expandedContent: {
  211. ScheduleItemPicker(
  212. item: $scheduleItems[index],
  213. isTimeSelectable: { self.isTimeSelectable($0, at: index) },
  214. valuePicker: { self.valuePicker(self.$scheduleItems[index], $0) }
  215. )
  216. }
  217. )
  218. }
  219. .accessibility(identifier: "schedule_item_\(index)")
  220. }
  221. private func isEditing(_ index: Int) -> Binding<Bool> {
  222. Binding(
  223. get: { index == self.editingIndex },
  224. set: { isNowEditing in
  225. self.editingIndex = isNowEditing ? index : nil
  226. }
  227. )
  228. }
  229. private func isTimeSelectable(_ time: TimeInterval, at index: Int) -> Bool {
  230. if index == scheduleItems.startIndex {
  231. return time == .hours(0)
  232. }
  233. let priorTime = scheduleItems[index - 1].startTime
  234. guard time > priorTime else {
  235. return false
  236. }
  237. if index < scheduleItems.endIndex - 1 {
  238. let nextTime = scheduleItems[index + 1].startTime
  239. guard time < nextTime else {
  240. return false
  241. }
  242. }
  243. return true
  244. }
  245. private var initialNewScheduleItem: RepeatingScheduleValue<Value> {
  246. assert(scheduleItems.count <= scheduleItemLimit)
  247. if scheduleItems.isEmpty {
  248. return RepeatingScheduleValue(startTime: .hours(0), value: defaultFirstScheduleItemValue)
  249. }
  250. if scheduleItems.last!.startTime == .hours(23.5) {
  251. let firstItemFollowedByOpening = scheduleItems.adjacentPairs().first(where: { item, next in
  252. next.startTime - item.startTime > .minutes(30)
  253. })!.0
  254. return RepeatingScheduleValue(
  255. startTime: firstItemFollowedByOpening.startTime + .minutes(30),
  256. value: firstItemFollowedByOpening.value
  257. )
  258. } else {
  259. return RepeatingScheduleValue(
  260. startTime: scheduleItems.last!.startTime + .minutes(30),
  261. value: scheduleItems.last!.value
  262. )
  263. }
  264. }
  265. private var trailingNavigationItems: some View {
  266. // TODO: SwiftUI's alignment of these buttons in the navigation bar is a little funky.
  267. // Tapping 'Edit' then 'Done' can shift '+' slightly.
  268. HStack(spacing: 24) {
  269. if tableDeletionState == .disabled {
  270. editButton
  271. } else {
  272. doneButton
  273. }
  274. addButton
  275. }
  276. }
  277. var cancelButton: some View {
  278. Button(action: { self.dismiss() } ) { Text(LocalizedString("Cancel", comment: "Cancel editing settings button title")) }
  279. }
  280. var editButton: some View {
  281. Button(
  282. action: {
  283. withAnimation {
  284. self.tableDeletionState = .enabled
  285. }
  286. },
  287. label: {
  288. Text(LocalizedString("Edit", comment: "Text for edit button"))
  289. }
  290. )
  291. .disabled(scheduleItems.count == 1)
  292. }
  293. var doneButton: some View {
  294. Button(
  295. action: {
  296. withAnimation {
  297. self.tableDeletionState = .disabled
  298. }
  299. },
  300. label: {
  301. Text(LocalizedString("Done", comment: "Text for done button")).bold()
  302. }
  303. )
  304. }
  305. var addButton: some View {
  306. Button(
  307. action: {
  308. withAnimation {
  309. self.isAddingNewItem = true
  310. }
  311. },
  312. label: {
  313. Image(systemName: "plus")
  314. .imageScale(.large)
  315. .contentShape(Rectangle())
  316. }
  317. )
  318. .disabled(tableDeletionState != .disabled || scheduleItems.count >= scheduleItemLimit)
  319. }
  320. private func startSaving() {
  321. guard mode == .settings else {
  322. self.continueSaving()
  323. return
  324. }
  325. authenticate(therapySettingType.authenticationChallengeDescription) {
  326. switch $0 {
  327. case .success: self.continueSaving()
  328. case .failure: break
  329. }
  330. }
  331. }
  332. private func continueSaving() {
  333. switch savingMechanism {
  334. case .synchronous(let save):
  335. save(scheduleItems)
  336. case .asynchronous(let save):
  337. withAnimation {
  338. self.editingIndex = nil
  339. self.isSyncing = true
  340. }
  341. save(scheduleItems) { error in
  342. DispatchQueue.main.async {
  343. if let error = error {
  344. withAnimation {
  345. self.isSyncing = false
  346. }
  347. self.presentedAlert = .saveError(error)
  348. }
  349. }
  350. }
  351. }
  352. }
  353. private func alert(for presentedAlert: PresentedAlert) -> SwiftUI.Alert {
  354. switch presentedAlert {
  355. case .saveConfirmation(let content):
  356. return Alert(
  357. title: content.title,
  358. message: content.message,
  359. primaryButton: .cancel(Text(LocalizedString("Go Back", comment: "Button text to return to editing a schedule after from alert popup when some schedule values are outside the recommended range"))),
  360. secondaryButton: .default(
  361. Text(LocalizedString("Continue", comment: "Button text to confirm saving from alert popup when some schedule values are outside the recommended range")),
  362. action: startSaving
  363. )
  364. )
  365. case .saveError(let error):
  366. return Alert(
  367. title: Text(LocalizedString("Unable to Save", comment: "Alert title when error occurs while saving a schedule")),
  368. message: Text(error.localizedDescription)
  369. )
  370. }
  371. }
  372. }
  373. struct DarkenedOverlay: View {
  374. var body: some View {
  375. Rectangle()
  376. .fill(Color.black.opacity(0.3))
  377. .edgesIgnoringSafeArea(.all)
  378. }
  379. }
  380. extension ScheduleEditor.PresentedAlert: Identifiable {
  381. var id: Int {
  382. switch self {
  383. case .saveConfirmation:
  384. return 0
  385. case .saveError:
  386. return 1
  387. }
  388. }
  389. }