ScheduleEditor.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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(\.dismissAction) var dismiss
  65. @Environment(\.authenticate) var authenticate
  66. @Environment(\.presentationMode) var presentationMode
  67. init(
  68. title: Text,
  69. description: Text,
  70. scheduleItems: Binding<[RepeatingScheduleValue<Value>]>,
  71. initialScheduleItems: [RepeatingScheduleValue<Value>],
  72. defaultFirstScheduleItemValue: Value,
  73. scheduleItemLimit: Int = 48,
  74. saveConfirmation: SaveConfirmation,
  75. @ViewBuilder valueContent: @escaping (_ value: Value, _ isEditing: Bool) -> ValueContent,
  76. @ViewBuilder valuePicker: @escaping (_ item: Binding<RepeatingScheduleValue<Value>>, _ availableWidth: CGFloat) -> ValuePicker,
  77. @ViewBuilder actionAreaContent: () -> ActionAreaContent,
  78. savingMechanism: SavingMechanism<[RepeatingScheduleValue<Value>]>,
  79. mode: SettingsPresentationMode = .settings,
  80. therapySettingType: TherapySetting = .none
  81. ) {
  82. self.title = title
  83. self.description = description
  84. self.initialScheduleItems = initialScheduleItems
  85. self._scheduleItems = scheduleItems
  86. self.defaultFirstScheduleItemValue = defaultFirstScheduleItemValue
  87. self.scheduleItemLimit = scheduleItemLimit
  88. self.saveConfirmation = saveConfirmation
  89. self.valueContent = valueContent
  90. self.valuePicker = valuePicker
  91. self.actionAreaContent = actionAreaContent()
  92. self.savingMechanism = savingMechanism
  93. self.mode = mode
  94. self.therapySettingType = therapySettingType
  95. }
  96. var body: some View {
  97. ZStack {
  98. configurationPage
  99. .disabled(isSyncing || isAddingNewItem)
  100. .zIndex(0)
  101. if isAddingNewItem {
  102. DarkenedOverlay()
  103. .zIndex(1)
  104. NewScheduleItemEditor(
  105. isPresented: $isAddingNewItem,
  106. initialItem: initialNewScheduleItem,
  107. selectableTimes: scheduleItems.isEmpty
  108. ? .only(.hours(0))
  109. : .allExcept(Set(scheduleItems.map { $0.startTime })),
  110. valuePicker: valuePicker,
  111. onSave: { newItem in
  112. self.scheduleItems.append(newItem)
  113. self.scheduleItems.sort(by: { $0.startTime < $1.startTime })
  114. }
  115. )
  116. .transition(
  117. AnyTransition
  118. .move(edge: .bottom)
  119. .combined(with: .opacity)
  120. )
  121. .zIndex(2)
  122. }
  123. }
  124. }
  125. @ViewBuilder
  126. private var configurationPage: some View {
  127. switch mode {
  128. case .acceptanceFlow:
  129. page
  130. .navigationBarBackButtonHidden(true)
  131. .toolbar {
  132. ToolbarItem(placement: .navigationBarLeading) {
  133. backButton
  134. }
  135. ToolbarItem(placement: .navigationBarTrailing) {
  136. trailingNavigationItems
  137. }
  138. }
  139. case .settings:
  140. page
  141. .navigationBarBackButtonHidden(shouldAddCancelButton)
  142. .toolbar {
  143. ToolbarItem(placement: .navigationBarLeading) {
  144. leadingNavigationBarItem
  145. }
  146. ToolbarItem(placement: .navigationBarTrailing) {
  147. trailingNavigationItems
  148. }
  149. }
  150. .navigationBarTitle("", displayMode: .inline)
  151. }
  152. }
  153. private var shouldAddCancelButton: Bool {
  154. switch saveButtonState {
  155. case .disabled, .loading:
  156. return false
  157. case .enabled:
  158. return true
  159. }
  160. }
  161. @ViewBuilder
  162. private var leadingNavigationBarItem: some View {
  163. if shouldAddCancelButton {
  164. cancelButton
  165. } else {
  166. EmptyView()
  167. }
  168. }
  169. private var page: some View {
  170. ConfigurationPage(
  171. title: title,
  172. actionButtonTitle: Text(mode.buttonText(isSaving: isSyncing)),
  173. actionButtonState: saveButtonState,
  174. cards: {
  175. Card {
  176. SettingDescription(text: description, informationalContent: {self.therapySettingType.helpScreen()})
  177. Splat(Array(scheduleItems.enumerated()), id: \.element.startTime) { index, item in
  178. self.itemView(for: item, at: index)
  179. }
  180. }
  181. },
  182. actionAreaContent: {
  183. actionAreaContent
  184. },
  185. action: {
  186. switch self.saveConfirmation {
  187. case .required(let alertContent):
  188. self.presentedAlert = .saveConfirmation(alertContent)
  189. case .notRequired:
  190. self.startSaving()
  191. }
  192. }
  193. )
  194. .alert(item: $presentedAlert, content: alert(for:))
  195. }
  196. private var saveButtonState: ConfigurationPageActionButtonState {
  197. if isSyncing {
  198. return .loading
  199. }
  200. let isEnabled = !scheduleItems.isEmpty
  201. && (scheduleItems != initialScheduleItems || mode == .acceptanceFlow)
  202. && tableDeletionState == .disabled
  203. return isEnabled ? .enabled : .disabled
  204. }
  205. private func itemView(for item: RepeatingScheduleValue<Value>, at index: Int) -> some View {
  206. Deletable(
  207. tableDeletionState: $tableDeletionState,
  208. index: index,
  209. isDeletable: index != 0,
  210. onDelete: {
  211. withAnimation {
  212. self.scheduleItems.remove(at: index)
  213. if self.scheduleItems.count == 1 {
  214. self.tableDeletionState = .disabled
  215. }
  216. }
  217. }
  218. ) {
  219. ScheduleItemView(
  220. time: item.startTime,
  221. isEditing: isEditing(index),
  222. valueContent: {
  223. valueContent(item.value, isEditing(index).wrappedValue)
  224. },
  225. expandedContent: {
  226. ScheduleItemPicker(
  227. item: $scheduleItems[index],
  228. isTimeSelectable: { self.isTimeSelectable($0, at: index) },
  229. valuePicker: { self.valuePicker(self.$scheduleItems[index], $0) }
  230. )
  231. }
  232. )
  233. }
  234. .accessibility(identifier: "schedule_item_\(index)")
  235. }
  236. private func isEditing(_ index: Int) -> Binding<Bool> {
  237. Binding(
  238. get: { index == self.editingIndex },
  239. set: { isNowEditing in
  240. self.editingIndex = isNowEditing ? index : nil
  241. }
  242. )
  243. }
  244. private func isTimeSelectable(_ time: TimeInterval, at index: Int) -> Bool {
  245. if index == scheduleItems.startIndex {
  246. return time == .hours(0)
  247. }
  248. let priorTime = scheduleItems[index - 1].startTime
  249. guard time > priorTime else {
  250. return false
  251. }
  252. if index < scheduleItems.endIndex - 1 {
  253. let nextTime = scheduleItems[index + 1].startTime
  254. guard time < nextTime else {
  255. return false
  256. }
  257. }
  258. return true
  259. }
  260. private var initialNewScheduleItem: RepeatingScheduleValue<Value> {
  261. assert(scheduleItems.count <= scheduleItemLimit)
  262. if scheduleItems.isEmpty {
  263. return RepeatingScheduleValue(startTime: .hours(0), value: defaultFirstScheduleItemValue)
  264. }
  265. if scheduleItems.last!.startTime == .hours(23.5) {
  266. let firstItemFollowedByOpening = scheduleItems.adjacentPairs().first(where: { item, next in
  267. next.startTime - item.startTime > .minutes(30)
  268. })!.0
  269. return RepeatingScheduleValue(
  270. startTime: firstItemFollowedByOpening.startTime + .minutes(30),
  271. value: firstItemFollowedByOpening.value
  272. )
  273. } else {
  274. return RepeatingScheduleValue(
  275. startTime: scheduleItems.last!.startTime + .minutes(30),
  276. value: scheduleItems.last!.value
  277. )
  278. }
  279. }
  280. private var trailingNavigationItems: some View {
  281. // TODO: SwiftUI's alignment of these buttons in the navigation bar is a little funky.
  282. // Tapping 'Edit' then 'Done' can shift '+' slightly.
  283. HStack(spacing: 24) {
  284. if tableDeletionState == .disabled {
  285. editButton
  286. } else {
  287. doneButton
  288. }
  289. addButton
  290. }
  291. }
  292. private var backButton: some View {
  293. Button(action: { presentationMode.wrappedValue.dismiss() }) {
  294. HStack {
  295. Image(systemName: "chevron.left")
  296. .resizable()
  297. .frame(width: 12, height: 20)
  298. Text(backButtonTitle)
  299. .fontWeight(.regular)
  300. }
  301. .offset(x: -6, y: 0)
  302. }
  303. }
  304. private var backButtonTitle: String { LocalizedString("Back", comment: "Back navigation button title") }
  305. var cancelButton: some View {
  306. Button(action: { self.dismiss() } ) { Text(LocalizedString("Cancel", comment: "Cancel editing settings button title")) }
  307. }
  308. var editButton: some View {
  309. Button(
  310. action: {
  311. withAnimation {
  312. self.tableDeletionState = .enabled
  313. }
  314. },
  315. label: {
  316. Text(LocalizedString("Edit", comment: "Text for edit button"))
  317. }
  318. )
  319. .disabled(scheduleItems.count == 1)
  320. }
  321. var doneButton: some View {
  322. Button(
  323. action: {
  324. withAnimation {
  325. self.tableDeletionState = .disabled
  326. }
  327. },
  328. label: {
  329. Text(LocalizedString("Done", comment: "Text for done button")).bold()
  330. }
  331. )
  332. }
  333. var addButton: some View {
  334. Button(
  335. action: {
  336. withAnimation {
  337. self.isAddingNewItem = true
  338. }
  339. },
  340. label: {
  341. Image(systemName: "plus")
  342. .imageScale(.large)
  343. .contentShape(Rectangle())
  344. }
  345. )
  346. .disabled(tableDeletionState != .disabled || scheduleItems.count >= scheduleItemLimit)
  347. }
  348. private func startSaving() {
  349. guard mode == .settings else {
  350. self.continueSaving()
  351. return
  352. }
  353. authenticate(therapySettingType.authenticationChallengeDescription) {
  354. switch $0 {
  355. case .success: self.continueSaving()
  356. case .failure: break
  357. }
  358. }
  359. }
  360. private func continueSaving() {
  361. switch savingMechanism {
  362. case .synchronous(let save):
  363. save(scheduleItems)
  364. case .asynchronous(let save):
  365. withAnimation {
  366. self.editingIndex = nil
  367. self.isSyncing = true
  368. }
  369. save(scheduleItems) { error in
  370. DispatchQueue.main.async {
  371. if let error = error {
  372. withAnimation {
  373. self.isSyncing = false
  374. }
  375. self.presentedAlert = .saveError(error)
  376. }
  377. }
  378. }
  379. }
  380. }
  381. private func alert(for presentedAlert: PresentedAlert) -> SwiftUI.Alert {
  382. switch presentedAlert {
  383. case .saveConfirmation(let content):
  384. return Alert(
  385. title: content.title,
  386. message: content.message,
  387. 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"))),
  388. secondaryButton: .default(
  389. Text(LocalizedString("Continue", comment: "Button text to confirm saving from alert popup when some schedule values are outside the recommended range")),
  390. action: startSaving
  391. )
  392. )
  393. case .saveError(let error):
  394. return Alert(
  395. title: Text(LocalizedString("Unable to Save", comment: "Alert title when error occurs while saving a schedule")),
  396. message: Text(error.localizedDescription)
  397. )
  398. }
  399. }
  400. }
  401. struct DarkenedOverlay: View {
  402. var body: some View {
  403. Rectangle()
  404. .fill(Color.black.opacity(0.3))
  405. .edgesIgnoringSafeArea(.all)
  406. }
  407. }
  408. extension ScheduleEditor.PresentedAlert: Identifiable {
  409. var id: Int {
  410. switch self {
  411. case .saveConfirmation:
  412. return 0
  413. case .saveError:
  414. return 1
  415. }
  416. }
  417. }