OSDN Git Service

Implement save notes.
authorHirami <tomohisa.hirami@nifty.com>
Fri, 30 Mar 2012 08:25:22 +0000 (17:25 +0900)
committerHirami <tomohisa.hirami@nifty.com>
Fri, 30 Mar 2012 08:25:22 +0000 (17:25 +0900)
When renaming title, file name is updated.
But update view is yet.

iOS/Tombo/Tombo/DetailViewController.h
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 7f3c93b..b7618fc 100644 (file)
@@ -1,9 +1,11 @@
 #import <UIKit/UIKit.h>
 #import "FileItem.h"
+#import "Storage.h"
 
 @interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
 
 @property (strong, nonatomic) FileItem *detailItem;
+@property (strong, nonatomic) Storage *storage;
 
 @property (weak, nonatomic) IBOutlet UITextView *detailText;
 
index c889fbf..6884fb4 100644 (file)
@@ -8,6 +8,7 @@
 @implementation DetailViewController
 
 @synthesize detailItem = _detailItem;
+@synthesize storage;
 @synthesize detailText = _detailText;
 @synthesize masterPopoverController = _masterPopoverController;
 
     // 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];
+    
+    [super viewWillDisappear: animated];
+}
+
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
index 70db8bc..b9a6a3c 100644 (file)
@@ -2,7 +2,7 @@
 
 @class DetailViewController;
 
-@interface MasterViewController : UITableViewController
+@interface MasterViewController : UITableViewController <UINavigationControllerDelegate>
 
 @property (strong, nonatomic) DetailViewController *detailViewController;
 
index bae688c..984dc88 100644 (file)
@@ -40,6 +40,8 @@
     self.navigationItem.rightBarButtonItem = addButton;
     self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
     
+    self.navigationController.delegate = self;
+    
     imgFolder = nil;
     imgDocument = nil;
     if (!storage) {
         
     } else {
         [controller setDetailItem:item];
+        [controller setStorage: storage];
     }
     
 }
     }
 }
 
+#pragma mark UINavigationControllerDelegate
+
+/*
+ * 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;
+    
+}
 @end
index cb44521..8ac56dd 100644 (file)
@@ -3,6 +3,7 @@
  Abstraction of file and directory.
  */
 #import <Foundation/Foundation.h>
+#import "FileItem.h"
 
 @interface Storage : NSObject
 
@@ -28,4 +29,6 @@
  * Is current directory Top?
  */
 -(BOOL)isTopDir;
+
+-(void)save:(NSString *)note item:(FileItem *)item;
 @end
index a235325..4879454 100644 (file)
 -(BOOL)isTopDir {
     return [currentDirectory isEqualToString:@"/"];
 }
+
+// save note
+-(void)save:(NSString *)note item:(FileItem *)item {
+    // Decide new title.
+    NSRange r;
+    r.location = 0;
+    r.length = 0;
+    NSRange titleRange = [note lineRangeForRange:r];
+    NSString *title = [note substringWithRange:titleRange];
+    if ([title characterAtIndex:(title.length - 1)] == '\n') {
+        title = [title substringToIndex:(title.length - 1)];
+    }
+    if (title.length == 0) {
+        title = @"New document";
+    }
+    
+    // If title is changed, rename one.
+    NSString *path;
+    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];
+    } else {
+        path = item.path;
+    }
+    
+    // Save note.
+    NSError *error = nil;
+    [note writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];    
+}
+
 @end