-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathPricingVC.swift
118 lines (91 loc) · 3.95 KB
/
PricingVC.swift
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
//
// PricingVC.swift
// ScrollStackControllerDemo
//
// Created by Daniele Margutti on 05/10/2019.
// Copyright © 2019 ScrollStackController. All rights reserved.
//
import UIKit
public protocol PricingVCProtocol: AnyObject {
func addFee()
}
public class PricingVC: UIViewController, ScrollStackContainableController {
public weak var delegate: PricingVCProtocol?
@IBOutlet public var pricingTable: UITableView!
@IBOutlet public var pricingTableHeightConstraint: NSLayoutConstraint!
public var pricingTags: [PricingTag] = [
PricingTag(title: "Night fee", subtitle: "$750 x 3 nights", price: "$2,250.00"),
PricingTag(title: "Hospitality fees", subtitle: "This fee covers services that come with the room", price: "$10.00"),
PricingTag(title: "Property use taxes", subtitle: "Taxes the cost pays to rent their room", price: "$200.00")
]
public static func create(delegate: PricingVCProtocol) -> PricingVC {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "PricingVC") as! PricingVC
vc.delegate = delegate
return vc
}
public func scrollStackRowSizeForAxis(_ axis: NSLayoutConstraint.Axis, row: ScrollStackRow, in stackView: ScrollStack) -> ScrollStack.ControllerSize? {
return .fitLayoutForAxis
// let size = CGSize(width: stackView.bounds.size.width, height: 9000)
// let best = self.view.systemLayoutSizeFitting(size, withHorizontalFittingPriority: .required, verticalFittingPriority: .defaultLow)
// // NOTE:
// // it's important to set both the height constraint and bottom safe constraints to safe area for tableview,
// // otherwise growing does not work.
// return best.height
}
override public func updateViewConstraints() {
pricingTableHeightConstraint.constant = pricingTable.contentSize.height // the size of the table as the size of its content
view.height(constant: nil) // cancel any height constraint already in place in the view
super.updateViewConstraints()
}
public override func viewDidLoad() {
super.viewDidLoad()
pricingTable.rowHeight = UITableView.automaticDimension
pricingTable.estimatedRowHeight = 60
pricingTable.reloadData()
pricingTable.sizeToFit()
}
public func addFee(_ otherFee: PricingTag) {
pricingTags.append(otherFee)
pricingTable.reloadData()
updateViewConstraints()
viewDidLayoutSubviews()
}
public func reloadContentFromStackView(stackView: ScrollStack, row: ScrollStackRow, animated: Bool) {
}
@IBAction public func addFee() {
delegate?.addFee()
}
}
extension PricingVC: UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pricingTags.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PricingCell", for: indexPath) as! PricingCell
cell.priceTag = pricingTags[indexPath.row]
return cell
}
}
public struct PricingTag {
public let title: String
public let subtitle: String
public let price: String
public init(title: String, subtitle: String, price: String) {
self.title = title
self.subtitle = subtitle
self.price = price
}
}
public class PricingCell: UITableViewCell {
@IBOutlet public var titleLabel: UILabel!
@IBOutlet public var subtitleLabel: UILabel!
@IBOutlet public var priceLabel: UILabel!
public var priceTag: PricingTag? {
didSet {
titleLabel.text = priceTag?.title ?? ""
subtitleLabel.text = priceTag?.subtitle ?? ""
priceLabel.text = priceTag?.price ?? ""
}
}
}