Skip to content
This repository has been archived by the owner on Jun 23, 2019. It is now read-only.

Show the actual month of the selected date #48

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Koyomi.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Koyomi'
s.version = '1.2.7'
s.version = '1.2.8'
s.summary = 'Simple customizable calendar component in Swift'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change shouldn't be included in this pull request.

s.description = <<-DESC

Expand Down
12 changes: 12 additions & 0 deletions Koyomi/DateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extension Date {
func formated(withFormat format: String = "yyyy/MM/dd") -> Date? {
let formatter: DateFormatter = .init()
formatter.dateFormat = format
formatter.timeZone = TimeZone.current
formatter.locale = Locale.current
let dateString = formatter.string(from: self)
return formatter.date(from: dateString)
}
Expand All @@ -24,6 +26,16 @@ extension Date {
return nil
}

func monthDifference(fromDate date: Date) -> Int {

let currentCalendar = Calendar.current

guard let startMonth = currentCalendar.ordinality(of: .month, in: .era, for: date) else { return 0 }
guard let endMonth = currentCalendar.ordinality(of: .month, in: .era, for: self) else { return 0 }

return endMonth - startMonth
}

private func dateFromComponents(_ date: Date) -> Date? {
let calender = Calendar.current
let components = calender.dateComponents([.year, .month, .day], from: date)
Expand Down
12 changes: 11 additions & 1 deletion Koyomi/DateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@

import UIKit

public enum MonthType { case previous, current, next }
public enum MonthType: Equatable { case previous, current, next, specific(diff: Int) }
public func ==(lhs: MonthType, rhs: MonthType) -> Bool {
switch (lhs, rhs) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should define this function in MonthType.

case (.previous, .previous): return true
case (.next, .next): return true
case (.current, .current): return true
case (.specific(let leftFrom), .specific(let rightFrom)): return leftFrom == rightFrom
default: return false
}
}

final class DateModel: NSObject {

Expand Down Expand Up @@ -347,6 +356,7 @@ private extension DateModel {
case .previous: return -1
case .current: return 0
case .next: return 1
case .specific(let diff): return diff
}
}()
return calendar.date(byAdding: components, to: currentDate) ?? Date()
Expand Down
11 changes: 10 additions & 1 deletion Koyomi/Koyomi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,16 @@ final public class Koyomi: UICollectionView {

@discardableResult
public func select(date: Date, to toDate: Date? = nil) -> Self {
model.select(from: date, to: toDate)
// Format `now` and `from` date
let now = Date().formated() ?? Date()
let fromDate = date.formated() ?? date

// Calculate the month difference between `now` and `from` and display the month of the selected date
let difference = fromDate.monthDifference(fromDate: now)
display(in: .specific(diff: difference))

model.select(from: fromDate, to: toDate)

return self
}

Expand Down
4 changes: 3 additions & 1 deletion Koyomi/KoyomiCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ final class KoyomiCell: UICollectionViewCell {
func configureAppearanse(of style: CellStyle, withColor color: UIColor, backgroundColor: UIColor, isSelected: Bool) {
switch style {
case .standard:
self.backgroundColor = isSelected ? color : backgroundColor
self.backgroundColor = UIColor.clear
self.layer.borderWidth = isSelected ? 1.0 : 0.0
self.layer.borderColor = isSelected ? color.cgColor : backgroundColor.cgColor

circularView.isHidden = true
lineView.isHidden = true
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ Set custom class of `UICollectionView ` to `Koyomi`
If you want to change displayed month, call `display(in: MonthType)`. `MonthType` is defined by three types.

```swift
public enum MonthType { case previous, current, next }
public enum MonthType { case previous, current, next, specific(diff: Int) }

// change month
koyomi.display(in: .next)

// go back 4 months
koyomi.display(in: .specific(diff: -4))
```

### Hide days of other months
Expand Down Expand Up @@ -173,6 +176,13 @@ You can select specific date .
// If want to select multiple day.
let dates: [Date] = [date1, date2, date3]
koyomi.select(dates: dates)

// ... or you can go around the world in 80 days and calculate when is your arrival date :)
let components = DateComponents.init(calendar: Calendar.current, day: +80)

if let eightyDays = Calendar.current.date(byAdding: components, to: today) {
koyomi.select(date: eightyDays) // new feature: the calendar will actualy show that date as selected
}
```

You can also unselect available.
Expand Down