Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #28 Add indication current date #36

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Example/Source/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
23 changes: 23 additions & 0 deletions Sources/Models/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions Sources/Views/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ open class FastisController<Value: FastisValue>: 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)
Expand Down
122 changes: 110 additions & 12 deletions Sources/Views/DayCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = []

Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}

}