Skip to content

Commit

Permalink
fix #31 Add option to hide currentValueView (#34)
Browse files Browse the repository at this point in the history
* fix #31 Add option to hide currentValueView

- logic hide currentValueView (flag closeOnSelectionImmediately)
- logic dismiss controller after select date in .single mode (flag closeOnSelectionImmediately)
- logic reset date for repeated tap on date in .single mode (flag allowToChooseNilDate)

* fix #31 Add text documentation about closeOnSelectionImmediately
  • Loading branch information
UriyDevyataev authored Aug 30, 2023
1 parent 596a57f commit bb686e0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@ var minimumDate: Date? = nil
var maximumDate: Date? = nil
var selectMonthOnHeaderTap: Bool = true
var allowDateRangeChanges: Bool = true
var closeOnSelectionImmediately: Bool = false

```

- `shortcuts`- Shortcuts array. The default value is `[]`. See [Shortcuts](#shortcuts) section
- `allowsToChooseNilDate`- Allow to choose `nil` date. If you set `true`, the done button will always be enabled. The default value is `false`.
- `allowsToChooseNilDate`- Allow to choose `nil` date. If you set `true`, the done button will always be enabled. And in `.single` mode you can reset the date when you tapped on it again. The default value is `false`.
- `dismissHandler`- The block to execute after the dismissal finishes. The default value is `nil`. Return DismissAction.done(FastisValue?) after the "Done" button will be tapped or DismissAction.cancel when controller dismissed without tapped the "Done" button.
- `initialValue`- And initial value which will be selected by default. The default value is `nil`.
- `minimumDate`- Minimal selection date. Dates less than current will be marked as unavailable. The default value is `nil`.
- `maximumDate`- Maximum selection date. Dates more significant than current will be marked as unavailable. The default value is `nil`.
- `selectMonthOnHeaderTap` (Only for `.range` mode) - Set this variable to `true` if you want to allow select date ranges by tapping on months. The default value is `true`.
- `allowDateRangeChanges` (Only for `.range` mode) - Set this variable to `false` if you want to disable date range changes. Next tap after selecting a range will start a new range selection. The default value is `true`.
- `closeOnSelectionImmediately` (Only for `.single` mode) - Set this variable to `true` if you want to hide view of the selected date and close the controller right after the date is selected. The default value is `false`

### Shortcuts

Expand Down
84 changes: 60 additions & 24 deletions Sources/Views/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import UIKit
```swift
let fastisController = FastisController(mode: .single)
fastisController.initialValue = Date()
fastisController.closeOnSelectionImmediately = true
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let resultDate):
Expand Down Expand Up @@ -129,15 +130,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
view.currentValue = self.value
view.translatesAutoresizingMaskIntoConstraints = false
view.onClear = { [weak self] in
guard let self else { return }
self.value = nil
self.viewConfigs.removeAll()
self.calendarView.deselectAllDates()
self.calendarView.visibleDates { segment in
UIView.performWithoutAnimation {
self.calendarView.reloadItems(at: (segment.outdates + segment.indates).map(\.indexPath))
}
}
self?.clear()
}
return view
}()
Expand Down Expand Up @@ -183,6 +176,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
}

private var isDone = false
private var privateCloseOnSelectionImmediately = false

/**
Shortcuts array
Expand All @@ -206,7 +200,8 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
/**
Allow to choose `nil` date

If you set `true` done button will be always enabled
If you set `true` done button will be always enabled.
And in `.single` mode you can reset the date when you tapped on it again
*/
public var allowToChooseNilDate = false

Expand Down Expand Up @@ -330,7 +325,9 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: self.monthHeaderReuseIdentifier
)
self.view.addSubview(self.currentValueView)
if !self.privateCloseOnSelectionImmediately {
self.view.addSubview(self.currentValueView)
}
self.view.addSubview(self.weekView)
self.view.addSubview(self.calendarView)
if !self.shortcuts.isEmpty {
Expand All @@ -339,16 +336,24 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
}

private func configureConstraints() {
NSLayoutConstraint.activate([
self.currentValueView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.currentValueView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 12),
self.currentValueView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -12)
])
NSLayoutConstraint.activate([
self.weekView.topAnchor.constraint(equalTo: self.currentValueView.bottomAnchor),
self.weekView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 12),
self.weekView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -12)
])
if !self.privateCloseOnSelectionImmediately {
NSLayoutConstraint.activate([
self.currentValueView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.currentValueView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 12),
self.currentValueView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -12)
])
NSLayoutConstraint.activate([
self.weekView.topAnchor.constraint(equalTo: self.currentValueView.bottomAnchor),
self.weekView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 12),
self.weekView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -12)
])
} else {
NSLayoutConstraint.activate([
self.weekView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.weekView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 12),
self.weekView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -12)
])
}
if !self.shortcuts.isEmpty {
NSLayoutConstraint.activate([
self.shortcutContainerView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
Expand Down Expand Up @@ -448,9 +453,17 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView

switch Value.mode {
case .single:
self.value = date as? Value
self.selectValue(date as? Value, in: calendar)
return
let oldDate = self.value as? Date
if oldDate == date, self.allowToChooseNilDate {
self.clear()
} else {
self.value = date as? Value
self.selectValue(date as? Value, in: calendar)
}

if self.privateCloseOnSelectionImmediately, self.value != nil {
self.done()
}

case .range:
var newValue: FastisRange!
Expand Down Expand Up @@ -500,6 +513,17 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
}
}

private func clear() {
self.value = nil
self.viewConfigs.removeAll()
self.calendarView.deselectAllDates()
self.calendarView.visibleDates { segment in
UIView.performWithoutAnimation {
self.calendarView.reloadItems(at: (segment.outdates + segment.indates).map(\.indexPath))
}
}
}

// MARK: - JTACMonthViewDelegate

public func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
Expand Down Expand Up @@ -678,6 +702,18 @@ public extension FastisController where Value == Date {
self.init(config: config)
}

/**
Set this variable to `true` if you want to hide view of the selected date and close the controller right after the date is selected. Default value — `"False"`
*/
var closeOnSelectionImmediately: Bool {
get {
self.privateCloseOnSelectionImmediately
}
set {
self.privateCloseOnSelectionImmediately = newValue
}
}

}

public extension FastisConfig {
Expand Down

0 comments on commit bb686e0

Please sign in to comment.