Skip to content

Commit

Permalink
- removed initialDate
Browse files Browse the repository at this point in the history
- small fixes
- updated README file
  • Loading branch information
rursache committed May 31, 2023
1 parent c38ecfe commit 0f5853a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
42 changes: 20 additions & 22 deletions Files/RSDatePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ open class RSDatePicker: UIView {
}

// config
public var initialDate: Date?
public var currentDate: Date? {
didSet {
DispatchQueue.main.async {
self.didUpdateDate()
}
}
}
public var minimumDate: Date? {
didSet {
self.checkDateLimits()
Expand Down Expand Up @@ -95,7 +101,7 @@ open class RSDatePicker: UIView {
self.dateLabel.textColor = self.textColor
}
}
public var initialText: String? {
public var initialText: String = "Select date" {
didSet {
guard self.currentDate == nil else { return }
self.didUpdateDate()
Expand All @@ -122,13 +128,7 @@ open class RSDatePicker: UIView {
}
}
public var forceScalePicker: Bool = false
public var currentDate: Date? {
didSet {
DispatchQueue.main.async {
self.didUpdateDate()
}
}
}

override open var backgroundColor: UIColor? {
didSet {
guard self.view != nil else { return }
Expand Down Expand Up @@ -179,37 +179,35 @@ open class RSDatePicker: UIView {
self.datePicker.layer.zPosition = CGFloat(MAXFLOAT)
self.datePicker.datePickerMode = self.pickerMode?.datePickerMode ?? .date
self.datePicker.alpha = 0.03
self.datePicker.date = self.initialDate ?? Date()
self.checkDateLimits()
self.updateMargins()

self.dateLabel.text = self.initialText
}

private func didUpdateDate() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = self.dateFormat ?? (self.pickerMode?.defaultFormat ?? PickerMode.date.defaultFormat)

guard let currentDate = self.currentDate else {
if let initialText = self.initialText {
self.dateLabel.text = initialText
} else if let initialDate = self.initialDate {
self.dateLabel.text = dateFormatter.string(from: initialDate)
} else {
self.dateLabel.text = dateFormatter.string(from: Date())
}
self.dateLabel.text = self.initialText
return
}


self.datePicker.date = currentDate
self.dateLabel.text = dateFormatter.string(from: currentDate)
}

private func checkDateLimits() {
self.datePicker.minimumDate = self.minimumDate
self.datePicker.maximumDate = self.maximumDate

guard let currentDate = self.currentDate else { return }

if let minimumDate = self.minimumDate, self.datePicker.date < minimumDate {
self.datePicker.date = minimumDate
} else if let maximumDate = self.maximumDate, self.datePicker.date > maximumDate {
self.datePicker.date = maximumDate
if let minimumDate = self.minimumDate, currentDate < minimumDate {
self.currentDate = minimumDate
} else if let maximumDate = self.maximumDate, currentDate > maximumDate {
self.currentDate = maximumDate
}
}

Expand Down
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RSDatePicker

A beautiful UIView wrapper for UIDatePicker. Requires iOS 13.2 or newer
A beautiful UIView wrapper for UIDatePicker. Requires iOS 13.4 or newer

## Setup
- Storyboard
Expand All @@ -15,21 +15,51 @@ Create a `UIView` and set its custom class to `RSDatePicker` and link it with a

You can customize the date picker in multiple ways:

- `initialDate` sets the default date
- `minimumDate` sets the minimum selectable date
- `maximumDate` sets the maximum selectable date
- `pickerMode` sets the date picker mode (`.date` or `time`)
- `dateFormat` sets the date format for the visible label
- `currentDate` sets the default date (optional)
- `initialText` sets the default text if a date is not selected (optional)
- `minimumDate` sets the minimum selectable date (optional)
- `maximumDate` sets the maximum selectable date (optional)
- `pickerMode` sets the date picker mode (`.date` or `time`) (optional)
- `dateFormat` sets the date format for the visible label (optional)
- `closeWhenSelectingDate` allows you to enable/disable closing after a date was picked
- `closeAnimationDuration` controls the animation speed when closing the picker
- `calendarIconIsHidden` controls if the calendar icon is hidden
- `calendarIconSizeMultiplier` controls the multiplier of the aspect ratio of the calendar imageview

You can change the margins (`left/right/top/bottomMargin`) of the container view
- `left/right/top/bottomMargin` changes the margins of the container view

There is also a callback for receiving the new picked date: `didChangeDate`

You can also get or set the current date whenever you need by using `currentDate`
You can also get the current date whenever you need by using `currentDate`

## Full example

```swift
import RSDatePicker

(...)

@IBOutlet weak var datePicker: RSDatePicker!

(...)

self.datePicker.cornerRadius = 16
self.datePicker.leftMargin = 20
self.datePicker.rightMargin = 16

self.datePicker.pickerMode = .date
self.datePicker.currentDate = Calendar.current.date(byAdding: .day, value: -1, to: Date()) // yesterday
self.datePicker.initialText = "Select Date"
self.datePicker.calendarIconIsHidden = false
self.datePicker.calendarIconTint = .label
self.datePicker.calendarIconSizeMultiplier = 0.8
self.datePicker.forceScalePicker = true
self.datePicker.closeWhenSelectingDate = true
self.datePicker.minimumDate = Calendar.current.date(byAdding: .day, value: -4, to: Date()) // 4 days ago
self.datePicker.maximumDate = Calendar.current.date(byAdding: .day, value: 20, to: Date()) // 20 days in the future
self.datePicker.didChangeDate = { newDate in
print(newDate)
}
```

## License
RSDatePicker is available under the **MPL-2.0 license**. More info available [here](https://www.mozilla.org/en-US/MPL/2.0/).

0 comments on commit 0f5853a

Please sign in to comment.