Skip to content

Commit

Permalink
fix #29 add indication for current day
Browse files Browse the repository at this point in the history
- change DayCell struct to class
- child class TodayCell from DayCell
  • Loading branch information
UriyDevyataev committed Sep 7, 2023
1 parent e073dfc commit 2d6ffc0
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 15 deletions.
17 changes: 16 additions & 1 deletion Example/Source/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,22 @@ class ViewController: UIViewController {

@objc
private func chooseRange() {
let fastisController = FastisController(mode: .range)
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 = 6
config.todayCell.circleVerticalInset = 2
config.todayCell.circleViewColor = .red
config.todayCell.circleViewSelectedColor = .orange
config.todayCell.circleViewOnRangeColor = .green
config.todayCell.circleViewUnavailableColor = .cyan

let fastisController = FastisController(mode: .range, config: config)
fastisController.title = "Choose range"
fastisController.initialValue = self.currentValue as? FastisRange
fastisController.minimumDate = Calendar.current.date(byAdding: .month, value: -2, to: Date())
Expand Down
19 changes: 19 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, etc.)
- `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,24 @@ 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
// 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
```

## Credits

Expand Down
32 changes: 30 additions & 2 deletions Sources/Models/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,33 @@ 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()
public static var `default` = FastisConfig(showCurrentDate: false)
public static var `withCurrentDate` = FastisConfig(showCurrentDate: true)

private init() { }
private init(showCurrentDate: Bool) {
self.showCurrentDate = showCurrentDate
}

/**
Base calendar used to build a view
Expand All @@ -48,6 +71,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()

/// Top header view with week day names
public var weekView = FastisConfig.WeekView()

Expand All @@ -60,4 +86,6 @@ public struct FastisConfig {
/// Shortcut item in the bottom view
public var shortcutItemView = FastisConfig.ShortcutItemView()

/// Flag to show or hide the current date indication
public let showCurrentDate: Bool
}
6 changes: 6 additions & 0 deletions Sources/Views/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
newConfig.dateLabelText = self.dayFormatter.string(from: date)
}

if self.config.showCurrentDate,
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 {
self.configAsTodayCell(viewConfig: config)
} else {
self.dateLabel.textColor = self.config.dateLabelColor
self.configAsDayCell(viewConfig: config)
}

} else {
Expand Down Expand Up @@ -316,6 +329,46 @@ final class DayCell: JTACDayCell {

}

private func configAsDayCell(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 configAsTodayCell(viewConfig: ViewConfig) {
self.dateLabel.font = self.todayConfig.dateLabelFont

if !viewConfig.isDateEnabled {
self.dateLabel.textColor = self.todayConfig.dateLabelUnavailableColor
self.circleView.backgroundColor = self.todayConfig.circleViewUnavailableColor
} else if !viewConfig.isSelectedViewHidden {
self.dateLabel.textColor = self.todayConfig.selectedLabelColor
self.circleView.backgroundColor = self.todayConfig.circleViewSelectedColor
} else if !viewConfig.rangeView.isHidden {
self.dateLabel.textColor = self.todayConfig.onRangeLabelColor
self.circleView.backgroundColor = self.todayConfig.onRangeLabelColor
} else {
self.dateLabel.textColor = self.todayConfig.dateLabelColor
self.circleView.backgroundColor = self.todayConfig.circleViewColor
}

self.circleView.layer.cornerRadius = self.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: self.todayConfig.circleVerticalInset),
self.circleView.widthAnchor.constraint(equalToConstant: self.todayConfig.circleSize),
self.circleView.heightAnchor.constraint(equalToConstant: self.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?
}

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 = .label

/**
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 = .label

/**
Inset circle view from date label

Default value — `5pt`
*/
public var circleVerticalInset: CGFloat = 3
}

}

0 comments on commit 2d6ffc0

Please sign in to comment.