devinross / tapkulibrary

tap + haiku = tapku, a well crafted open source iOS framework
http://devinross.com/tapku/documentation
MIT License
3.9k stars 653 forks source link

Events are not getting displayed after reloaddata in Timelineview of Tapku Calendar #235

Open gvnsandeep opened 11 years ago

gvnsandeep commented 11 years ago

Hi Devinross,

I am using the Tapku Calendar Library Day View in order to display events. Events are getting displayed properly when the calendar loads for the first time. If I change the date lets say either to yesterday or tomorrow, the calendar is not displaying the events.

I have implemented the following events.

- (void)calendarDayTimelineView:(TKCalendarDayView *)calendarDay didMoveToDate:(NSDate *)date
 {
        //Here is my logic to pull the data from db server.
       //After this I am calling the method below.
       [self.dayView reloadData];
 }

- (NSArray *) calendarDayTimelineView:(TKCalendarDayView*)calendarDayTimeline eventsForDate:(NSDate *)eventDate{
self.myAppointments = nil;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Tasks" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.myAppointments = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]] == NSOrderedAscending) return @[];
if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24*60*60]] == NSOrderedDescending) return @[];

NSDateComponents *info = [[NSDate date] dateComponentsWithTimeZone:calendarDayTimeline.timeZone];
info.second = 0;
NSMutableArray *ret = [NSMutableArray array];

for(Tasks *apt in self.myAppointments){

    TKCalendarDayEventView *event = [calendarDayTimeline dequeueReusableEventView];
    if(event == nil) event = [TKCalendarDayEventView eventView];

    event.identifier = nil;
    event.titleLabel.text = apt.task_subject;

    if ( [allTrim(apt.location) length] != 0 )
    {
        event.locationLabel.text = apt.location;
    }

    NSDate *startDate = apt.task_start;
    NSDate *endDate = apt.task_end;

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:startDate];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];

    info.hour = hour;
    info.minute = minute;
    event.startDate = [NSDate dateWithDateComponents:info];

    components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:endDate];
    hour = [components hour];
    minute = [components minute];

    info.hour = hour;
    info.minute = minute;
    event.endDate = [NSDate dateWithDateComponents:info];

    [ret addObject:event];

}

 return ret;
   }

I have debugged the code, the data is getting assigned to the events, however I am not able to view any thing on the calendar.

Could you Pls have a look at my code and help in fixing up this issue.

Regards,

G.V.N.Sandeep

dmschlab commented 11 years ago

In calendarDayTimeLineView, remove these two lines:

if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24_60_60]] == NSOrderedAscending) return @[]; if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24_60_60]] == NSOrderedDescending) return @[];

They limit the day calendar.

gvnsandeep commented 11 years ago

Hi,

I have removed the two lines as said by you. But it is still of no use. I am not able to view any events on the calendar. However the data is getting assigned to the events, this I came to know while debugging the code.

Regards,

G.V.N.Sandeep

bigyelow commented 11 years ago

There has another way to solve this problem.

As you see, in the methods below:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"Day View", @"");

    self.data = @[
  @[@"Meeting with five random dudes", @"Five Guys", @960, @0, @1000, @30],
  @[@"Unlimited bread rolls got me sprung", @"Olive Garden", @7, @0, @12, @0],
  @[@"Appointment", @"Dennys", @15, @0, @18, @0],
  @[@"Hamburger Bliss", @"Wendys", @15, @0, @18, @0],
  @[@"Fishy Fishy Fishfelayyyyyyyy", @"McDonalds", @5, @30, @6, @0],
  @[@"Turkey Time...... oh wait", @"Chick-fela", @14, @0, @19, @0],
  @[@"Greet the king at the castle", @"Burger King", @19, @30, @100, @0]];  
}

self.data is loaded.

We can move the loading process to "viewWillAppear" methods to update data if you do some writing. Note that, there has no method to add an event to a specific date. But we can add an event like this: @[@"Fishy Fishy Fishfelayyyyyyyy", @"McDonalds", @48, @00, @50, @0]. @48 means the event at the day after tomorrow.

So, first you should calculate the interval between the specific date and self.dayview.date(today). Following codes may help:

- (NSArray *)getTimeFromNow:(NSDate *)startDate endDateTime:(NSDate *)endDate
{
  NSDate *todayDate = [NSDate date];
  NSTimeInterval startFromNowSeconds = [startDate timeIntervalSinceDate:todayDate];
  NSTimeInterval endFromNowSeconds = [endDate timeIntervalSinceDate:todayDate];

  NSNumber *startHour = [NSNumber numberWithInt:startFromNowSeconds / (60 * 60)];
  NSNumber *startMinute = [NSNumber numberWithInt:startFromNowSeconds / 60 - startHour.intValue * 60];

  NSNumber *endHour = [NSNumber numberWithInt:endFromNowSeconds / (60 * 60)];
  NSNumber *endMinute = [NSNumber numberWithInt:endFromNowSeconds / 60 - endHour.intValue * 60];

  return @[startHour, startMinute, endHour, endMinute];
}

Below should be commented: if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24_60_60]] == NSOrderedAscending) return @[]; if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24_60_60]] == NSOrderedDescending) return @[];

But pay attention to the timezone. I think it's not very easy to return a right time array calculated above.

More over, i think it is essential necessary to add the method that add event to a specific day in DayViewController.

Note: as i have do some coding, if your time is @900 hours from now, the event will not be showed in dayview... but @500 hours it works well.

What a terrible designs here! Waiting a more complete dayviewcontroller ...