Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 213 lines (157 sloc) 6.826 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
//
// MainViewController.m
// RPG Kit
//
// Created by Philip Regan on 4/14/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "GameSelectorViewController.h"

@interface MainViewController ()

// list of games driving the table view
@property (strong, nonatomic, readwrite) NSMutableArray *games;

@end

@implementation MainViewController

@synthesize managedObjectContext = _managedObjectContext;
@synthesize gameDataController = _gameDataController;
@synthesize tableView = _tableView;
@synthesize games = _games;

#pragma mark - Object Lifecycle Stack

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ( [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] ) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
    [self prepareViewController];
   
}

- (void)viewDidUnload
{
    [self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark NavigationController Stack

- (void)viewWillAppear:(BOOL)animated {
    // deselect the last row selected by the user so we don't get things confused
    [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:animated];
    [self prepareViewController];
}



- (void)viewDidDisappear:(BOOL)animated {
    
    // we want the back button to state the intent of the previous screen and not just
    // "Back". To do this, we have to create a new button before the next screen is
    // pushed into view
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Games"
                                                                             style:UIBarButtonItemStyleBordered
                                                                            target:nil
                                                                            action:nil];
    
    // the other way to do this is to change the title of this screen, which, while
    // shorter, requires the name be visibly changed every time the screen changes.
    // This at least lets the values get cached by the navigation controller as needed.
    
}

/*
private convenience method for managing the prep of the interface
*/

-(void)prepareViewController {
    self.games = [self.gameDataController retrieveGames];
    self.title = NSLocalizedString(@"OCAppName", @"");
    self.navigationController.delegate = self;
    [self.tableView reloadData];
}

#pragma mark - Table View Stack

/*
required by UITableViewDelegate and UITableViewDataSource
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.games count];
}

/*
required by UITableViewDelegate and UITableViewDataSource
*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"GameSelectorCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if ( !cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = [[self.games objectAtIndex:indexPath.row] valueForKey:@"titleStory"];
    //cell.detailTextLabel.text = [[self.games objectAtIndex:indexPath.row] valueForKey:@"introStory"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}

/*
required by UITableViewDelegate to capture clicks
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
    // get the universe for this row and set it to the game data controller
    self.gameDataController.currentUniverse = [self.games objectAtIndex:indexPath.row];
    
    // set up the Game Selector View
    GameSelectorViewController *gsvc = [[GameSelectorViewController alloc] initWithNibName:@"GameSelectorViewController" bundle:nil];
    gsvc.gameDataController = self.gameDataController;
    
    // push
    [self.navigationController pushViewController:gsvc animated:YES];
}

/*
required by UITableViewDelegate to move and delete rows, the latter being what
we want here.
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ( editingStyle == UITableViewCellEditingStyleDelete ) {
    
        // capture the universe to be deleted
        NSManagedObject *targetUniverse = [self.games objectAtIndex:indexPath.row];
        
        // delete the object
        if ( [self.gameDataController deleteUniverse:targetUniverse] ) {
            
            // update the data source for the table view
            [self.games removeObjectAtIndex:indexPath.row];
                        
            // update the rows
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            
        } else {
            
            // something bad happened
            NSLog(@"ERROR: MainViewController:newGame: NO");
        }
         
    }
    // update the table
    self.games = [self.gameDataController retrieveGames];
    [self.tableView setEditing:NO animated:YES];
}

#pragma mark - Action Stack

- (IBAction)newGame:(id)sender {
    // check to be sure the new universe was made
    if ( [self.gameDataController createNewUniverse] ) {
        // update the list
        self.games = [self.gameDataController retrieveGames];
        [self.tableView reloadData];
    } else {
        // something bad happened
        NSLog(@"ERROR: MainViewController:newGame: NO");
    }
}

- (IBAction)editGames:(id)sender {
    
    if ( self.tableView.editing ) {
        [self.tableView setEditing:NO animated:YES];
    } else {
        [self.tableView setEditing:YES animated:YES];
    }
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self prepareViewController];
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showInfo:(id)sender
{
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    controller.gameDataController = self.gameDataController;
    [self presentModalViewController:controller animated:YES];
}

@end
Something went wrong with that request. Please try again.