/***
 * Excerpted from "iOS SDK Development",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/adios for more book information.
***/
//
//  PRPAppDelegate.m
//  DocumentRecipes
//
//  Created by Bill Dudney on 9/18/12.
//  Copyright (c) 2012 Pragmatic Programmers LLC. All rights reserved.
//

#import "PRPAppDelegate.h"
#import "PRPRecipesListViewController.h"
#import "PRPRecipesDocument.h"

@implementation PRPAppDelegate

//
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  UINavigationController *navigationController =
  (UINavigationController *)self.window.rootViewController;
  PRPRecipesListViewController *controller =
  (PRPRecipesListViewController *)navigationController.topViewController;
  NSURL *docDir = [[[NSFileManager defaultManager]
                    URLsForDirectory:NSDocumentDirectory
                    inDomains:NSUserDomainMask] lastObject];
  NSURL *docURL = [docDir URLByAppendingPathComponent:@"Recipes.recipes"];
  PRPRecipesDocument *doc = [[PRPRecipesDocument alloc] initWithFileURL:docURL];
  controller.dataSource = doc; 
  [doc openWithCompletionHandler:^(BOOL success) {
    if(success) {
      [controller.tableView reloadData];
    } else {
      NSLog(@"Failed to open document");
    }
  }];
  return YES;
}
//

@end
