forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleValueScheduleTableViewController.swift
More file actions
300 lines (229 loc) · 11.1 KB
/
SingleValueScheduleTableViewController.swift
File metadata and controls
300 lines (229 loc) · 11.1 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// SingleValueScheduleTableViewController.swift
// Naterade
//
// Created by Nathan Racklyeft on 2/13/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
import LoopKit
public enum RepeatingScheduleValueResult<T: RawRepresentable> {
case success(scheduleItems: [RepeatingScheduleValue<T>], timeZone: TimeZone)
case failure(Error)
}
public protocol SingleValueScheduleTableViewControllerSyncSource: AnyObject {
func syncScheduleValues(for viewController: SingleValueScheduleTableViewController, completion: @escaping (_ result: RepeatingScheduleValueResult<Double>) -> Void)
func syncButtonTitle(for viewController: SingleValueScheduleTableViewController) -> String
func syncButtonDetailText(for viewController: SingleValueScheduleTableViewController) -> String?
func singleValueScheduleTableViewControllerIsReadOnly(_ viewController: SingleValueScheduleTableViewController) -> Bool
}
open class SingleValueScheduleTableViewController: DailyValueScheduleTableViewController, RepeatingScheduleValueTableViewCellDelegate {
open override func viewDidLoad() {
super.viewDidLoad()
tableView.register(RepeatingScheduleValueTableViewCell.nib(), forCellReuseIdentifier: RepeatingScheduleValueTableViewCell.className)
tableView.register(TextButtonTableViewCell.self, forCellReuseIdentifier: TextButtonTableViewCell.className)
}
open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if syncSource == nil {
delegate?.dailyValueScheduleTableViewControllerWillFinishUpdating(self)
}
}
// MARK: - State
public var scheduleItems: [RepeatingScheduleValue<Double>] = []
override func addScheduleItem(_ sender: Any?) {
guard !isReadOnly && !isSyncInProgress else {
return
}
tableView.endEditing(false)
var startTime = TimeInterval(0)
var value = 0.0
if scheduleItems.count > 0, let cell = tableView.cellForRow(at: IndexPath(row: scheduleItems.count - 1, section: 0)) as? RepeatingScheduleValueTableViewCell {
let lastItem = scheduleItems.last!
let interval = cell.datePickerInterval
startTime = lastItem.startTime + interval
value = lastItem.value
if startTime >= TimeInterval(hours: 24) {
return
}
}
scheduleItems.append(
RepeatingScheduleValue(
startTime: min(TimeInterval(hours: 23.5), startTime),
value: value
)
)
super.addScheduleItem(sender)
}
override func insertableIndiciesByRemovingRow(_ row: Int, withInterval timeInterval: TimeInterval) -> [Bool] {
return insertableIndices(for: scheduleItems, removing: row, with: timeInterval)
}
var preferredValueFractionDigits: Int {
return 1
}
public weak var syncSource: SingleValueScheduleTableViewControllerSyncSource? {
didSet {
isReadOnly = syncSource?.singleValueScheduleTableViewControllerIsReadOnly(self) ?? false
if isViewLoaded {
tableView.reloadData()
}
}
}
private var isSyncInProgress = false {
didSet {
for cell in tableView.visibleCells {
switch cell {
case let cell as TextButtonTableViewCell:
cell.isEnabled = !isSyncInProgress
cell.isLoading = isSyncInProgress
case let cell as RepeatingScheduleValueTableViewCell:
cell.isReadOnly = isReadOnly || isSyncInProgress
default:
break
}
}
for item in navigationItem.rightBarButtonItems ?? [] {
item.isEnabled = !isSyncInProgress
}
navigationItem.hidesBackButton = isSyncInProgress
}
}
// MARK: - UITableViewDataSource
private enum Section: Int {
case schedule
case sync
}
open override func numberOfSections(in tableView: UITableView) -> Int {
if syncSource != nil {
return 2
}
return 1
}
open override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Section(rawValue: section)! {
case .schedule:
return scheduleItems.count
case .sync:
return 1
}
}
open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch Section(rawValue: indexPath.section)! {
case .schedule:
let cell = tableView.dequeueReusableCell(withIdentifier: RepeatingScheduleValueTableViewCell.className, for: indexPath) as! RepeatingScheduleValueTableViewCell
let item = scheduleItems[indexPath.row]
let interval = cell.datePickerInterval
cell.timeZone = timeZone
cell.date = midnight.addingTimeInterval(item.startTime)
cell.valueNumberFormatter.minimumFractionDigits = preferredValueFractionDigits
cell.value = item.value
cell.unitString = unitDisplayString
cell.isReadOnly = isReadOnly || isSyncInProgress
cell.delegate = self
if indexPath.row > 0 {
let lastItem = scheduleItems[indexPath.row - 1]
cell.datePicker.minimumDate = midnight.addingTimeInterval(lastItem.startTime).addingTimeInterval(interval)
}
if indexPath.row < scheduleItems.endIndex - 1 {
let nextItem = scheduleItems[indexPath.row + 1]
cell.datePicker.maximumDate = midnight.addingTimeInterval(nextItem.startTime).addingTimeInterval(-interval)
} else {
cell.datePicker.maximumDate = midnight.addingTimeInterval(TimeInterval(hours: 24) - interval)
}
return cell
case .sync:
let cell = tableView.dequeueReusableCell(withIdentifier: TextButtonTableViewCell.className, for: indexPath) as! TextButtonTableViewCell
cell.textLabel?.text = syncSource?.syncButtonTitle(for: self)
cell.isEnabled = !isSyncInProgress
cell.isLoading = isSyncInProgress
return cell
}
}
open override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
switch Section(rawValue: section)! {
case .schedule:
return nil
case .sync:
return syncSource?.syncButtonDetailText(for: self)
}
}
open override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
scheduleItems.remove(at: indexPath.row)
super.tableView(tableView, commit: editingStyle, forRowAt: indexPath)
}
}
open override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath != destinationIndexPath {
let item = scheduleItems.remove(at: sourceIndexPath.row)
scheduleItems.insert(item, at: destinationIndexPath.row)
guard destinationIndexPath.row > 0, let cell = tableView.cellForRow(at: destinationIndexPath) as? RepeatingScheduleValueTableViewCell else {
return
}
let interval = cell.datePickerInterval
let startTime = scheduleItems[destinationIndexPath.row - 1].startTime + interval
scheduleItems[destinationIndexPath.row] = RepeatingScheduleValue(startTime: startTime, value: scheduleItems[destinationIndexPath.row].value)
// Since the valid date ranges of neighboring cells are affected, the lazy solution is to just reload the entire table view
DispatchQueue.main.async {
tableView.reloadData()
}
}
}
// MARK: - UITableViewDelegate
open override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return super.tableView(tableView, canEditRowAt: indexPath) && !isSyncInProgress
}
open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
super.tableView(tableView, didSelectRowAt: indexPath)
switch Section(rawValue: indexPath.section)! {
case .schedule:
break
case .sync:
if let syncSource = syncSource, !isSyncInProgress {
isSyncInProgress = true
syncSource.syncScheduleValues(for: self) { (result) in
DispatchQueue.main.async {
switch result {
case .success(let items, let timeZone):
self.scheduleItems = items
self.timeZone = timeZone
self.tableView.reloadSections([Section.schedule.rawValue], with: .fade)
self.isSyncInProgress = false
self.delegate?.dailyValueScheduleTableViewControllerWillFinishUpdating(self)
case .failure(let error):
self.present(UIAlertController(with: error), animated: true) {
self.isSyncInProgress = false
}
}
}
}
}
}
}
open override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
guard sourceIndexPath != proposedDestinationIndexPath, let cell = tableView.cellForRow(at: sourceIndexPath) as? RepeatingScheduleValueTableViewCell else {
return proposedDestinationIndexPath
}
let interval = cell.datePickerInterval
let indices = insertableIndices(for: scheduleItems, removing: sourceIndexPath.row, with: interval)
let closestDestinationRow = indices.insertableIndex(closestTo: proposedDestinationIndexPath.row, from: sourceIndexPath.row)
return IndexPath(row: closestDestinationRow, section: proposedDestinationIndexPath.section)
}
// MARK: - RepeatingScheduleValueTableViewCellDelegate
override public func datePickerTableViewCellDidUpdateDate(_ cell: DatePickerTableViewCell) {
if let indexPath = tableView.indexPath(for: cell) {
let currentItem = scheduleItems[indexPath.row]
scheduleItems[indexPath.row] = RepeatingScheduleValue(
startTime: cell.date.timeIntervalSince(midnight),
value: currentItem.value
)
}
super.datePickerTableViewCellDidUpdateDate(cell)
}
func repeatingScheduleValueTableViewCellDidUpdateValue(_ cell: RepeatingScheduleValueTableViewCell) {
if let indexPath = tableView.indexPath(for: cell) {
let currentItem = scheduleItems[indexPath.row]
scheduleItems[indexPath.row] = RepeatingScheduleValue(startTime: currentItem.startTime, value: cell.value)
}
}
}