Skip to content

Commit

Permalink
fix #29 - Create one dismissHandler with two param instead dismissHa… (
Browse files Browse the repository at this point in the history
  • Loading branch information
UriyDevyataev authored Aug 29, 2023
1 parent a335bdb commit 562fdcf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 32 deletions.
18 changes: 14 additions & 4 deletions Example/Source/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,13 @@ class ViewController: UIViewController {
fastisController.maximumDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())
fastisController.allowToChooseNilDate = true
fastisController.shortcuts = [.today, .lastWeek, .lastMonth]
fastisController.doneHandler = { [weak self] newValue in
self?.currentValue = newValue
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let newValue):
self?.currentValue = newValue
case .cancel:
print("any actions")
}
}
fastisController.present(above: self)
}
Expand All @@ -120,8 +125,13 @@ class ViewController: UIViewController {
fastisController.initialValue = self.currentValue as? Date
fastisController.maximumDate = Date()
fastisController.shortcuts = [.today, .yesterday, .tomorrow]
fastisController.doneHandler = { [weak self] newDate in
self?.currentValue = newDate
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let newValue):
self?.currentValue = newValue
case .cancel:
print("any actions")
}
}
fastisController.present(above: self)
}
Expand Down
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ class MyViewController: UIViewController {
fastisController.maximumDate = Date()
fastisController.allowToChooseNilDate = true
fastisController.shortcuts = [.today, .lastWeek]
fastisController.doneHandler = { resultRange in
...
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let newValue):
...
case .cancel:
...
}
}
fastisController.present(above: self)
}
Expand All @@ -117,8 +122,13 @@ If you want to get a single date, you have to use the `Date` type:
```swift
let fastisController = FastisController(mode: .single)
fastisController.initialValue = Date()
fastisController.doneHandler = { resultDate in
print(resultDate) // resultDate is Date
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let resultDate):
print(resultDate) // resultDate is Date
case .cancel:
...
}
}
```

Expand All @@ -127,8 +137,13 @@ If you want to get a date range, you have to use the `FastisRange` type:
```swift
let fastisController = FastisController(mode: .range)
fastisController.initialValue = FastisRange(from: Date(), to: Date()) // or .from(Date(), to: Date())
fastisController.doneHandler = { resultRange in
print(resultRange) // resultDate is FastisRange
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let resultRange):
print(resultRange) // resultRange is FastisRange
case .cancel:
...
}
}
```

Expand All @@ -139,8 +154,7 @@ FastisController has the following default configuration parameters:
```swift
var shortcuts: [FastisShortcut<Value>] = []
var allowsToChooseNilDate: Bool = false
var dismissHandler: (() -> Void)? = nil
var doneHandler: ((Value?) -> Void)? = nil
var dismissHandler: ((DismissAction) -> Void)? = nil
var initialValue: Value? = nil
var minimumDate: Date? = nil
var maximumDate: Date? = nil
Expand All @@ -150,8 +164,7 @@ var allowDateRangeChanges: Bool = true

- `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`.
- `dismissHandler`- The block to execute after the dismissal finishes. The default value is `nil`.
- `doneHandler`- The block to execute after the "Done" button will be tapped. The default value is `nil`.
- `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`.
Expand Down
5 changes: 5 additions & 0 deletions Sources/Models/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public enum FastisModeRange {
case range
}

public enum DismissAction {
case done(FastisValue?)
case cancel
}

extension Date: FastisValue {

/// Mode of value for ``FastisController``. Always `.single`
Expand Down
56 changes: 38 additions & 18 deletions Sources/Views/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ import UIKit
fastisController.maximumDate = Date()
fastisController.allowToChooseNilDate = true
fastisController.shortcuts = [.today, .lastWeek]
fastisController.doneHandler = { resultRange in
...
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let newValue):
...
case .cancel:
...
}
}
fastisController.present(above: self)
```
Expand All @@ -32,8 +37,13 @@ import UIKit
```swift
let fastisController = FastisController(mode: .single)
fastisController.initialValue = Date()
fastisController.doneHandler = { resultDate in
print(resultDate) // resultDate is Date
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let resultDate):
print(resultDate) // resultDate is Date
case .cancel:
...
}
}
```

Expand All @@ -42,8 +52,13 @@ import UIKit
```swift
let fastisController = FastisController(mode: .range)
fastisController.initialValue = FastisRange(from: Date(), to: Date()) // or .from(Date(), to: Date())
fastisController.doneHandler = { resultRange in
print(resultRange) // resultDate is FastisRange
fastisController.dismissHandler = { [weak self] action in
switch action {
case .done(let resultRange):
print(resultRange) // resultRange is FastisRange
case .cancel:
...
}
}
```
*/
Expand Down Expand Up @@ -166,6 +181,7 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
self.doneBarButtonItem.isEnabled = self.allowToChooseNilDate || self.value != nil
}
}
private var isDone = false

/**
Shortcuts array
Expand Down Expand Up @@ -194,14 +210,9 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
public var allowToChooseNilDate = false

/**
The block to execute after the dismissal finishes
*/
public var dismissHandler: (() -> Void)?

/**
The block to execute after "Done" button will be tapped
The block to execute after the dismissal finishes, return two variable .done(FastisValue?) and .cancel
*/
public var doneHandler: ((Value?) -> Void)?
public var dismissHandler: ((DismissAction) -> Void)?

/**
And initial value which will be selected by default
Expand Down Expand Up @@ -265,6 +276,17 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView
self.configureInitialState()
}

open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

if self.isDone {
self.dismissHandler?(.done(self.value))
} else {
self.dismissHandler?(.cancel)
}

}

/**
Present FastisController above current top view controller

Expand Down Expand Up @@ -398,15 +420,13 @@ open class FastisController<Value: FastisValue>: UIViewController, JTACMonthView

@objc
private func cancel() {
self.navigationController?.dismiss(animated: true, completion: {
self.dismissHandler?()
})
self.dismiss(animated: true)
}

@objc
private func done() {
self.doneHandler?(self.value)
self.cancel()
self.isDone = true
self.dismiss(animated: true)
}

private func selectValue(_ value: Value?, in calendar: JTACMonthView) {
Expand Down

0 comments on commit 562fdcf

Please sign in to comment.