Skip to content

Instantly share code, notes, and snippets.

@anthonycvella
Created June 3, 2012 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonycvella/2865143 to your computer and use it in GitHub Desktop.
Save anthonycvella/2865143 to your computer and use it in GitHub Desktop.
//
// DetailsViewController.h
// HungerCraft
//
// Created by Anthony Vella on 5/31/12.
// Copyright (c) 2012 Aurora High School. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DetailsViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *_dataJSON;
@property (nonatomic, strong) IBOutlet UILabel *ipLabel;
@property (nonatomic, strong) IBOutlet UILabel *mapLabel;
@property (nonatomic, strong) IBOutlet UILabel *statusLabel;
@property (nonatomic, strong) IBOutlet UILabel *aliveLabel;
@end
//
// DetailsViewController.m
// HungerCraft
//
// Created by Anthony Vella on 5/31/12.
// Copyright (c) 2012 Aurora High School. All rights reserved.
//
#import "DetailsViewController.h"
@interface DetailsViewController ()
@end
@implementation DetailsViewController
@synthesize _dataJSON = _dataJSON;
@synthesize ipLabel = _ipLabel;
@synthesize mapLabel = _mapLabel;
@synthesize statusLabel = _statusLabel;
@synthesize aliveLabel = _aliveLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *ip = [_dataJSON objectAtIndex:0];
NSDictionary *details = [ip objectForKey:@"details"];
_ipLabel.text = [ip description];
_mapLabel.text = [details objectForKey:@"map"];
_statusLabel.text = [details objectForKey:@"gameState"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
//
// ServerViewController.h
// HungerCraft
//
// Created by Anthony Vella on 5/31/12.
// Copyright (c) 2012 Aurora High School. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ServerViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *_dataJSON;
- (IBAction)JSONLoader:(id)sender;
@end
//
// ServerViewController.m
// HungerCraft
//
// Created by Anthony Vella on 5/31/12.
// Copyright (c) 2012 Aurora High School. All rights reserved.
//
#import "ServerViewController.h"
#import "DetailsViewController.h"
@interface ServerViewController () <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *_tableView;
NSMutableArray *_dataJSON;
NSString *_serverIP;
NSString *_gameState;
NSString *_map;
UIImage *statImage;
}
- (void)refreshServers;
@end
@implementation ServerViewController
@synthesize _dataJSON;
- (void)viewDidLoad
{
[super viewDidLoad];
[self refreshServers];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)JSONLoader:(id)sender
{
[self refreshServers];
}
- (void)refreshServers
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://old.hungercraft.net/serverlist.php"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
_dataJSON = [json copy];
[_tableView reloadData];
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataJSON count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServerCell"];
if (cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ServerCell"];
}
NSDictionary *ip = [_dataJSON objectAtIndex:indexPath.row];
NSDictionary *details = [ip objectForKey:@"details"];
_serverIP = [ip objectForKey:@"ip"];
_gameState = [details objectForKey:@"gamestate"];
_map = [details objectForKey:@"map"];
NSString *status = [details objectForKey:@"stat"];
//NSLog(@"%@", [ip allKeys]);
if([status isEqualToString:@"Online"])
{
statImage = [UIImage imageNamed:@"status_on.png"];
} else {
statImage = [UIImage imageNamed:@"status_off.png"];
}
cell.textLabel.text = _serverIP;
cell.imageView.image = statImage;
//NSLog(@"State: %@", _gameState);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"ShowSelectedServers" sender:[_dataJSON objectAtIndex:indexPath.row]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ShowSelectedServers"])
{
DetailsViewController *detailViewController = [segue destinationViewController];
detailViewController._dataJSON = sender;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment