-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathDayCell.swift
524 lines (406 loc) · 19.2 KB
/
DayCell.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//
// FastisDayCell.swift
// Fastis
//
// Created by Ilya Kharlamov on 10.04.2020.
// Copyright © 2020 DIGITAL RETAIL TECHNOLOGIES, S.L. All rights reserved.
//
import JTAppleCalendar
import UIKit
final class DayCell: JTACDayCell {
// MARK: - Outlets
lazy var dateLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var circleView: UIView = {
let view = UIView()
view.backgroundColor = .red
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var selectionBackgroundView: UIView = {
let view = UIView()
view.isHidden = true
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var backgroundRangeView: UIView = {
let view = UIView()
view.layer.masksToBounds = true
view.layer.cornerCurve = .continuous
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
// MARK: - Variables
private var config: FastisConfig.DayCell = FastisConfig.default.dayCell
private var todayConfig: FastisConfig.TodayCell? = FastisConfig.default.todayCell
private var rangeViewTopAnchorConstraints: [NSLayoutConstraint] = []
private var rangeViewBottomAnchorConstraints: [NSLayoutConstraint] = []
private var rangeViewLeftAnchorToSuperviewConstraint: NSLayoutConstraint?
private var rangeViewLeftAnchorToCenterConstraint: NSLayoutConstraint?
private var rangeViewRightAnchorToSuperviewConstraint: NSLayoutConstraint?
private var rangeViewRightAnchorToCenterConstraint: NSLayoutConstraint?
// MARK: - Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
self.configureSubviews()
self.configureConstraints()
self.applyConfig(.default)
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
self.circleView.removeFromSuperview()
}
// MARK: - Configurations
func applyConfig(_ config: FastisConfig) {
self.backgroundColor = config.controller.backgroundColor
let todayConfig = config.todayCell
let config = config.dayCell
self.todayConfig = todayConfig
self.config = config
self.backgroundRangeView.backgroundColor = config.onRangeBackgroundColor
self.backgroundRangeView.layer.cornerRadius = config.rangeViewCornerRadius
self.selectionBackgroundView.backgroundColor = config.selectedBackgroundColor
self.dateLabel.font = config.dateLabelFont
self.dateLabel.textColor = config.dateLabelColor
if let cornerRadius = config.customSelectionViewCornerRadius {
self.selectionBackgroundView.layer.cornerRadius = cornerRadius
}
self.rangeViewTopAnchorConstraints.forEach({ $0.constant = config.rangedBackgroundViewVerticalInset })
self.rangeViewBottomAnchorConstraints.forEach({ $0.constant = -config.rangedBackgroundViewVerticalInset })
}
public func configureSubviews() {
self.contentView.addSubview(self.backgroundRangeView)
self.contentView.addSubview(self.selectionBackgroundView)
self.contentView.addSubview(self.dateLabel)
self.selectionBackgroundView.layer.cornerRadius = min(self.frame.width, self.frame.height) / 2
}
public func configureConstraints() {
let inset = self.config.rangedBackgroundViewVerticalInset
NSLayoutConstraint.activate([
self.dateLabel.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
self.dateLabel.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor)
])
self.rangeViewLeftAnchorToSuperviewConstraint = self.backgroundRangeView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor)
self.rangeViewLeftAnchorToCenterConstraint = self.backgroundRangeView.leftAnchor.constraint(equalTo: self.contentView.centerXAnchor)
self.rangeViewRightAnchorToSuperviewConstraint = self.backgroundRangeView.rightAnchor
.constraint(equalTo:self.contentView.rightAnchor)
self.rangeViewRightAnchorToCenterConstraint = self.backgroundRangeView.rightAnchor
.constraint(equalTo: self.contentView.centerXAnchor)
NSLayoutConstraint.activate([
self.rangeViewLeftAnchorToSuperviewConstraint,
self.rangeViewRightAnchorToSuperviewConstraint
].compactMap { $0 })
NSLayoutConstraint.activate([
{
let constraint = self.selectionBackgroundView.heightAnchor.constraint(equalToConstant: 100)
constraint.priority = .defaultLow
return constraint
}(),
self.selectionBackgroundView.leftAnchor.constraint(greaterThanOrEqualTo: self.contentView.leftAnchor, constant: 1),
self.selectionBackgroundView.topAnchor.constraint(greaterThanOrEqualTo: self.contentView.topAnchor, constant: 1),
self.selectionBackgroundView.rightAnchor.constraint(lessThanOrEqualTo: self.contentView.rightAnchor, constant: -1),
self.selectionBackgroundView.bottomAnchor.constraint(lessThanOrEqualTo: self.contentView.bottomAnchor, constant: -1),
self.selectionBackgroundView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
self.selectionBackgroundView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor),
self.selectionBackgroundView.widthAnchor.constraint(equalTo: self.selectionBackgroundView.heightAnchor)
])
self.rangeViewTopAnchorConstraints = [
self.backgroundRangeView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: inset)
]
self.rangeViewBottomAnchorConstraints = [
self.backgroundRangeView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -inset)
]
NSLayoutConstraint.activate(self.rangeViewTopAnchorConstraints)
NSLayoutConstraint.activate(self.rangeViewBottomAnchorConstraints)
}
public static func makeViewConfig(
for state: CellState,
minimumDate: Date?,
maximumDate: Date?,
rangeValue: FastisRange?,
calendar: Calendar
) -> ViewConfig {
var config = ViewConfig()
if state.dateBelongsTo != .thisMonth {
config.isSelectedViewHidden = true
if let value = rangeValue {
var showRangeView = false
if state.dateBelongsTo == .followingMonthWithinBoundary {
let endOfPreviousMonth = calendar.date(byAdding: .month, value: -1, to: state.date)!.endOfMonth(in: calendar)
let startOfCurrentMonth = state.date.startOfMonth(in: calendar)
let fromDateIsInPast = value.fromDate < endOfPreviousMonth
let toDateIsInFutureOrCurrent = value.toDate > startOfCurrentMonth
showRangeView = fromDateIsInPast && toDateIsInFutureOrCurrent
} else if state.dateBelongsTo == .previousMonthWithinBoundary {
let startOfNextMonth = calendar.date(byAdding: .month, value: 1, to: state.date)!.startOfMonth(in: calendar)
let endOfCurrentMonth = state.date.endOfMonth(in: calendar)
let toDateIsInFuture = value.toDate > startOfNextMonth
let fromDateIsInPastOrCurrent = value.fromDate < endOfCurrentMonth
showRangeView = toDateIsInFuture && fromDateIsInPastOrCurrent
}
if showRangeView {
if state.day.rawValue == calendar.firstWeekday {
config.rangeView.leftSideState = .rounded
config.rangeView.rightSideState = .squared
} else if state.day.rawValue == calendar.lastWeekday {
config.rangeView.leftSideState = .squared
config.rangeView.rightSideState = .rounded
} else {
config.rangeView.leftSideState = .squared
config.rangeView.rightSideState = .squared
}
}
}
return config
}
config.dateLabelText = state.text
if let minimumDate, state.date < minimumDate.startOfDay(in: calendar) {
config.isDateEnabled = false
return config
} else if let maximumDate, state.date > maximumDate.endOfDay(in: calendar) {
config.isDateEnabled = false
return config
}
if state.isSelected {
let position = state.selectedPosition()
switch position {
case .full:
config.isSelectedViewHidden = false
case .left,
.right,
.middle:
config.isSelectedViewHidden = position == .middle
if position == .right, state.day.rawValue == calendar.firstWeekday {
config.rangeView.leftSideState = .rounded
} else if position == .left, state.day.rawValue == calendar.lastWeekday {
config.rangeView.rightSideState = .rounded
} else if position == .left {
config.rangeView.rightSideState = .squared
} else if position == .right {
config.rangeView.leftSideState = .squared
} else if state.day.rawValue == calendar.firstWeekday {
config.rangeView.leftSideState = .rounded
config.rangeView.rightSideState = .squared
} else if state.day.rawValue == calendar.lastWeekday {
config.rangeView.leftSideState = .squared
config.rangeView.rightSideState = .rounded
} else {
config.rangeView.leftSideState = .squared
config.rangeView.rightSideState = .squared
}
default:
break
}
}
return config
}
enum RangeSideState {
case squared
case rounded
case hidden
}
struct RangeViewConfig: Hashable {
var leftSideState: RangeSideState = .hidden
var rightSideState: RangeSideState = .hidden
var isHidden: Bool {
self.leftSideState == .hidden && self.rightSideState == .hidden
}
}
struct ViewConfig {
var dateLabelText: String?
var isSelectedViewHidden = true
var isDateEnabled = true
var rangeView = RangeViewConfig()
var isToday = false
}
internal func configure(for config: ViewConfig) {
self.selectionBackgroundView.isHidden = config.isSelectedViewHidden
self.isUserInteractionEnabled = config.dateLabelText != nil && config.isDateEnabled
self.clipsToBounds = config.dateLabelText == nil
if let dateLabelText = config.dateLabelText {
self.dateLabel.isHidden = false
self.dateLabel.text = dateLabelText
if config.isToday, let todayConfig {
self.configureTodayCell(viewConfig: config, todayConfig: todayConfig)
} else {
self.configureDayCell(viewConfig: config)
}
} else {
self.dateLabel.isHidden = true
}
self.backgroundRangeView.isHidden = false
self.backgroundRangeView.layer.maskedCorners = []
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = false
self.rangeViewLeftAnchorToCenterConstraint?.isActive = false
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = false
self.rangeViewRightAnchorToCenterConstraint?.isActive = false
switch (config.rangeView.leftSideState, config.rangeView.rightSideState) {
case (.hidden, .hidden):
self.backgroundRangeView.isHidden = true
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = true
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = true
case (.squared, .squared):
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = true
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = true
case (.hidden, .squared):
self.rangeViewLeftAnchorToCenterConstraint?.isActive = true
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = true
case (.squared, .hidden):
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = true
self.rangeViewRightAnchorToCenterConstraint?.isActive = true
case (.rounded, .squared):
self.backgroundRangeView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = true
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = true
case (.squared, .rounded):
self.backgroundRangeView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = true
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = true
case (.rounded, .hidden):
self.backgroundRangeView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
self.rangeViewLeftAnchorToSuperviewConstraint?.isActive = true
self.rangeViewRightAnchorToCenterConstraint?.isActive = true
case (.hidden, .rounded):
self.backgroundRangeView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
self.rangeViewLeftAnchorToCenterConstraint?.isActive = true
self.rangeViewRightAnchorToSuperviewConstraint?.isActive = true
default:
break
}
}
private func configureDayCell(viewConfig: ViewConfig) {
if !viewConfig.isDateEnabled {
self.dateLabel.textColor = self.config.dateLabelUnavailableColor
} else if !viewConfig.isSelectedViewHidden {
self.dateLabel.textColor = self.config.selectedLabelColor
} else if !viewConfig.rangeView.isHidden {
self.dateLabel.textColor = self.config.onRangeLabelColor
} else {
self.dateLabel.textColor = self.config.dateLabelColor
}
}
private func configureTodayCell(viewConfig: ViewConfig, todayConfig: FastisConfig.TodayCell) {
self.dateLabel.font = todayConfig.dateLabelFont
if !viewConfig.isDateEnabled {
self.dateLabel.textColor = todayConfig.dateLabelUnavailableColor
self.circleView.backgroundColor = todayConfig.circleViewUnavailableColor
} else if !viewConfig.isSelectedViewHidden {
self.dateLabel.textColor = todayConfig.selectedLabelColor
self.circleView.backgroundColor = todayConfig.circleViewSelectedColor
} else if !viewConfig.rangeView.isHidden {
self.dateLabel.textColor = todayConfig.onRangeLabelColor
self.circleView.backgroundColor = todayConfig.onRangeLabelColor
} else {
self.dateLabel.textColor = todayConfig.dateLabelColor
self.circleView.backgroundColor = todayConfig.circleViewColor
}
self.circleView.layer.cornerRadius = todayConfig.circleSize * 0.5
self.circleView.removeFromSuperview()
self.contentView.addSubview(self.circleView)
NSLayoutConstraint.activate([
self.circleView.centerXAnchor.constraint(equalTo: self.dateLabel.centerXAnchor),
self.circleView.topAnchor.constraint(equalTo: self.dateLabel.bottomAnchor, constant: todayConfig.circleVerticalInset),
self.circleView.widthAnchor.constraint(equalToConstant: todayConfig.circleSize),
self.circleView.heightAnchor.constraint(equalToConstant: todayConfig.circleSize)
])
}
}
public extension FastisConfig {
/**
Day cells (selection parameters, font, etc.)
Configurable in FastisConfig.``FastisConfig/dayCell-swift.property`` property
*/
class DayCell {
/**
Font of date label in cell
Default value — `.systemFont(ofSize: 17)`
*/
public var dateLabelFont: UIFont = .systemFont(ofSize: 17)
/**
Color of date label in cell
Default value — `.label`
*/
public var dateLabelColor: UIColor = .label
/**
Color of date label in cell when date is unavailable for select
Default value — `.tertiaryLabel`
*/
public var dateLabelUnavailableColor: UIColor = .tertiaryLabel
/**
Color of background of cell when date is selected
Default value — `.systemBlue`
*/
public var selectedBackgroundColor: UIColor = .systemBlue
/**
Color of date label in cell when date is selected
Default value — `.white`
*/
public var selectedLabelColor: UIColor = .white
/**
Corner radius of cell when date is a start or end of selected range
Default value — `6pt`
*/
public var rangeViewCornerRadius: CGFloat = 6
/**
Color of background of cell when date is a part of selected range
Default value — `.systemBlue.withAlphaComponent(0.2)`
*/
public var onRangeBackgroundColor: UIColor = .systemBlue.withAlphaComponent(0.2)
/**
Color of date label in cell when date is a part of selected range
Default value — `.label`
*/
public var onRangeLabelColor: UIColor = .label
/**
Inset of cell's background view when date is a part of selected range
Default value — `3pt`
*/
public var rangedBackgroundViewVerticalInset: CGFloat = 3
/**
This property allows to set custom radius for selection view
If this value is not `nil` then selection view will have corner radius `.height / 2`
Default value — `nil`
*/
public var customSelectionViewCornerRadius: CGFloat?
}
final class TodayCell: DayCell {
/**
Size circle view in cell
Default value — `4pt`
*/
public var circleSize: CGFloat = 4
/**
Color of circle view in cell
Default value — `.label`
*/
public var circleViewColor: UIColor = .systemBlue
/**
Color of circle view in cell when date is unavailable for select
Default value — `.tertiaryLabel`
*/
public var circleViewUnavailableColor: UIColor = .tertiaryLabel
/**
Color of circle view in cell when date is selected
Default value — `.white`
*/
public var circleViewSelectedColor: UIColor = .white
/**
Color of circle view in cell when date is a part of selected range
Default value — `.label`
*/
public var circleViewOnRangeColor: UIColor = .systemBlue
/**
Inset circle view from date label
Default value — `5pt`
*/
public var circleVerticalInset: CGFloat = 3
}
}