Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to Swift3, make it more swifty! #42

Open
wants to merge 2 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
37 changes: 21 additions & 16 deletions EPCalendar/EPCalendarPicker/EPCalendarPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ open class EPCalendarPicker: UICollectionViewController {

let startDate = Date(year: startYear, month: 1, day: 1)
let firstDayOfMonth = startDate.dateByAddingMonths(section)
let addingPrefixDaysWithMonthDyas = ( firstDayOfMonth.numberOfDaysInMonth() + firstDayOfMonth.weekday() - Calendar.current.firstWeekday )

let addingPrefixDaysWithMonthDyas = (firstDayOfMonth.numberOfDaysInMonth + firstDayOfMonth.weekday - Calendar.current.firstWeekday)

let addingSuffixDays = addingPrefixDaysWithMonthDyas%7
var totalNumber = addingPrefixDaysWithMonthDyas
if addingSuffixDays != 0 {
totalNumber = totalNumber + (7 - addingSuffixDays)
// totalNumber = totalNumber + (7 - addingSuffixDays)
}

return totalNumber
Expand All @@ -189,25 +191,26 @@ open class EPCalendarPicker: UICollectionViewController {

let calendarStartDate = Date(year:startYear, month: 1, day: 1)
let firstDayOfThisMonth = calendarStartDate.dateByAddingMonths(indexPath.section)
let prefixDays = ( firstDayOfThisMonth.weekday() - Calendar.current.firstWeekday)

let prefixDays = (firstDayOfThisMonth.weekday - Calendar.current.firstWeekday)

if indexPath.row >= prefixDays {
cell.isCellSelectable = true
let currentDate = firstDayOfThisMonth.dateByAddingDays(indexPath.row-prefixDays)
let nextMonthFirstDay = firstDayOfThisMonth.dateByAddingDays(firstDayOfThisMonth.numberOfDaysInMonth()-1)
let nextMonthFirstDay = firstDayOfThisMonth.dateByAddingDays(firstDayOfThisMonth.numberOfDaysInMonth-1)

cell.currentDate = currentDate
cell.lblDay.text = "\(currentDate.day())"
cell.lblDay.text = "\(currentDate.day)"

if arrSelectedDates.filter({ $0.isDateSameDay(currentDate)
}).count > 0 && (firstDayOfThisMonth.month() == currentDate.month()) {
}).count > 0 && (firstDayOfThisMonth.month == currentDate.month) {

cell.selectedForLabelColor(dateSelectionColor)
}
else{
cell.deSelectedForLabelColor(weekdayTintColor)

if cell.currentDate.isSaturday() || cell.currentDate.isSunday() {
if cell.currentDate.isSaturday || cell.currentDate.isSunday {
cell.lblDay.textColor = weekendTintColor
}
if (currentDate > nextMonthFirstDay) {
Expand All @@ -218,12 +221,14 @@ open class EPCalendarPicker: UICollectionViewController {
cell.lblDay.textColor = self.dayDisabledTintColor
}
}
if currentDate.isToday() && hightlightsToday {
if currentDate.isToday && hightlightsToday {
cell.setTodayCellColor(todayTintColor)
}

if startDate != nil {
if Calendar.current.startOfDay(for: cell.currentDate as Date) < Calendar.current.startOfDay(for: startDate!) {

if Calendar.current.startOfDay(for: cell.currentDate) < Calendar.current.startOfDay(for: startDate!) {

cell.isCellSelectable = false
cell.lblDay.textColor = self.dayDisabledTintColor
}
Expand All @@ -235,7 +240,7 @@ open class EPCalendarPicker: UICollectionViewController {
cell.isCellSelectable = false
let previousDay = firstDayOfThisMonth.dateByAddingDays(-( prefixDays - indexPath.row))
cell.currentDate = previousDay
cell.lblDay.text = "\(previousDay.day())"
cell.lblDay.text = "\(previousDay.day)"
if hideDaysFromOtherMonth {
cell.lblDay.textColor = UIColor.clear
} else {
Expand Down Expand Up @@ -269,7 +274,7 @@ open class EPCalendarPicker: UICollectionViewController {
let startDate = Date(year: startYear, month: 1, day: 1)
let firstDayOfMonth = startDate.dateByAddingMonths(indexPath.section)

header.lblTitle.text = firstDayOfMonth.monthNameFull()
header.lblTitle.text = firstDayOfMonth.monthNameFull
header.lblTitle.textColor = monthTitleColor
header.updateWeekdaysLabelColor(weekdayTintColor)
header.updateWeekendLabelColor(weekendTintColor)
Expand All @@ -296,21 +301,21 @@ open class EPCalendarPicker: UICollectionViewController {
arrSelectedDates.append(cell.currentDate)
cell.selectedForLabelColor(dateSelectionColor)

if cell.currentDate.isToday() {
if cell.currentDate.isToday {
cell.setTodayCellColor(dateSelectionColor)
}
}
else {
arrSelectedDates = arrSelectedDates.filter(){
return !($0.isDateSameDay(cell.currentDate))
}
if cell.currentDate.isSaturday() || cell.currentDate.isSunday() {
if cell.currentDate.isSaturday || cell.currentDate.isSunday {
cell.deSelectedForLabelColor(weekendTintColor)
}
else {
cell.deSelectedForLabelColor(weekdayTintColor)
}
if cell.currentDate.isToday() && hightlightsToday{
if cell.currentDate.isToday && hightlightsToday{
cell.setTodayCellColor(todayTintColor)
}
}
Expand Down Expand Up @@ -345,8 +350,8 @@ open class EPCalendarPicker: UICollectionViewController {

open func scrollToMonthForDate (_ date: Date) {

let month = date.month()
let year = date.year()
let month = date.month
let year = date.year
let section = ((year - startYear) * 12) + month
let indexPath = IndexPath(row:1, section: section-1)

Expand Down
102 changes: 47 additions & 55 deletions EPCalendar/EPCalendarPicker/EPExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2015 Prabaharan Elangovan. All rights reserved.
//

import Foundation
import UIKit

//MARK: UIViewController Extensions
Expand Down Expand Up @@ -50,145 +49,138 @@ extension Date {

}

func firstDayOfMonth () -> Date {
var firstDayOfMonth: Date {
let calendar = Calendar.current
var dateComponent = (calendar as NSCalendar).components([.year, .month, .day ], from: self)
var dateComponent = calendar.dateComponents([.year, .month, .day], from: self)

dateComponent.day = 1
return calendar.date(from: dateComponent)!
}

init(year : Int, month : Int, day : Int) {

let calendar = Calendar.current
var dateComponent = DateComponents()
dateComponent.year = year
dateComponent.month = month
dateComponent.day = day
self = calendar.date(from: dateComponent)!
self = Date(timeInterval: 0, since: calendar.date(from: dateComponent)!)
}

func dateByAddingMonths(_ months : Int ) -> Date {
let calendar = Calendar.current
var dateComponent = DateComponents()
dateComponent.month = months
return (calendar as NSCalendar).date(byAdding: dateComponent, to: self, options: NSCalendar.Options.matchNextTime)!
return Calendar.current.date(byAdding: .month, value: months, to: self)!
}

func dateByAddingDays(_ days : Int ) -> Date {
let calendar = Calendar.current
var dateComponent = DateComponents()
dateComponent.day = days
return (calendar as NSCalendar).date(byAdding: dateComponent, to: self, options: NSCalendar.Options.matchNextTime)!
return Calendar.current.date(byAdding: .day, value: days, to: self)!

}

func hour() -> Int {
var hour: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.hour, from: self)
let dateComponent = calendar.dateComponents([.hour], from: self)
return dateComponent.hour!
}

func second() -> Int {
var second: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.second, from: self)
let dateComponent = calendar.dateComponents([.second], from: self)
return dateComponent.second!
}

func minute() -> Int {
var minute: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.minute, from: self)
let dateComponent = calendar.dateComponents([.minute], from: self)
return dateComponent.minute!
}

func day() -> Int {
var day: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.day, from: self)
let dateComponent = calendar.dateComponents([.day], from: self)
return dateComponent.day!
}

func weekday() -> Int {
var weekday: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.weekday, from: self)
let dateComponent = calendar.dateComponents([.weekday], from: self)
return dateComponent.weekday!
}

func month() -> Int {
var month: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.month, from: self)
let dateComponent = calendar.dateComponents([.month], from: self)
return dateComponent.month!
}

func year() -> Int {
var year: Int {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components(.year, from: self)
let dateComponent = calendar.dateComponents([.year], from: self)
return dateComponent.year!
}

func numberOfDaysInMonth() -> Int {
var numberOfDaysInMonth: Int {
let calendar = Calendar.current
let days = (calendar as NSCalendar).range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: self)
return days.length
let days = calendar.range(of: .day, in: .month, for: self)!
return days.count
}

func dateByIgnoringTime() -> Date {
var dateByIgnoringTime: Date {
let calendar = Calendar.current
let dateComponent = (calendar as NSCalendar).components([.year, .month, .day ], from: self)
let dateComponent = calendar.dateComponents([.year, .month, .day], from: self)
return calendar.date(from: dateComponent)!
}

func monthNameFull() -> String {
var monthNameFull: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM YYYY"
return dateFormatter.string(from: self)
}

func isSunday() -> Bool
var isSunday: Bool
{
return (self.getWeekday() == 1)
return (self.getWeekday == 1)
}

func isMonday() -> Bool
var isMonday: Bool
{
return (self.getWeekday() == 2)
return (self.getWeekday == 2)
}

func isTuesday() -> Bool
var isTuesday: Bool
{
return (self.getWeekday() == 3)
return (self.getWeekday == 3)
}

func isWednesday() -> Bool
var isWednesday: Bool
{
return (self.getWeekday() == 4)
return (self.getWeekday == 4)
}

func isThursday() -> Bool
var isThursday: Bool
{
return (self.getWeekday() == 5)
return (self.getWeekday == 5)
}

func isFriday() -> Bool
var isFriday: Bool
{
return (self.getWeekday() == 6)
return (self.getWeekday == 6)
}

func isSaturday() -> Bool
var isSaturday: Bool
{
return (self.getWeekday() == 7)
return (self.getWeekday == 7)
}
func getWeekday() -> Int {

var getWeekday: Int {
let calendar = Calendar.current
return (calendar as NSCalendar).components( .weekday, from: self).weekday!
return calendar.dateComponents([.weekday], from: self).weekday!
}

func isToday() -> Bool {
var isToday: Bool {
return self.isDateSameDay(Date())
}

func isDateSameDay(_ date: Date) -> Bool {

return (self.day() == date.day()) && (self.month() == date.month() && (self.year() == date.year()))

return (self.day == date.day) && (self.month == date.month && (self.year == date.year))
}

static func ==(lhs: Date, rhs: Date) -> Bool {
Expand All @@ -202,4 +194,4 @@ extension Date {
static func >(lhs: Date, rhs: Date) -> Bool {
return rhs.compare(lhs) == ComparisonResult.orderedAscending
}
}
}