#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];
}
#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
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
*/
-(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
}
// 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