Files
old-festivalhelper/FestivalHelper/schedule/FESScheduleTableViewController.m
2014-09-07 07:18:07 +02:00

175 lines
5.6 KiB
Objective-C

//
// FESScheduleTableViewController.m
// FestivalHelper
//
// Created by Hamo Hapic on 02/09/14.
// Copyright (c) 2014 Senad Uka. All rights reserved.
//
#import "FESScheduleTableViewController.h"
#import "FESSCheduleEntry.h"
#import "FESScheduleEntryCell.h"
#import "FESScheduleFilmsViewController.h"
#import "FESDataProvider.h"
@interface FESScheduleTableViewController ()
@end
@implementation FESScheduleTableViewController
@synthesize scheduleArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[FESDataProvider getDataFromServerForUrl:SCHEDULE_URL andProcessThemWith:^(NSData *data) {
[self setupScheduleFromJSONArray:data];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(setupTitle) withObject:nil waitUntilDone:NO];
}];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.scheduleArray count];
}
-(void)setupScheduleFromJSONArray:(NSData*)dataFromServerArray{
NSError *error;
self.scheduleArray = [[NSMutableArray alloc] init];
NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromServerArray options:0 error:&error];
if(error){
NSLog(@"error parsing the json data from server with error description - %@", [error localizedDescription]);
}
else {
self.scheduleArray = [[NSMutableArray alloc] init];
for(NSDictionary *scheduleData in arrayFromServer)
{
FESSCheduleEntry *entry = [[FESSCheduleEntry alloc] initWithJSONData:scheduleData];
[self.scheduleArray addObject:entry];
}
NSLog(@"success!");
}
}
-(void)setupTitle {
if (scheduleArray != nil && scheduleArray.count > 0) {
FESSCheduleEntry *se = [scheduleArray objectAtIndex:0];
self.navigationItem.title = se.scheduleYear;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *retrievedCell = [tableView dequeueReusableCellWithIdentifier:@"scheduleEntryCell" forIndexPath:indexPath];
FESScheduleEntryCell *cell = (FESScheduleEntryCell *)retrievedCell;
if(cell)
{
//The beauty of this is that you have all your data in one object and grab WHATEVER you like
//This way in the future you can add another field without doing much.
FESSCheduleEntry *scheduleEntry = [scheduleArray objectAtIndex:indexPath.row];
cell.scheduleDate.text = [scheduleEntry scheduleDate];
cell.scheduleDayOfWeek.text = [scheduleEntry scheduleDayOfWeek];
}
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
FESScheduleFilmsViewController *destination = (FESScheduleFilmsViewController *) [segue destinationViewController];
FESSCheduleEntry *entry = (FESSCheduleEntry *)[self.scheduleArray objectAtIndex:self.tableView.indexPathForSelectedRow.row];
// Pass the selected object to the new view controller.
destination.scheduleFilmsArray = entry.scheduleFilms;
destination.navigationItem.title = [[entry.scheduleDayOfWeek stringByAppendingString:@", "] stringByAppendingString:entry.scheduleDate];
}
@end