forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessonConfigurationViewController.swift
More file actions
161 lines (127 loc) · 5.15 KB
/
LessonConfigurationViewController.swift
File metadata and controls
161 lines (127 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// LessonConfigurationViewController.swift
// Learn
//
// Copyright © 2019 LoopKit Authors. All rights reserved.
//
import UIKit
import LoopKitUI
class LessonConfigurationViewController: UITableViewController {
var lesson: Lesson!
private enum State {
case editing
case executing
}
private var state = State.editing {
didSet {
guard state != oldValue else {
return
}
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: lesson.configurationSections.count)) as? TextButtonTableViewCell {
cell.isLoading = state == .executing
cell.isEnabled = state == .editing
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
title = lesson.title
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableView.automaticDimension
for section in lesson.configurationSections {
for cell in section.cells {
cell.registerCell(for: self.tableView)
}
}
tableView.register(TextButtonTableViewCell.self, forCellReuseIdentifier: TextButtonTableViewCell.className)
}
/// If a tap occurs in the table view, but not on any cells, dismiss any active edits
@IBAction private func dismissActiveEditing(gestureRecognizer: UITapGestureRecognizer) {
let tapPoint = gestureRecognizer.location(in: tableView)
guard tableView.indexPathForRow(at: tapPoint) == nil else {
return
}
tableView.endEditing(false)
tableView.beginUpdates()
hideDatePickerCells()
tableView.endUpdates()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return lesson.configurationSections.count + 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == lesson.configurationSections.count {
return 1
} else {
return lesson.configurationSections[section].cells.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == lesson.configurationSections.count {
let cell = tableView.dequeueReusableCell(withIdentifier: TextButtonTableViewCell.className, for: indexPath) as! TextButtonTableViewCell
cell.textLabel?.text = NSLocalizedString("Continue", comment: "Title of the button to begin lesson execution")
switch state {
case .editing:
cell.isEnabled = true
cell.isLoading = false
case .executing:
cell.isEnabled = false
cell.isLoading = true
}
return cell
} else {
return lesson.configurationSections[indexPath.section].cells[indexPath.item].tableView(tableView, cellForRowAt: indexPath)
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < lesson.configurationSections.count {
return lesson.configurationSections[section].headerTitle
} else {
return nil
}
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if section < lesson.configurationSections.count {
return lesson.configurationSections[section].footerTitle
} else {
return nil
}
}
// MARK: - UITableViewDelegate
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return state == .editing
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard case .editing = state else {
return nil
}
tableView.endEditing(false)
tableView.beginUpdates()
hideDatePickerCells(excluding: indexPath)
return indexPath
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.endUpdates()
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.section == lesson.configurationSections.count {
state = .executing
lesson.execute { resultSections in
dispatchPrecondition(condition: .onQueue(.main))
self.performSegue(withIdentifier: LessonResultsViewController.className, sender: resultSections)
self.state = .editing
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let results = sender as? [LessonSectionProviding], let destination = segue.destination as? LessonResultsViewController {
destination.lesson = lesson
destination.results = results
}
}
}
extension LessonConfigurationViewController: DatePickerTableViewCellDelegate {
func datePickerTableViewCellDidUpdateDate(_ cell: DatePickerTableViewCell) {
}
}