NSDateComponents sharedInstance

1. Make NSDateComponents in NSDate+FSExtension.h single instance, avoid
alloc every time using it. This makes performance better

2. Optimize other logic
This commit is contained in:
WenchaoIOS 2015-11-08 09:13:09 +08:00
parent 6ad2658ec7
commit 0446f9febf
4 changed files with 75 additions and 56 deletions

View File

@ -7,14 +7,12 @@
//
#import "AppDelegate.h"
#import "FScalendar.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}

View File

@ -130,7 +130,7 @@
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar
{
NSLog(@"did change to page %@",[calendar.currentPage fs_stringWithFormat:@"MMMM yyyy"]);
[calendar selectDate:calendar.currentPage];
// [calendar selectDate:calendar.currentPage];
}
- (void)calendarCurrentScopeWillChange:(FSCalendar *)calendar animated:(BOOL)animated

View File

@ -477,19 +477,22 @@
}
return NO;
}
if ([collectionView.indexPathsForSelectedItems containsObject:indexPath] || [_selectedDates containsObject:[self dateForIndexPath:indexPath]]) {
NSDate *targetDate = [self dateForIndexPath:indexPath];
if ([self isDateSelected:targetDate]) {
// if
if (self.allowsMultipleSelection) {
if ([self collectionView:collectionView shouldDeselectItemAtIndexPath:indexPath]) {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
[self collectionView:collectionView didDeselectItemAtIndexPath:indexPath];
}
} else {
//
[self didSelectDate:self.selectedDate];
}
return NO;
}
BOOL shouldSelect = YES;
if (shouldSelect && cell.date && [self isDateInRange:cell.date] && !_supressEvent) {
if (cell.date && [self isDateInRange:cell.date] && !_supressEvent) {
shouldSelect &= [self shouldSelectDate:cell.date];
}
if (shouldSelect) {
@ -516,6 +519,7 @@
}
NSDate *selectedDate = cell.date ?: [self dateForIndexPath:indexPath];
[_selectedDates removeObject:selectedDate];
[self deselectCounterpartDate:selectedDate];
[self didDeselectDate:selectedDate];
}
@ -1182,60 +1186,62 @@
if (![self isDateInRange:date]) {
[NSException raise:@"selectedDate out of range" format:@""];
}
NSDate *targetDate = [date fs_daysFrom:_minimumDate] < 0 ? _minimumDate.copy : date;
targetDate = [targetDate fs_daysFrom:_maximumDate] > 0 ? _maximumDate.copy : targetDate;
targetDate = targetDate.fs_dateByIgnoringTimeComponents;
NSDate *targetDate = date.fs_dateByIgnoringTimeComponents;
NSIndexPath *targetIndexPath = [self indexPathForDate:targetDate];
BOOL shouldSelect = !_supressEvent;
//
if (forPlaceholder) {
//
if (self.allowsMultipleSelection && [self isDateSelected:targetDate]) {
//
BOOL shouldDeselect = [self shouldDeselectDate:targetDate];
if (!shouldDeselect) {
if (self.allowsMultipleSelection) {
//
if ([self isDateSelected:targetDate]) {
BOOL shouldDeselect = [self shouldDeselectDate:targetDate];
if (!shouldDeselect) {
return;
}
} else {
shouldSelect &= [self shouldSelectDate:targetDate];
if (!shouldSelect) {
return;
}
}
} else {
//
shouldSelect &= [self shouldSelectDate:targetDate];
if (shouldSelect) {
if ([self isDateSelected:targetDate]) {
[self didSelectDate:targetDate];
} else {
NSDate *selectedDate = self.selectedDate;
if (selectedDate) {
[self deselectDate:selectedDate];
}
[_collectionView selectItemAtIndexPath:targetIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:_collectionView didSelectItemAtIndexPath:targetIndexPath];
}
} else {
return;
}
}
shouldSelect &= [self shouldSelectDate:targetDate];
if (shouldSelect && ![self isDateSelected:targetDate]) {
if (_collectionView.indexPathsForSelectedItems.count && self.selectedDate && !self.allowsMultipleSelection) {
NSIndexPath *currentIndexPath = [self indexPathForDate:self.selectedDate];
[_collectionView deselectItemAtIndexPath:currentIndexPath animated:YES];
[self deselectCounterpartDate:self.selectedDate];
[self collectionView:_collectionView didDeselectItemAtIndexPath:currentIndexPath];
}
if ([self collectionView:_collectionView shouldSelectItemAtIndexPath:targetIndexPath]) {
[_collectionView selectItemAtIndexPath:targetIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:_collectionView didSelectItemAtIndexPath:targetIndexPath];
}
} else {
[self didSelectDate:targetDate];
}
} else if (![_selectedDates containsObject:targetDate]){
//
} else if (![self isDateSelected:targetDate]){
//
if (self.selectedDate && !self.allowsMultipleSelection) {
NSDate *selectedDate = self.selectedDate;
NSIndexPath *currentIndexPath = [self indexPathForDate:selectedDate];
[_collectionView deselectItemAtIndexPath:currentIndexPath animated:NO];
FSCalendarCell *cell = (FSCalendarCell *)[_collectionView cellForItemAtIndexPath:currentIndexPath];
cell.dateIsSelected = NO;
[cell setNeedsLayout];
[_selectedDates removeObject:selectedDate];
[self deselectCounterpartDate:selectedDate];
[self deselectDate:self.selectedDate];
}
[_collectionView selectItemAtIndexPath:targetIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
FSCalendarCell *cell = (FSCalendarCell *)[_collectionView cellForItemAtIndexPath:targetIndexPath];
[cell performSelecting];
[self enqueueSelectedDate:targetDate];
[self selectCounterpartDate:targetDate];
} else if (![_collectionView.indexPathsForSelectedItems containsObject:targetIndexPath]) {
//
[_collectionView selectItemAtIndexPath:targetIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
if (scrollToDate) {
//
if (forPlaceholder && !shouldSelect) {
return;
}
@ -1275,7 +1281,7 @@
}
}
if (_pagingEnabled) {
if (!self.floatingMode) {
switch (_collectionViewLayout.scrollDirection) {
case UICollectionViewScrollDirectionVertical: {
@ -1292,16 +1298,18 @@
}
} else {
CGRect itemFrame = [_collectionViewLayout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:scrollOffset]].frame;
if (self.floatingMode && CGRectEqualToRect(itemFrame, CGRectZero)) {
//
CGRect headerFrame = [_collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:scrollOffset]].frame;
if (CGSizeEqualToSize(headerFrame.size, CGSizeZero)) {
// loadViewviewDidLoad, UICollectionViewsetContentOffset,layoutSubviews
_currentPage = targetDate;
_needsAdjustingMonthPosition = YES;
[self setNeedsLayout];
} else {
CGRect headerFrame = [_collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:scrollOffset]].frame;
CGPoint targetOffset = CGPointMake(0, MIN(headerFrame.origin.y,_collectionView.contentSize.height-_collectionView.fs_bottom));
[_collectionView setContentOffset:targetOffset animated:animated];
}
}
if (_header && !animated) {
@ -1335,7 +1343,7 @@
}
[self didChangeValueForKey:@"currentPage"];
[self scrollToDate:_currentPage animated:animated];
} else if (!_pagingEnabled) {
} else {
[self scrollToDate:date.fs_firstDayOfMonth animated:animated];
}
}

View File

@ -99,11 +99,12 @@
{
NSCalendar *calendar = [NSCalendar fs_sharedCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
NSDateComponents *componentsToSubtract = [NSDateComponents fs_sharedDateComponents];
componentsToSubtract.day = - (weekdayComponents.weekday - calendar.firstWeekday);
NSDate *beginningOfWeek = [calendar dateByAddingComponents:componentsToSubtract toDate:self options:0];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:beginningOfWeek];
beginningOfWeek = [calendar dateFromComponents:components];
componentsToSubtract.day = NSIntegerMax;
return beginningOfWeek;
}
@ -142,19 +143,25 @@
+ (instancetype)fs_dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day
{
NSCalendar *calendar = [NSCalendar fs_sharedCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSDateComponents *components = [NSDateComponents fs_sharedDateComponents];
components.year = year;
components.month = month;
components.day = day;
return [calendar dateFromComponents:components];
NSDate *date = [calendar dateFromComponents:components];
components.year = NSIntegerMax;
components.month = NSIntegerMax;
components.day = NSIntegerMax;
return date;
}
- (NSDate *)fs_dateByAddingYears:(NSInteger)years
{
NSCalendar *calendar = [NSCalendar fs_sharedCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSDateComponents *components = [NSDateComponents fs_sharedDateComponents];
components.year = years;
return [calendar dateByAddingComponents:components toDate:self options:0];
NSDate *date = [calendar dateByAddingComponents:components toDate:self options:0];
components.year = NSIntegerMax;
return date;
}
- (NSDate *)fs_dateBySubtractingYears:(NSInteger)years
@ -165,9 +172,11 @@
- (NSDate *)fs_dateByAddingMonths:(NSInteger)months
{
NSCalendar *calendar = [NSCalendar fs_sharedCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSDateComponents *components = [NSDateComponents fs_sharedDateComponents];
components.month = months;
return [calendar dateByAddingComponents:components toDate:self options:0];
NSDate *date = [calendar dateByAddingComponents:components toDate:self options:0];
components.month = NSIntegerMax;
return date;
}
- (NSDate *)fs_dateBySubtractingMonths:(NSInteger)months
@ -178,9 +187,11 @@
- (NSDate *)fs_dateByAddingWeeks:(NSInteger)weeks
{
NSCalendar *calendar = [NSCalendar fs_sharedCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSDateComponents *components = [NSDateComponents fs_sharedDateComponents];
components.weekOfYear = weeks;
return [calendar dateByAddingComponents:components toDate:self options:0];
NSDate *date = [calendar dateByAddingComponents:components toDate:self options:0];
components.weekOfYear = NSIntegerMax;
return date;
}
-(NSDate *)fs_dateBySubtractingWeeks:(NSInteger)weeks
@ -191,9 +202,11 @@
- (NSDate *)fs_dateByAddingDays:(NSInteger)days
{
NSCalendar *calendar = [NSCalendar fs_sharedCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
return [calendar dateByAddingComponents:components toDate:self options:0];
NSDateComponents *components = [NSDateComponents fs_sharedDateComponents];
components.day = days;
NSDate *date = [calendar dateByAddingComponents:components toDate:self options:0];
components.day = NSIntegerMax;
return date;
}
- (NSDate *)fs_dateBySubtractingDays:(NSInteger)days