From e4638495c22841f174be163eedc8a9c415ef8bc4 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 3 May 2015 19:56:41 -0700 Subject: [PATCH] wire up the blog controller properly after restoring state --- Blog/AppDelegate.m | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/Blog/AppDelegate.m b/Blog/AppDelegate.m index 20edaa6..7136cb9 100644 --- a/Blog/AppDelegate.m +++ b/Blog/AppDelegate.m @@ -19,21 +19,28 @@ @interface AppDelegate () +@property (nonatomic, readonly, strong) BlogController *blogController; +@property (nonatomic, readonly, strong) PostsViewController *postsViewController; +@property (nonatomic, readonly, strong) EditorViewController *editorViewController; + @end @implementation AppDelegate +@synthesize blogController = _blogController; + - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - [self configureCodeInjection]; + [self setupCodeInjection]; UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; UINavigationController *navigationController = splitViewController.viewControllers.lastObject; navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem; splitViewController.delegate = self; - [self setupBlogController]; + self.postsViewController.blogController = self.blogController; + self.editorViewController.blogController = self.blogController; return YES; } -- (void)configureCodeInjection { +- (void)setupCodeInjection { __block BOOL codeInjectionEnabled = NO; [[[NSProcessInfo processInfo] arguments] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) { if ([obj isEqual:@"EnableCodeInjection"]) { @@ -45,6 +52,16 @@ } } +- (BlogController *)blogController { + if (!_blogController) { + NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject; + NSString *dbPath = [cachesPath stringByAppendingPathComponent:@"blog.sqlite"]; + ModelStore *store = [self newModelStoreWithPath:dbPath]; + _blogController = [self newBlogControllerWithModelStore:store rootURL:@"http://ocean.samhuri.net:6706/"]; + } + return _blogController; +} + - (PostsViewController *)postsViewController { UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; UINavigationController *navigationController = splitViewController.viewControllers.firstObject; @@ -52,13 +69,12 @@ return postsViewController; } -- (void)setupBlogController { - NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject; - NSString *dbPath = [cachesPath stringByAppendingPathComponent:@"blog.sqlite"]; - ModelStore *store = [self newModelStoreWithPath:dbPath]; - BlogController *blogController = [self newBlogControllerWithModelStore:store rootURL:@"http://ocean.samhuri.net:6706/"]; - - [self postsViewController].blogController = blogController; +- (EditorViewController *)editorViewController { + UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; + UINavigationController *navigationController = splitViewController.viewControllers.firstObject; + navigationController = navigationController.viewControllers.lastObject; + EditorViewController *editorViewController = (EditorViewController *)navigationController.viewControllers.firstObject; + return editorViewController; } - (ModelStore *)newModelStoreWithPath:(NSString *)dbPath {