OSDN Git Service

Update master view when notes is changed.
authorHirami <tomohisa.hirami@nifty.com>
Fri, 30 Mar 2012 11:31:03 +0000 (20:31 +0900)
committerHirami <tomohisa.hirami@nifty.com>
Fri, 30 Mar 2012 11:31:03 +0000 (20:31 +0900)
iOS/Tombo/Tombo/DetailViewController.m
iOS/Tombo/Tombo/MasterViewController.h
iOS/Tombo/Tombo/MasterViewController.m
iOS/Tombo/Tombo/Storage.h
iOS/Tombo/Tombo/Storage.m

index 6884fb4..374f9d2 100644 (file)
@@ -1,4 +1,5 @@
 #import "DetailViewController.h"
+#import "MasterViewController.h"
 
 @interface DetailViewController ()
 @property (strong, nonatomic) UIPopoverController *masterPopoverController;
                selector:@selector(keyboardDidHide:)
                    name:UIKeyboardDidHideNotification
                  object:nil];
-    
+
     [self configureView];
 }
 
 - (void)viewDidUnload
 {
     [self setDetailText:nil];
+    self.detailItem = nil;
+    self.storage = nil;
     [super viewDidUnload];
-    // Release any retained subviews of the main view.
 }
 
 - (void)viewWillDisappear:(BOOL)animated {
     // Leaving detail view
     NSString *note = self.detailText.text;
-    [storage save:note item: self.detailItem];
+    
+    FileItem *newPath = [storage save:note item: self.detailItem];
+    
+    // To notify master view, retract reference from navigation controller.
+    MasterViewController *master = [self.navigationController.viewControllers objectAtIndex:0];
+    
+    if (self.detailItem.name) {
+        // item exists
+        if (self.detailItem != newPath) {
+            [master itemChanged: self.detailItem to:newPath];
+        }
+    } else {
+        // new item
+        [master itemAdded: newPath];
+    }
     
     [super viewWillDisappear: animated];
 }
index b9a6a3c..72f22d9 100644 (file)
@@ -1,9 +1,13 @@
 #import <UIKit/UIKit.h>
+#import "FileItem.h"
 
 @class DetailViewController;
 
-@interface MasterViewController : UITableViewController <UINavigationControllerDelegate>
+@interface MasterViewController : UITableViewController
 
 @property (strong, nonatomic) DetailViewController *detailViewController;
 
+// Call back handler for detail view.
+- (void)itemChanged:(FileItem *)from to:(FileItem *)to;
+- (void)itemAdded: (FileItem *)item;
 @end
index 984dc88..e4a78e7 100644 (file)
@@ -39,9 +39,7 @@
                                                                                action:@selector(openNewNote:)];
     self.navigationItem.rightBarButtonItem = addButton;
     self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
-    
-    self.navigationController.delegate = self;
-    
+
     imgFolder = nil;
     imgDocument = nil;
     if (!storage) {
                           withRowAnimation:UITableViewRowAnimationAutomatic];
 }
 
+- (void) deleteItem:(FileItem *)item {
+    NSUInteger i = 0;
+    for (FileItem *f in _objects) {
+        if ([f.name isEqualToString: item.name]) {
+            [_objects removeObjectAtIndex:i];
+            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
+            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
+                                  withRowAnimation:UITableViewRowAnimationAutomatic];
+            break;
+        }
+        i++;
+    }
+}
 #pragma mark - Table View
 
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
             customSegue.isStop = YES;            
         }
         [self transitDetailView:indexPath controller:[segue destinationViewController]];
+    } else if ([[segue identifier] isEqualToString:@"newNote"]) {
+        // When open new document, detailItem is newItem.
+        DetailViewController *detail = [segue destinationViewController];        
+        detail.detailItem = [storage newItem];
+        detail.storage = storage;
     }
 }
 
-#pragma mark UINavigationControllerDelegate
+- (void)itemChanged:(FileItem *)from to:(FileItem *)to {
+    // Remove old item and insert new item
+    [self deleteItem: from];
+    [self insertItem: to];
+}
 
-/*
- * Called when view is changed
- */
-- (void)navigationController:(UINavigationController *)navigationController
-      willShowViewController:(UIViewController *)viewController 
-                    animated:(BOOL)animated {
-    
-    // I want to catch when returning from detail view to master view.
-    // At current time, this means switch to master view.
-    // But if there are another subviews, it should to be add another condition.
-    if (![viewController isKindOfClass:[MasterViewController class]]) return;
-    
+- (void)itemAdded:(FileItem *)item {
+    // Simply insert a item.
+    [self insertItem: item];
 }
 @end
index 8ac56dd..1a25abe 100644 (file)
  */
 -(BOOL)isTopDir;
 
--(void)save:(NSString *)note item:(FileItem *)item;
+
+-(FileItem*)newItem;
+
+/*
+ * Save note to file.
+ * 
+ * Returns new FileItem. This may be path/name is changed if note's title is changed.
+ * If path is not changed, returns item itself.
+ */
+-(FileItem *)save:(NSString *)note item:(FileItem *)item;
 @end
index 4879454..fa16918 100644 (file)
@@ -60,7 +60,9 @@
 }
 
 // save note
--(void)save:(NSString *)note item:(FileItem *)item {
+-(FileItem *)save:(NSString *)note item:(FileItem *)item {
+    FileItem *result = [FileItem alloc];
+    
     // Decide new title.
     NSRange r;
     r.location = 0;
     }
     
     // If title is changed, rename one.
-    NSString *path;
-    if (![title isEqualToString: item.name]) {
+    if (!item.name) {
+        // New item.
+        result.name = title;
+        result.path = [[item.path stringByDeletingLastPathComponent] stringByAppendingFormat:@"/%@.%@", title, [item.path pathExtension]];
+    } else if (![title isEqualToString: item.name]) {
         // Title is changed. Rename one.
         NSString *toPath = [[item.path stringByDeletingLastPathComponent] stringByAppendingFormat:@"/%@.%@", title, [item.path pathExtension]];
         NSError *error = nil;
-        path = toPath;
         [fileManager moveItemAtPath:item.path toPath:toPath error:&error];
+        result.name = title;
+        result.path = toPath;
     } else {
-        path = item.path;
+        result = item;
     }
     
     // Save note.
     NSError *error = nil;
-    [note writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];    
+    [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error];
+    
+    return result;
+}
+
+- (FileItem *)newItem {
+    FileItem *p = [FileItem allocWithName: nil];
+    p.path = [documentRoot stringByAppendingString:@"/_dummy.txt"];
+    return p;
 }
 
 @end