Skip to content

Instantly share code, notes, and snippets.

@TyrfingMjolnir
Created April 6, 2014 04:10
Show Gist options
  • Save TyrfingMjolnir/10001439 to your computer and use it in GitHub Desktop.
Save TyrfingMjolnir/10001439 to your computer and use it in GitHub Desktop.
//
// FixturesController.m
// Liga Zon Sagres Companion
//
// Created by Berto Bettencourt on 2/10/2014.
// Copyright (c) 2014 Orrian. All rights reserved.
//
#import "FixturesController.h"
#import "GADBannerView.h"
#import "GADRequest.h"
#import "appID.h"
@interface FixturesController ()
@end
@implementation FixturesController
@synthesize tableDataFixtures;
@synthesize bannerView = bannerView_;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)checkForWIFIConnection {
Reachability* wifiReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
if (netStatus == NotReachable)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Sem conexão à internet!", @"AlertView")
message:NSLocalizedString(@"Não está conectado à internet. Tente novamente após se connectar.", @"AlertView")
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK", @"AlertView")
otherButtonTitles: nil];
[alertView show];
return NO;
}
else {
return YES;
}
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if ([self checkForWIFIConnection]) {
[self parseXMLFixtures];
}
}
-(GADRequest *) createRequest
{
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
return request;
}
-(void) adViewDidReceiveAd:(GADBannerView *)adView
{
NSLog(@"Ad received");
[UIView animateWithDuration:1.0 animations:^{ adView.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height - adView.frame.size.height, adView.frame.size.width, adView.frame.size.height);
}];
CGRect screenRect = [[UIScreen mainScreen] bounds];
//CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if(screenHeight > 480)
{
[UIView animateWithDuration:1.0 animations:^{ adView.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height - adView.frame.size.height - 42, adView.frame.size.width, adView.frame.size.height);
}];
} else
{
[UIView animateWithDuration:1.0 animations:^{ adView.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height - adView.frame.size.height + 46, adView.frame.size.width, adView.frame.size.height);
}];
}
}
-(void) adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(@"Failed to received ad %@", [error localizedFailureReason]);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.bannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(50.0, 0.0, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
self.bannerView.adUnitID = MyAdUnitID;
self.bannerView.delegate = self;
[self.bannerView setRootViewController:self];
[self.view addSubview:self.bannerView];
[self.bannerView loadRequest:[self createRequest]];
self.edgesForExtendedLayout = UIRectEdgeAll;
self.tableView.contentInset = UIEdgeInsetsMake(0., 0., CGRectGetHeight(self.tabBarController.tabBar.frame), 0);
goUp = 1;
seasonRoundNumber = [NSNumber numberWithInt:goUp];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)goUpByOne:(UIButton *)sender {
if (goUp == 30){
}
else{
goUp = goUp + 1;
}
seasonRoundNumber = [NSNumber numberWithInt:goUp];
[self parseXMLFixtures];
[self.tableView reloadData];
}
- (IBAction)goDownByFive:(UIButton *)sender {
if (goUp - 3 >= 1){
goUp = goUp - 3;
}
else{
}
seasonRoundNumber = [NSNumber numberWithInt:goUp];
[self parseXMLFixtures];
[self.tableView reloadData];
}
- (IBAction)goUpByFive:(UIButton *)sender {
if (goUp + 3 <= 30){
goUp = goUp + 3;
}
else{
}
seasonRoundNumber = [NSNumber numberWithInt:goUp];
[self parseXMLFixtures];
[self.tableView reloadData];
}
- (IBAction)goDownByOne:(UIButton *)sender {
if (goUp == 1){
}
else{
goUp = goUp - 1;
}
seasonRoundNumber = [NSNumber numberWithInt:goUp];
[self parseXMLFixtures];
[self.tableView reloadData];
}
-(void) parseXMLFixtures
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"hurrayyyy"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
NSMutableArray *items = [xml objectForKey:@"Match"];
NSMutableArray *newFixtureObjectArray = [[NSMutableArray alloc] init];
NSNull *nullValue = [NSNull null];
[newFixtureObjectArray insertObject:nullValue atIndex:0];
[newFixtureObjectArray insertObject:nullValue atIndex:1];
for (NSDictionary *dict in items) {
FixturesObject *myFixtures = [FixturesObject fixtureFromXMLDictionary:dict];
[newFixtureObjectArray addObject:myFixtures];
}
_roundParser = [[NSMutableArray alloc]init];
// searches the array for objects with the same round value
_seasonRoundString = [NSString stringWithFormat:@"%d", [seasonRoundNumber intValue]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"round == %@", _seasonRoundString];
NSArray *filteredArray = [newFixtureObjectArray filteredArrayUsingPredicate:predicate];
_roundParser = [NSMutableArray arrayWithArray:filteredArray];
[_roundParser insertObject:nullValue atIndex:0];
[self setTableDataFixtures:_roundParser];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// Response has been received, initialize the instance variable to append data, re-initialize to clear
_responseDataFixtures = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//Append the new data to the instance variable you declared
[_responseDataFixtures appendData:data];
}
-(NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
// The request is complete and data has been receveid. Parse the data in your instance bariable now
NSString *responseString = [[NSString alloc] initWithData:_responseDataFixtures encoding:NSUTF8StringEncoding];
NSLog(@"The content of the response is %@", responseString);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return indexPath.row + 20 - indexPath.row;}
if (indexPath.row == 1) {
return indexPath.row + 40 - indexPath.row;}
else {
return indexPath.row + 26 - indexPath.row;}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"fixturesCellID";
FixturesCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
FixturesObject *item = [tableDataFixtures objectAtIndex:(int)([indexPath row]/2)];
// NSString *round = [NSString stringWithFormat:@"%@", seasonRoundNumber];
// cell.round.text = round;
if ([item isKindOfClass:[FixturesObject class]]) {
NSDate * dateStringCurrent = [NSDate date];
NSDateFormatter* dateFormatterCurrent = [[NSDateFormatter alloc] init];
[dateFormatterCurrent setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *dateInEpochCurrent = [NSString stringWithFormat:@"%.0f", [dateStringCurrent timeIntervalSince1970]];
int x = 0;
int bestDifference = 0;
int initDifference = 999999999;
for (; x < tableDataFixtures.count; x++) {
bestDifference = (int)dateInEpochCurrent - (int)item.epoch;
if (initDifference < bestDifference) {
bestDifference = initDifference;
}
}
NSLog(@"show me the best difference %i", bestDifference);
NSString * dateString = [NSString stringWithFormat: @"%@",item.date];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd.MM.Y 'ás' HH:mm"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
cell.homeName.text = item.homeName;
cell.awayName.text = item.awayName;
cell.homeLogo.image = item.homeLogo;
cell.awayLogo.image = item.awayLogo;
cell.homeScore.text = item.homeGoals;
cell.awayScore.text = item.awayGoals;
cell.matchDate.text = stringFromDate;
if ([item.homeName isEqual: @"Benfica"]) {
cell.homeName.text = @"SL Benfica";
}
if ([item.homeName isEqual: @"Braga"]) {
cell.homeName.text = @"SC Braga";
}
if ([item.homeName isEqual: @"Guimaraes"]) {
cell.homeName.text = @"V. Guimarães";
}
if ([item.homeName isEqual: @"Vitoria de Guimaraes"]) {
cell.homeName.text = @"V. Guimarães";
}
if ([item.homeName isEqual: @"CD Nacional de Madeira"]) {
cell.homeName.text = @"Nacional";
}
if ([item.homeName isEqual: @"Vitoria de Setubal"]) {
cell.homeName.text = @"V. Setúbal";
}
if ([item.homeName isEqual: @"Vitoria Setubal"]) {
cell.homeName.text = @"V. Setúbal";
}
if ([item.homeName isEqual: @"Associação Académica de Coimbra"]) {
cell.homeName.text = @"Académica";
}
if ([item.homeName isEqual: @"Maritimo"]) {
cell.homeName.text = @"Marítimo";
}
if ([item.homeName isEqual: @"F.C. Paços de Ferreira"]) {
cell.homeName.text = @"P. Ferreira";
}
if ([item.homeName isEqual: @"P.Ferreira"]) {
cell.homeName.text = @"P. Ferreira";
}
if ([item.homeName isEqual: @"Estoril-Praia"]) {
cell.homeName.text = @"Estoril";
}
if ([item.homeName isEqual: @"Sporting Lisbon"]) {
cell.homeName.text = @"Sporting CP";
}
if ([item.awayName isEqual: @"Benfica"]) {
cell.awayName.text = @"SL Benfica";
}
if ([item.awayName isEqual: @"Braga"]) {
cell.awayName.text = @"SC Braga";
}
if ([item.awayName isEqual: @"Guimaraes"]) {
cell.awayName.text = @"V. Guimarães";
}
if ([item.awayName isEqual: @"CD Nacional de Madeira"]) {
cell.awayName.text = @"Nacional";
}
if ([item.awayName isEqual: @"Vitoria Setubal"]) {
cell.awayName.text = @"V. Setúbal";
}
if ([item.awayName isEqual: @"Academica"]) {
cell.awayName.text = @"Académica";
}
if ([item.awayName isEqual: @"Associação Académica de Coimbra"]) {
cell.awayName.text = @"Académica";
}
if ([item.awayName isEqual: @"Maritimo"]) {
cell.awayName.text = @"Marítimo";
}
if ([item.awayName isEqual: @"F.C. Paços de Ferreira"]) {
cell.awayName.text = @"P. Ferreira";
}
if ([item.awayName isEqual: @"Estoril-Praia"]) {
cell.awayName.text = @"Estoril";
}
if ([item.awayName isEqual: @"Sporting Lisbon"]) {
cell.awayName.text = @"Sporting CP";
}
}
else {
}
if (indexPath.row % 2 ) {
cell.backgroundColor = [[UIColor alloc]initWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1]; // white labels
cell.matchDate.hidden = YES;
cell.round.hidden = YES;
cell.fastForward.hidden = YES;
cell.fastRewind.hidden = YES;
cell.slowForward.hidden = YES;
cell.slowRewind.hidden = YES;
}
else {
cell.backgroundColor = [[UIColor alloc]initWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1]; // grey labels
cell.homeName.hidden = YES;
cell.awayName.hidden = YES;
cell.homeLogo.hidden = YES;
cell.awayLogo.hidden = YES;
cell.homeScore.hidden = YES;
cell.awayScore.hidden = YES;
cell.scoreSpace.hidden = YES;
cell.round.hidden = YES;
cell.matchDate.hidden = NO;
cell.userInteractionEnabled = NO;
cell.fastForward.hidden = YES;
cell.fastRewind.hidden = YES;
cell.slowForward.hidden = YES;
cell.slowRewind.hidden = YES;
}
if (indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryNone; // Removes Accessory Disclosure
cell.backgroundColor = [[UIColor alloc]initWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1];
cell.homeName.hidden = YES;
cell.awayName.hidden = YES;
cell.homeLogo.hidden = YES;
cell.awayLogo.hidden = YES;
cell.homeScore.hidden = YES;
cell.awayScore.hidden = YES;
cell.scoreSpace.hidden = YES;
cell.round.hidden = YES;
cell.matchDate.hidden = YES;
cell.userInteractionEnabled = NO;
}
if (indexPath.row == 1){
cell.round.hidden = NO;
cell.round.textColor = [UIColor darkGrayColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.round.font = [UIFont fontWithName:@"Helvetica" size:20];
cell.backgroundColor = [[UIColor alloc]initWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1];
cell.homeName.hidden = YES;
cell.awayName.hidden = YES;
cell.homeLogo.hidden = YES;
cell.awayLogo.hidden = YES;
cell.homeScore.hidden = YES;
cell.awayScore.hidden = YES;
cell.scoreSpace.hidden = YES;
cell.userInteractionEnabled = YES;
cell.fastForward.hidden = NO;
cell.fastRewind.hidden = NO;
cell.slowForward.hidden = NO;
cell.slowRewind.hidden = NO;
}
return cell;
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// The request has failed. Check the error variable
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 18;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment