This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduleTableViewCell.swift
120 lines (101 loc) · 2.94 KB
/
ScheduleTableViewCell.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
119
120
//
// ScheduleTableViewCell.swift
// PASSCITY
//
// Created by Алексей on 04.11.17.
// Copyright © 2017 PASSCITY-iOS-TEST. All rights reserved.
//
import Foundation
import UIKit
import EasyPeasy
extension Collection {
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
class ScheduleTableViewCell: UITableViewCell {
let titleLabel = UILabel()
let weekViews = UIView()
let schedulesView = UIStackView()
let expandButton = UIButton()
let closeButton = UIButton()
var isExpandedHanlder: (() -> Void)? = nil
var isExpandedConstraint: NSLayoutConstraint? = nil
var isExpanded: Bool = false {
didSet {
easy_reload()
schedulesView.isHidden = !isExpanded
layoutIfNeeded()
updateConstraints()
isExpandedHanlder?()
}
}
func confure(_ item: PassCityFeedItem) {
titleLabel.text = item.fullSchedule?.short ?? (item.shortSchedule.isEmpty ? item.schedule : item.shortSchedule)
schedulesView.spacing = 8
guard let scheduleState: ScheduleState = item.fullSchedule else { return }
for (offset, day) in scheduleState.full.enumerated() {
if let view = schedulesView.arrangedSubviews[safe: offset] as? AvailableWeekView {
view.configure(time: day.title, days: day.week)
} else {
let scheduleView = AvailableWeekView(text: day.title, days: day.week)
scheduleView <- [
Left(),
Height(20),
Right()
]
scheduleView.backgroundColor = offset%2 == 0 ? .white20 : .white
schedulesView.addArrangedSubview(scheduleView)
layoutIfNeeded()
updateFocusIfNeeded()
}
}
easy_reload()
}
init() {
super.init(style: .default, reuseIdentifier: nil)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
selectionStyle = .none
addSubview(titleLabel)
addSubview(schedulesView)
addSubview(expandButton)
addSubview(closeButton)
titleLabel.font = UIFont.systemFont(ofSize: 13)
titleLabel <- [
Top(16).with(.high),
CenterX()
]
expandButton <- [
Top(5).to(titleLabel),
CenterX()
]
schedulesView.axis = .vertical
schedulesView.distribution = .equalSpacing
schedulesView.isHidden = isExpanded
schedulesView <- [
Top(20).to(expandButton),
Left(20),
Right(10)
]
isExpandedConstraint = schedulesView.heightAnchor.constraint(equalToConstant: 0)
isExpandedConstraint?.isActive = false
expandButton.setImage(#imageLiteral(resourceName: "handle"), for: .normal)
closeButton.setImage (#imageLiteral(resourceName: "iconNavbarClose"), for: .normal)
closeButton.addTarget(self, action: #selector(toggleExpandButton), for: .touchUpInside)
closeButton <- [
Top(20).to(schedulesView),
Bottom(20),
Height(20),
CenterX()
]
}
func toggleExpandButton() {
isExpanded = !isExpanded
}
}