#import "JSON.h"
#import "EventsSqliteDataSource.h"
#import "Events.h"
static BOOL IsDateBetweenInclusive(NSDate *date, NSDate *begin, NSDate *end)
{
return [date compare:begin] != NSOrderedAscending && [date compare:end] != NSOrderedDescending;
}
@interface EventsSqliteDataSource ()
- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate;
- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate;
@end
@implementation EventsSqliteDataSource
@synthesize eventPHP;
+ (EventsSqliteDataSource *)dataSource
{
return [[[[self class] alloc] init] autorelease];
}
- (id)init
{
if ((self = [super init])) {
items = [[NSMutableArray alloc] init];
eventPHP = [[NSMutableArray alloc] init];
buffer = [[NSMutableData alloc] init];
}
return self;
}
- (Events *)EventsAtIndexPath:(NSIndexPath *)indexPath
{
return [items objectAtIndex:indexPath.row];
}
#pragma mark UITableViewDataSource protocol conformance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
}
Events *event = [self EventsAtIndexPath:indexPath];
//cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"flags/%@.gif", holiday.country]];
//NSDictionary *dict = [eventPHP objectAtIndex: indexPath.row];
//cell.textLabel.text = [dict objectForKey:@"title_event"];
cell.textLabel.text = event.title;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
#pragma mark Fetch from the internet
- (void)fetchEvents
{
NSString *path = @"http://www.iksu-app.com/event.php";
NSLog(@"Fetching %@", path);
dataReady = NO;
[eventPHP removeAllObjects];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] delegate:self];
[conn start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[buffer setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[buffer appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *str = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
NSArray *array = [str JSONValue];
if (!array)
return;
NSLog(@"after this line crash");
NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
[fmt setDateFormat:@"yyyy-MM-dd"];
for (NSDictionary *dict in array) {
NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
NSLog(@"%@",d);
[eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"] description:[dict objectForKey:@"description"] date:d]];
}
dataReady = YES;
[callback loadedDataSource:self];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"EventsCalendarDataSource connection failure: %@", error);
}
#pragma mark KalDataSource protocol conformance
- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
if (dataReady) {
[callback loadedDataSource:self];
return;
}
callback = delegate;
[self fetchEvents];
}
- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
if (!dataReady)
return [NSArray array];
return [[self eventsFrom:fromDate to:toDate] valueForKeyPath:@"eve_date"];
}
- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
if (!dataReady)
return;
[items addObjectsFromArray:[self eventsFrom:fromDate to:toDate]];
}
- (void)removeAllItems
{
[items removeAllObjects];
}
#pragma mark -
- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
NSMutableArray *matches = [NSMutableArray array];
for (Events *event in eventPHP)
if (IsDateBetweenInclusive(event.date, fromDate, toDate))
[matches addObject:event];
return matches;
}
- (void)dealloc
{
[items release];
[eventPHP release];
[buffer release];
[super dealloc];
}
@end