forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateAndDurationTableViewController.swift
More file actions
99 lines (79 loc) · 3.13 KB
/
DateAndDurationTableViewController.swift
File metadata and controls
99 lines (79 loc) · 3.13 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
//
// DateAndDurationTableViewController.swift
// LoopKitUI
//
// Created by Michael Pangburn on 11/24/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//
import UIKit
public protocol DateAndDurationTableViewControllerDelegate: AnyObject {
func dateAndDurationTableViewControllerDidChangeDate(_ controller: DateAndDurationTableViewController)
}
public class DateAndDurationTableViewController: UITableViewController {
public enum InputMode {
case date(Date, mode: UIDatePicker.Mode)
case duration(TimeInterval)
}
public var inputMode: InputMode = .date(Date(), mode: .dateAndTime) {
didSet {
delegate?.dateAndDurationTableViewControllerDidChangeDate(self)
}
}
public var titleText: String?
public var contextHelp: String?
public var indexPath: IndexPath?
public weak var delegate: DateAndDurationTableViewControllerDelegate?
public convenience init() {
self.init(style: .grouped)
}
public override func viewDidLoad() {
super.viewDidLoad()
tableView.register(DateAndDurationTableViewCell.nib(), forCellReuseIdentifier: DateAndDurationTableViewCell.className)
}
private var completion: ((InputMode) -> Void)?
public func onSave(_ completion: @escaping (InputMode) -> Void) {
let saveBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(save))
navigationItem.rightBarButtonItem = saveBarButtonItem
self.completion = completion
}
@objc private func save() {
completion?(inputMode)
dismiss(animated: true)
}
public override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: DateAndDurationTableViewCell.className, for: indexPath) as! DateAndDurationTableViewCell
switch inputMode {
case .date(let date, mode: let mode):
cell.datePicker.datePickerMode = mode
cell.date = date
case .duration(let duration):
cell.datePicker.datePickerMode = .countDownTimer
cell.maximumDuration = .hours(24)
cell.duration = duration
}
cell.titleLabel.text = titleText
cell.isDatePickerHidden = false
cell.selectionStyle = .none
cell.delegate = self
return cell
}
public override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return contextHelp
}
}
extension DateAndDurationTableViewController: DatePickerTableViewCellDelegate {
public func datePickerTableViewCellDidUpdateDate(_ cell: DatePickerTableViewCell) {
switch inputMode {
case .date(_, mode: let mode):
inputMode = .date(cell.date, mode: mode)
case .duration(_):
inputMode = .duration(cell.duration)
}
}
}