Skip to content

Commit bb39d0b

Browse files
fix #29 add indication for current day (#36)
1 parent 738b151 commit bb39d0b

File tree

5 files changed

+153
-16
lines changed

5 files changed

+153
-16
lines changed

Example/Source/ViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ViewController: UIViewController {
1313

1414
// MARK: - Outlets
1515

16-
lazy var containerView: UIStackView = {
16+
private lazy var containerView: UIStackView = {
1717
let view = UIStackView()
1818
view.backgroundColor = .clear
1919
view.axis = .vertical
@@ -26,14 +26,14 @@ class ViewController: UIViewController {
2626

2727
private lazy var currentDateLabel = UILabel()
2828

29-
lazy var chooseRangeButton: UIButton = {
29+
private lazy var chooseRangeButton: UIButton = {
3030
let button = UIButton(type: .system)
3131
button.setTitle("Choose range of dates", for: .normal)
3232
button.addTarget(self, action: #selector(self.chooseRange), for: .touchUpInside)
3333
return button
3434
}()
3535

36-
lazy var chooseSingleButton: UIButton = {
36+
private lazy var chooseSingleButton: UIButton = {
3737
let button = UIButton(type: .system)
3838
button.setTitle("Choose single date", for: .normal)
3939
button.addTarget(self, action: #selector(self.chooseSingleDate), for: .touchUpInside)
@@ -42,7 +42,7 @@ class ViewController: UIViewController {
4242

4343
// MARK: - Variables
4444

45-
var currentValue: FastisValue? {
45+
private var currentValue: FastisValue? {
4646
didSet {
4747
let formatter = DateFormatter()
4848
formatter.dateFormat = "dd/MM/yyyy"

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Fastis can be customised global or local. `FastisConfig` have some sections:
202202
- `controller` - base view controller (`cancelButtonTitle`, `doneButtonTitle`, etc.)
203203
- `monthHeader` - month titles
204204
- `dayCell` - day cells (selection parameters, font, etc.)
205+
- `todayCell` - today cell (selection parameters, font, circle view, etc.). If you set `todayCell` to `nil`, view will use `dayCell` instead
205206
- `weekView` - top header view with weekday names
206207
- `currentValueView` - current value view appearance (clear button, date format, etc.)
207208
- `shortcutContainerView` - bottom view with shortcuts
@@ -220,6 +221,17 @@ var customConfig = FastisConfig.default
220221
customConfig.controller.dayCell.dateLabelColor = .blue
221222
let fastisController = FastisController(mode: .range, config: customConfig)
222223
```
224+
To customise a today cell:
225+
226+
```swift
227+
let config = FastisConfig.withCurrentDate
228+
config.todayCell.dateLabelColor = .red
229+
config.todayCell.onRangeLabelColor = .red
230+
config.todayCell.circleSize = 4
231+
config.todayCell.circleViewColor = .red
232+
```
233+
234+
If you don't want to customzie today date cell, just set `config.todayCell = nil` and today cell will use `dayCell` config.
223235

224236
## Credits
225237

Sources/Models/Config.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ public struct FastisConfig {
2727
config.monthHeader.labelColor = .red
2828
let controller = FastisController(mode: .single, config: config)
2929
```
30+
Default configuration set showCurrentDate to false. In this case current day will not be indicate
31+
WithCurrentDate configuration set showCurrentDateto true. In this case current day will be indicate
32+
33+
You can customized indication current day:
34+
```swift
35+
let config = FastisConfig.withCurrentDate
36+
// date label
37+
config.todayCell.dateLabelColor = .red
38+
config.todayCell.selectedLabelColor = .orange
39+
config.todayCell.onRangeLabelColor = .green
40+
config.todayCell.dateLabelUnavailableColor = .cyan
41+
42+
// circle view
43+
config.todayCell.circleSize = 4
44+
config.todayCell.circleVerticalInset = 5
45+
config.todayCell.circleViewColor = .red
46+
config.todayCell.circleViewSelectedColor = .orange
47+
config.todayCell.circleViewOnRangeColor = .green
48+
config.todayCell.circleViewUnavailableColor = .cyan
49+
```
3050
*/
3151
public static var `default` = FastisConfig()
3252

@@ -48,6 +68,9 @@ public struct FastisConfig {
4868
/// Day cells (selection parameters, font, etc.)
4969
public var dayCell = FastisConfig.DayCell()
5070

71+
/// Today cell (selection parameters, font, etc.)
72+
public var todayCell: FastisConfig.TodayCell? = FastisConfig.TodayCell()
73+
5174
/// Top header view with week day names
5275
public var weekView = FastisConfig.WeekView()
5376

Sources/Views/Controller.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
418418
newConfig.dateLabelText = self.dayFormatter.string(from: date)
419419
}
420420

421+
if Calendar.current.isDateInToday(date) {
422+
newConfig.isToday = true
423+
}
424+
421425
self.viewConfigs[indexPath] = newConfig
422426
cell.applyConfig(self.config)
423427
cell.configure(for: newConfig)

Sources/Views/DayCell.swift

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ final class DayCell: JTACDayCell {
2020
return label
2121
}()
2222

23+
lazy var circleView: UIView = {
24+
let view = UIView()
25+
view.backgroundColor = .red
26+
view.translatesAutoresizingMaskIntoConstraints = false
27+
return view
28+
}()
29+
2330
lazy var selectionBackgroundView: UIView = {
2431
let view = UIView()
2532
view.isHidden = true
@@ -46,6 +53,7 @@ final class DayCell: JTACDayCell {
4653
// MARK: - Variables
4754

4855
private var config: FastisConfig.DayCell = FastisConfig.default.dayCell
56+
private var todayConfig: FastisConfig.TodayCell? = FastisConfig.default.todayCell
4957
private var rangeViewTopAnchorConstraints: [NSLayoutConstraint] = []
5058
private var rangeViewBottomAnchorConstraints: [NSLayoutConstraint] = []
5159

@@ -63,13 +71,22 @@ final class DayCell: JTACDayCell {
6371
fatalError("init(coder:) has not been implemented")
6472
}
6573

74+
override func prepareForReuse() {
75+
super.prepareForReuse()
76+
self.circleView.removeFromSuperview()
77+
}
78+
6679
// MARK: - Configurations
6780

6881
public func applyConfig(_ config: FastisConfig) {
6982
self.backgroundColor = config.controller.backgroundColor
7083

84+
let todayConfig = config.todayCell
7185
let config = config.dayCell
86+
87+
self.todayConfig = todayConfig
7288
self.config = config
89+
7390
self.rightRangeView.backgroundColor = config.onRangeBackgroundColor
7491
self.leftRangeView.backgroundColor = config.onRangeBackgroundColor
7592
self.rightRangeView.layer.cornerRadius = config.rangeViewCornerRadius
@@ -95,10 +112,8 @@ final class DayCell: JTACDayCell {
95112
public func configureConstraints() {
96113
let inset = self.config.rangedBackgroundViewVerticalInset
97114
NSLayoutConstraint.activate([
98-
self.dateLabel.leftAnchor.constraint(equalTo: self.contentView.leftAnchor),
99-
self.dateLabel.rightAnchor.constraint(equalTo: self.contentView.rightAnchor),
100-
self.dateLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor),
101-
self.dateLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)
115+
self.dateLabel.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
116+
self.dateLabel.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor)
102117
])
103118
NSLayoutConstraint.activate([
104119
self.leftRangeView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor),
@@ -267,6 +282,7 @@ final class DayCell: JTACDayCell {
267282
var isSelectedViewHidden = true
268283
var isDateEnabled = true
269284
var rangeView = RangeViewConfig()
285+
var isToday = false
270286
}
271287

272288
internal func configure(for config: ViewConfig) {
@@ -278,14 +294,11 @@ final class DayCell: JTACDayCell {
278294
if let dateLabelText = config.dateLabelText {
279295
self.dateLabel.isHidden = false
280296
self.dateLabel.text = dateLabelText
281-
if !config.isDateEnabled {
282-
self.dateLabel.textColor = self.config.dateLabelUnavailableColor
283-
} else if !config.isSelectedViewHidden {
284-
self.dateLabel.textColor = self.config.selectedLabelColor
285-
} else if !config.rangeView.isHidden {
286-
self.dateLabel.textColor = self.config.onRangeLabelColor
297+
298+
if config.isToday, let todayConfig {
299+
self.configureTodayCell(viewConfig: config, todayConfig: todayConfig)
287300
} else {
288-
self.dateLabel.textColor = self.config.dateLabelColor
301+
self.configureDayCell(viewConfig: config)
289302
}
290303

291304
} else {
@@ -316,6 +329,46 @@ final class DayCell: JTACDayCell {
316329

317330
}
318331

332+
private func configureDayCell(viewConfig: ViewConfig) {
333+
if !viewConfig.isDateEnabled {
334+
self.dateLabel.textColor = self.config.dateLabelUnavailableColor
335+
} else if !viewConfig.isSelectedViewHidden {
336+
self.dateLabel.textColor = self.config.selectedLabelColor
337+
} else if !viewConfig.rangeView.isHidden {
338+
self.dateLabel.textColor = self.config.onRangeLabelColor
339+
} else {
340+
self.dateLabel.textColor = self.config.dateLabelColor
341+
}
342+
}
343+
344+
private func configureTodayCell(viewConfig: ViewConfig, todayConfig: FastisConfig.TodayCell) {
345+
self.dateLabel.font = todayConfig.dateLabelFont
346+
347+
if !viewConfig.isDateEnabled {
348+
self.dateLabel.textColor = todayConfig.dateLabelUnavailableColor
349+
self.circleView.backgroundColor = todayConfig.circleViewUnavailableColor
350+
} else if !viewConfig.isSelectedViewHidden {
351+
self.dateLabel.textColor = todayConfig.selectedLabelColor
352+
self.circleView.backgroundColor = todayConfig.circleViewSelectedColor
353+
} else if !viewConfig.rangeView.isHidden {
354+
self.dateLabel.textColor = todayConfig.onRangeLabelColor
355+
self.circleView.backgroundColor = todayConfig.onRangeLabelColor
356+
} else {
357+
self.dateLabel.textColor = todayConfig.dateLabelColor
358+
self.circleView.backgroundColor = todayConfig.circleViewColor
359+
}
360+
361+
self.circleView.layer.cornerRadius = todayConfig.circleSize * 0.5
362+
self.circleView.removeFromSuperview()
363+
self.contentView.addSubview(self.circleView)
364+
NSLayoutConstraint.activate([
365+
self.circleView.centerXAnchor.constraint(equalTo: self.dateLabel.centerXAnchor),
366+
self.circleView.topAnchor.constraint(equalTo: self.dateLabel.bottomAnchor, constant: todayConfig.circleVerticalInset),
367+
self.circleView.widthAnchor.constraint(equalToConstant: todayConfig.circleSize),
368+
self.circleView.heightAnchor.constraint(equalToConstant: todayConfig.circleSize)
369+
])
370+
}
371+
319372
}
320373

321374
public extension FastisConfig {
@@ -325,7 +378,7 @@ public extension FastisConfig {
325378

326379
Configurable in FastisConfig.``FastisConfig/dayCell-swift.property`` property
327380
*/
328-
struct DayCell {
381+
class DayCell {
329382

330383
/**
331384
Font of date label in cell
@@ -400,4 +453,49 @@ public extension FastisConfig {
400453
public var customSelectionViewCornerRadius: CGFloat?
401454
}
402455

456+
final class TodayCell: DayCell {
457+
458+
/**
459+
Size circle view in cell
460+
461+
Default value — `4pt`
462+
*/
463+
public var circleSize: CGFloat = 4
464+
465+
/**
466+
Color of circle view in cell
467+
468+
Default value — `.label`
469+
*/
470+
public var circleViewColor: UIColor = .systemBlue
471+
472+
/**
473+
Color of circle view in cell when date is unavailable for select
474+
475+
Default value — `.tertiaryLabel`
476+
*/
477+
public var circleViewUnavailableColor: UIColor = .tertiaryLabel
478+
479+
/**
480+
Color of circle view in cell when date is selected
481+
482+
Default value — `.white`
483+
*/
484+
public var circleViewSelectedColor: UIColor = .white
485+
486+
/**
487+
Color of circle view in cell when date is a part of selected range
488+
489+
Default value — `.label`
490+
*/
491+
public var circleViewOnRangeColor: UIColor = .systemBlue
492+
493+
/**
494+
Inset circle view from date label
495+
496+
Default value — `5pt`
497+
*/
498+
public var circleVerticalInset: CGFloat = 3
499+
}
500+
403501
}

0 commit comments

Comments
 (0)