diff --git a/Example/Source/ViewController.swift b/Example/Source/ViewController.swift index 950987f..8b73693 100644 --- a/Example/Source/ViewController.swift +++ b/Example/Source/ViewController.swift @@ -13,7 +13,7 @@ class ViewController: UIViewController { // MARK: - Outlets - lazy var containerView: UIStackView = { + private lazy var containerView: UIStackView = { let view = UIStackView() view.backgroundColor = .clear view.axis = .vertical @@ -26,14 +26,14 @@ class ViewController: UIViewController { private lazy var currentDateLabel = UILabel() - lazy var chooseRangeButton: UIButton = { + private lazy var chooseRangeButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Choose range of dates", for: .normal) button.addTarget(self, action: #selector(self.chooseRange), for: .touchUpInside) return button }() - lazy var chooseSingleButton: UIButton = { + private lazy var chooseSingleButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Choose single date", for: .normal) button.addTarget(self, action: #selector(self.chooseSingleDate), for: .touchUpInside) @@ -42,7 +42,7 @@ class ViewController: UIViewController { // MARK: - Variables - var currentValue: FastisValue? { + private var currentValue: FastisValue? { didSet { let formatter = DateFormatter() formatter.dateFormat = "dd/MM/yyyy" diff --git a/README.md b/README.md index 82ff453..7bea492 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,7 @@ Fastis can be customised global or local. `FastisConfig` have some sections: - `controller` - base view controller (`cancelButtonTitle`, `doneButtonTitle`, etc.) - `monthHeader` - month titles - `dayCell` - day cells (selection parameters, font, etc.) +- `todayCell` - today cell (selection parameters, font, circle view, etc.). If you set `todayCell` to `nil`, view will use `dayCell` instead - `weekView` - top header view with weekday names - `currentValueView` - current value view appearance (clear button, date format, etc.) - `shortcutContainerView` - bottom view with shortcuts @@ -220,6 +221,17 @@ var customConfig = FastisConfig.default customConfig.controller.dayCell.dateLabelColor = .blue let fastisController = FastisController(mode: .range, config: customConfig) ``` +To customise a today cell: + +```swift +let config = FastisConfig.withCurrentDate +config.todayCell.dateLabelColor = .red +config.todayCell.onRangeLabelColor = .red +config.todayCell.circleSize = 4 +config.todayCell.circleViewColor = .red +``` + +If you don't want to customzie today date cell, just set `config.todayCell = nil` and today cell will use `dayCell` config. ## Credits diff --git a/Sources/Models/Config.swift b/Sources/Models/Config.swift index 8dac950..e938645 100644 --- a/Sources/Models/Config.swift +++ b/Sources/Models/Config.swift @@ -27,6 +27,26 @@ public struct FastisConfig { config.monthHeader.labelColor = .red let controller = FastisController(mode: .single, config: config) ``` + Default configuration set showCurrentDate to false. In this case current day will not be indicate + WithCurrentDate configuration set showCurrentDateto true. In this case current day will be indicate + + You can customized indication current day: + ```swift + let config = FastisConfig.withCurrentDate + // date label + config.todayCell.dateLabelColor = .red + config.todayCell.selectedLabelColor = .orange + config.todayCell.onRangeLabelColor = .green + config.todayCell.dateLabelUnavailableColor = .cyan + + // circle view + config.todayCell.circleSize = 4 + config.todayCell.circleVerticalInset = 5 + config.todayCell.circleViewColor = .red + config.todayCell.circleViewSelectedColor = .orange + config.todayCell.circleViewOnRangeColor = .green + config.todayCell.circleViewUnavailableColor = .cyan + ``` */ public static var `default` = FastisConfig() @@ -48,6 +68,9 @@ public struct FastisConfig { /// Day cells (selection parameters, font, etc.) public var dayCell = FastisConfig.DayCell() + /// Today cell (selection parameters, font, etc.) + public var todayCell: FastisConfig.TodayCell? = FastisConfig.TodayCell() + /// Top header view with week day names public var weekView = FastisConfig.WeekView() diff --git a/Sources/Views/Controller.swift b/Sources/Views/Controller.swift index 6ac8f21..171aaed 100644 --- a/Sources/Views/Controller.swift +++ b/Sources/Views/Controller.swift @@ -418,6 +418,10 @@ open class FastisController: UIViewController, JTACMonthView newConfig.dateLabelText = self.dayFormatter.string(from: date) } + if Calendar.current.isDateInToday(date) { + newConfig.isToday = true + } + self.viewConfigs[indexPath] = newConfig cell.applyConfig(self.config) cell.configure(for: newConfig) diff --git a/Sources/Views/DayCell.swift b/Sources/Views/DayCell.swift index 751c9dc..2fc5fe9 100644 --- a/Sources/Views/DayCell.swift +++ b/Sources/Views/DayCell.swift @@ -20,6 +20,13 @@ final class DayCell: JTACDayCell { 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 @@ -46,6 +53,7 @@ final class DayCell: JTACDayCell { // 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] = [] @@ -63,13 +71,22 @@ final class DayCell: JTACDayCell { fatalError("init(coder:) has not been implemented") } + override func prepareForReuse() { + super.prepareForReuse() + self.circleView.removeFromSuperview() + } + // MARK: - Configurations public func applyConfig(_ config: FastisConfig) { self.backgroundColor = config.controller.backgroundColor + let todayConfig = config.todayCell let config = config.dayCell + + self.todayConfig = todayConfig self.config = config + self.rightRangeView.backgroundColor = config.onRangeBackgroundColor self.leftRangeView.backgroundColor = config.onRangeBackgroundColor self.rightRangeView.layer.cornerRadius = config.rangeViewCornerRadius @@ -95,10 +112,8 @@ final class DayCell: JTACDayCell { public func configureConstraints() { let inset = self.config.rangedBackgroundViewVerticalInset NSLayoutConstraint.activate([ - self.dateLabel.leftAnchor.constraint(equalTo: self.contentView.leftAnchor), - self.dateLabel.rightAnchor.constraint(equalTo: self.contentView.rightAnchor), - self.dateLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor), - self.dateLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor) + self.dateLabel.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor), + self.dateLabel.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor) ]) NSLayoutConstraint.activate([ self.leftRangeView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor), @@ -267,6 +282,7 @@ final class DayCell: JTACDayCell { var isSelectedViewHidden = true var isDateEnabled = true var rangeView = RangeViewConfig() + var isToday = false } internal func configure(for config: ViewConfig) { @@ -278,14 +294,11 @@ final class DayCell: JTACDayCell { if let dateLabelText = config.dateLabelText { self.dateLabel.isHidden = false self.dateLabel.text = dateLabelText - if !config.isDateEnabled { - self.dateLabel.textColor = self.config.dateLabelUnavailableColor - } else if !config.isSelectedViewHidden { - self.dateLabel.textColor = self.config.selectedLabelColor - } else if !config.rangeView.isHidden { - self.dateLabel.textColor = self.config.onRangeLabelColor + + if config.isToday, let todayConfig { + self.configureTodayCell(viewConfig: config, todayConfig: todayConfig) } else { - self.dateLabel.textColor = self.config.dateLabelColor + self.configureDayCell(viewConfig: config) } } else { @@ -316,6 +329,46 @@ final class DayCell: JTACDayCell { } + 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 { @@ -325,7 +378,7 @@ public extension FastisConfig { Configurable in FastisConfig.``FastisConfig/dayCell-swift.property`` property */ - struct DayCell { + class DayCell { /** Font of date label in cell @@ -400,4 +453,49 @@ public extension FastisConfig { 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 + } + }