forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupTableViewController.swift
More file actions
116 lines (83 loc) · 4.21 KB
/
SetupTableViewController.swift
File metadata and controls
116 lines (83 loc) · 4.21 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
//
// SetupTableViewController.swift
// Loop
//
// Copyright © 2018 LoopKit Authors. All rights reserved.
//
import UIKit
public protocol SetupTableViewControllerDelegate: AnyObject {
func setupTableViewControllerCancelButtonPressed(_ viewController: SetupTableViewController)
}
open class SetupTableViewController: UITableViewController {
private(set) open lazy var footerView = SetupTableFooterView(frame: .zero)
private var lastContentHeight: CGFloat = 0
public var padFooterToBottom: Bool = true
public weak var delegate: SetupTableViewControllerDelegate?
open override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed(_:)))
footerView.primaryButton.addTarget(self, action: #selector(continueButtonPressed(_:)), for: .touchUpInside)
}
open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Reposition footer view if necessary
if tableView.contentSize.height != lastContentHeight {
lastContentHeight = tableView.contentSize.height
tableView.tableFooterView = nil
var footerSize = footerView.systemLayoutSizeFitting(CGSize(width: tableView.frame.size.width, height: UIView.layoutFittingCompressedSize.height))
let visibleHeight = tableView.bounds.size.height - (tableView.adjustedContentInset.top + tableView.adjustedContentInset.bottom)
let footerHeight = padFooterToBottom ? max(footerSize.height, visibleHeight - tableView.contentSize.height) : footerSize.height
footerSize.height = footerHeight
footerView.frame.size = footerSize
tableView.tableFooterView = footerView
}
}
@IBAction open func cancelButtonPressed(_: Any) {
delegate?.setupTableViewControllerCancelButtonPressed(self)
}
@IBAction open func continueButtonPressed(_ sender: Any) {
if shouldPerformSegue(withIdentifier: "Continue", sender: sender) {
performSegue(withIdentifier: "Continue", sender: sender)
}
}
// MARK: - UITableViewDelegate
open override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
open override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
open override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
}
open class SetupTableFooterView: UIView {
public let primaryButton = SetupButton(type: .custom)
public override init(frame: CGRect) {
let buttonStack = UIStackView(arrangedSubviews: [primaryButton])
super.init(frame: frame)
autoresizingMask = [.flexibleWidth]
primaryButton.resetTitle()
buttonStack.alignment = .center
buttonStack.axis = .vertical
buttonStack.distribution = .fillEqually
buttonStack.translatesAutoresizingMaskIntoConstraints = false
addSubview(buttonStack)
NSLayoutConstraint.activate([
primaryButton.leadingAnchor.constraint(equalTo: buttonStack.leadingAnchor),
primaryButton.trailingAnchor.constraint(equalTo: buttonStack.trailingAnchor),
buttonStack.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor),
buttonStack.leadingAnchor.constraint(equalToSystemSpacingAfter: layoutMarginsGuide.leadingAnchor, multiplier: 1),
layoutMarginsGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: buttonStack.trailingAnchor, multiplier: 1),
safeAreaLayoutGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: buttonStack.bottomAnchor, multiplier: 2),
])
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
public extension SetupButton {
func resetTitle() {
setTitle(LocalizedString("Continue", comment: "Title of the setup button to continue"), for: .normal)
}
}