k06a / ABCalendarPicker

Fully configurable iOS calendar UI component with multiple layouts and smooth animations.
MIT License
710 stars 119 forks source link

re-calculate today day #23

Closed Piero87 closed 10 years ago

Piero87 commented 10 years ago

i have see an issue, if the app remain in the background, and is not terminated, when open it doesn't calculate the today day (if the day is changed) because it's not called the viewDidLoad method, there is a way to refresh the today day? maybe in the viewwillappear?

EDIT: i have found this:

[self.calendarPicker setDate:[NSDate date] andState:ABCalendarPickerStateDays animated:YES];

but if i insert it in the viewwillappear every time the view is open, put the today date, and if the user have visited a particular date, when he return on that view he doens't see it anymore but see the today day, there is another solution?

k06a commented 10 years ago

You can use notification UIApplicationSignificantTimeChangeNotification to track day changes...

k06a commented 10 years ago

This is my method on this notification:

- (void)significantTimeChanged:(id)sender
{
    NSDate * todayDate = [NSDate date];
    NSDateComponents * todayComponents = [self.calendarPicker.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:todayDate];
    NSDateComponents * selectedComponents = [self.calendarPicker.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self.calendarPicker.selectedDate];
    if (todayComponents.day == selectedComponents.day
        && todayComponents.month == selectedComponents.month
        && todayComponents.year == selectedComponents.year)
        return;

    NSDateComponents * highlightedComponents = [self.calendarPicker.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self.calendarPicker.highlightedDate];
    if (highlightedComponents.month == selectedComponents.month
        && highlightedComponents.day == selectedComponents.day)
        [self.calendarPicker setHighlightedAndSectedDate:[NSDate date] animated:YES];
    else
        [self.calendarPicker setSelectedDate:self.calendarPicker.selectedDate animated:YES];
}
Piero87 commented 10 years ago

thank you very very very much, for someone that have this problem i have also add this:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(significantTimeChanged:)
                                             name:UIApplicationSignificantTimeChangeNotification
                                           object:nil];

in view did load, as @k06a suggest me!

Piero87 commented 10 years ago

I have tried to open the app on 14 August at 23:50 CET (my timezone), then i leave the app in background, and i reopen it now at 15 August at 00:02 CET, the midnight is passed but the calendar have the today day 14 August, i think it's not worked...

k06a commented 10 years ago

I tested this several times. This should work...