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

Fixed first day of week to be universal, added inversed display #15

Open
wants to merge 1 commit 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
Empty file modified MNCalendarView/MNCalendarHeaderView.h
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarHeaderView.m
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarView-Prefix.pch
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions MNCalendarView/MNCalendarView.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@property(nonatomic,strong) Class weekdayCellClass;
@property(nonatomic,strong) Class dayCellClass;

@property(nonatomic, assign) BOOL inversed;

- (void)reloadData;
- (void)registerUICollectionViewClasses;

Expand Down
66 changes: 44 additions & 22 deletions MNCalendarView/MNCalendarView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,19 @@ - (void)setSelectedDate:(NSDate *)selectedDate {
- (void)reloadData {
NSMutableArray *monthDates = @[].mutableCopy;
MNFastDateEnumeration *enumeration =
[[MNFastDateEnumeration alloc] initWithFromDate:[self.fromDate mn_firstDateOfMonth:self.calendar]
toDate:[self.toDate mn_firstDateOfMonth:self.calendar]
[[MNFastDateEnumeration alloc] initWithFromDate:[self.fromDate firstDateOfMonthWithCalendar:self.calendar]
toDate:[self.toDate lastDateOfMonthWithCalendar:self.calendar]
calendar:self.calendar
unit:NSMonthCalendarUnit];
for (NSDate *date in enumeration) {
[monthDates addObject:date];
}
self.monthDates = monthDates;

if (self.inversed) {
self.monthDates = [[monthDates reverseObjectEnumerator] allObjects];
} else {
self.monthDates = monthDates;
}

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = self.calendar;
Expand All @@ -138,26 +143,32 @@ - (void)registerUICollectionViewClasses {
}

- (NSDate *)firstVisibleDateOfMonth:(NSDate *)date {
date = [date mn_firstDateOfMonth:self.calendar];

NSDateComponents *components =
[self.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
fromDate:date];

return
[[date mn_dateWithDay:-((components.weekday - 1) % self.daysInWeek) calendar:self.calendar] dateByAddingTimeInterval:MN_DAY];

return [[date firstDateOfMonthWithCalendar:self.calendar] firstDateOfWeekWithCalendar:self.calendar];

// date = [date mn_firstDateOfMonth:self.calendar];
//
// NSDateComponents *components =
// [self.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
// fromDate:date];
//
// return
// [[date mn_dateWithDay:-((components.weekday - 1) % self.daysInWeek) calendar:self.calendar] dateByAddingTimeInterval:MN_DAY];
}

- (NSDate *)lastVisibleDateOfMonth:(NSDate *)date {
date = [date mn_lastDateOfMonth:self.calendar];

NSDateComponents *components =
[self.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
fromDate:date];

return
[date mn_dateWithDay:components.day + (self.daysInWeek - 1) - ((components.weekday - 1) % self.daysInWeek)
calendar:self.calendar];

return [[date lastDateOfMonthWithCalendar:self.calendar] lastDateOfWeekWithCalendar:self.calendar];

// date = [date mn_lastDateOfMonth:self.calendar];
//
// NSDateComponents *components =
// [self.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
// fromDate:date];
//
// return
// [date mn_dateWithDay:components.day + (self.daysInWeek - 1) - ((components.weekday - 1) % self.daysInWeek)
// calendar:self.calendar];
}

- (void)applyConstraints {
Expand Down Expand Up @@ -236,9 +247,11 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
MNCalendarViewWeekdayCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:MNCalendarViewWeekdayCellIdentifier
forIndexPath:indexPath];


NSInteger adjustedIndexOfDayInWeek = (indexPath.item + self.calendar.firstWeekday - 1) % self.daysInWeek;

cell.backgroundColor = self.collectionView.backgroundColor;
cell.titleLabel.text = self.weekdaySymbols[indexPath.item];
cell.titleLabel.text = self.weekdaySymbols[adjustedIndexOfDayInWeek];
cell.separatorColor = self.separatorColor;
return cell;
}
Expand All @@ -252,6 +265,15 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView

NSUInteger day = indexPath.item - self.daysInWeek;

if (self.inversed) {

NSInteger numberOfDaysInMonth = ([self.collectionView numberOfItemsInSection:indexPath.section] - self.daysInWeek) - 1;
NSInteger r = day / self.daysInWeek;
NSInteger c = day % self.daysInWeek;
day = numberOfDaysInMonth - (r * self.daysInWeek + (self.daysInWeek - c)) + 1;

}

NSDateComponents *components =
[self.calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
fromDate:firstDateInMonth];
Expand Down
Empty file modified MNCalendarView/MNCalendarViewCell.h
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarViewCell.m
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarViewDayCell.h
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions MNCalendarView/MNCalendarViewDayCell.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ - (void)setDate:(NSDate *)date
[self.calendar components:NSMonthCalendarUnit
fromDate:self.month];

self.weekday = components.weekday;
self.titleLabel.text = [NSString stringWithFormat:@"%d", components.day];
self.weekday = (components.weekday + self.calendar.firstWeekday - 1) % 7;
self.titleLabel.text = [NSString stringWithFormat:@"%ld", (long)components.day];
self.enabled = monthComponents.month == components.month;

[self setNeedsDisplay];
Expand Down
Empty file modified MNCalendarView/MNCalendarViewLayout.h
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarViewLayout.m
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarViewWeekdayCell.h
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNCalendarViewWeekdayCell.m
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNFastDateEnumeration.h
100644 → 100755
Empty file.
Empty file modified MNCalendarView/MNFastDateEnumeration.m
100644 → 100755
Empty file.
12 changes: 10 additions & 2 deletions MNCalendarView/NSDate+MNAdditions.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@

@interface NSDate (MNAdditions)

- (instancetype)mn_firstDateOfMonth:(NSCalendar *)calendar;
- (NSDate *)firstDateOfWeekWithCalendar:(NSCalendar *)calendar;

- (instancetype)mn_lastDateOfMonth:(NSCalendar *)calendar;
- (NSDate *)lastDateOfWeekWithCalendar:(NSCalendar *)calendar;

- (NSDate *)firstDateOfMonthWithCalendar:(NSCalendar *)calendar;

- (NSDate *)lastDateOfMonthWithCalendar:(NSCalendar *)calendar;

//- (instancetype)mn_firstDateOfMonth:(NSCalendar *)calendar;
//
//- (instancetype)mn_lastDateOfMonth:(NSCalendar *)calendar;
//
- (instancetype)mn_beginningOfDay:(NSCalendar *)calendar;

- (instancetype)mn_dateWithDay:(NSUInteger)day calendar:(NSCalendar *)calendar;
Expand Down
31 changes: 31 additions & 0 deletions MNCalendarView/NSDate+MNAdditions.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,35 @@ - (instancetype)mn_dateWithDay:(NSUInteger)day calendar:(NSCalendar *)calendar {
return [calendar dateFromComponents:components];
}

- (NSDate *)firstDateOfWeekWithCalendar:(NSCalendar *)calendar; {

NSDate *startOfWeek;
[calendar rangeOfUnit:NSWeekOfYearCalendarUnit startDate:&startOfWeek interval:NULL forDate:self];
return startOfWeek;

}

- (NSDate *)firstDateOfMonthWithCalendar:(NSCalendar *)calendar; {

NSDate *startOfMonth;
[calendar rangeOfUnit:NSMonthCalendarUnit startDate:&startOfMonth interval:NULL forDate:self];
return startOfMonth;

}

- (NSDate *)lastDateOfWeekWithCalendar:(NSCalendar *)calendar; {

NSDateComponents * oneWeek = [[NSDateComponents alloc] init];
[oneWeek setWeek:1];
return [[calendar dateByAddingComponents:oneWeek toDate:[self firstDateOfWeekWithCalendar:calendar] options:0] dateByAddingTimeInterval:-1];

}

- (NSDate *)lastDateOfMonthWithCalendar:(NSCalendar *)calendar; {

NSRange days = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self];
return [[self firstDateOfMonthWithCalendar:calendar] dateByAddingTimeInterval:(days.length * 24 * 60 * 60) - 1];

}

@end