From 3687471fafdd7ed316b193c3b5997c4101d47f97 Mon Sep 17 00:00:00 2001 From: Hirami Date: Wed, 11 Apr 2012 23:34:54 +0900 Subject: [PATCH] AlertView related refactoring. NewFolderAlert and EditCancelAlert --- iOS/Tombo/Tombo/EditCancelAlert.h | 7 +++- iOS/Tombo/Tombo/EditCancelAlert.m | 37 +++++++++++++++++- iOS/Tombo/Tombo/MasterViewController.m | 70 ++++++---------------------------- iOS/Tombo/Tombo/NewFolderAlert.h | 7 +++- iOS/Tombo/Tombo/NewFolderAlert.m | 46 +++++++++++++++++++++- 5 files changed, 104 insertions(+), 63 deletions(-) diff --git a/iOS/Tombo/Tombo/EditCancelAlert.h b/iOS/Tombo/Tombo/EditCancelAlert.h index c197799..8ac61f5 100644 --- a/iOS/Tombo/Tombo/EditCancelAlert.h +++ b/iOS/Tombo/Tombo/EditCancelAlert.h @@ -1,4 +1,9 @@ #import -@interface EditCancelAlert : UIAlertView +@interface EditCancelAlert : UIAlertView + +- (id)initWithDefault; + +- (BOOL)showAndWait; + @end diff --git a/iOS/Tombo/Tombo/EditCancelAlert.m b/iOS/Tombo/Tombo/EditCancelAlert.m index 8cb0468..1b94ac2 100644 --- a/iOS/Tombo/Tombo/EditCancelAlert.m +++ b/iOS/Tombo/Tombo/EditCancelAlert.m @@ -1,4 +1,39 @@ #import "EditCancelAlert.h" -@implementation EditCancelAlert +@implementation EditCancelAlert { + BOOL finished; + BOOL isOK; +} + +- (id)initWithDefault { + return [self initWithTitle:@"Confirm" + message:@"Note is modified. Are you sure to discard changes?" delegate:self + cancelButtonTitle:@"Cancel" + otherButtonTitles:@"OK", nil]; +} + +- (BOOL)showAndWait { + [self show]; + finished = NO; + + // Wait in runloop till a button is clicked. + while (!finished) { + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode + beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]]; + } + return isOK; +} + +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { + finished = YES; + + if (buttonIndex == 0) { + // Cancel + isOK = NO; + } else if (buttonIndex == 1) { + isOK = YES; + } +} + + @end diff --git a/iOS/Tombo/Tombo/MasterViewController.m b/iOS/Tombo/Tombo/MasterViewController.m index 21f278e..bc7fdf6 100644 --- a/iOS/Tombo/Tombo/MasterViewController.m +++ b/iOS/Tombo/Tombo/MasterViewController.m @@ -8,29 +8,7 @@ #import "FileItem.h" #import "PasswordManager.h" -@interface BackgroundView : UIView { -} -@end - -@implementation BackgroundView -- (id)initWithFrame:(CGRect)frame { - self = [super initWithFrame:frame]; - if (self) { - self.backgroundColor = [UIColor clearColor]; - } - return self; -} - -- (void)drawRect:(CGRect)rect { - UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:5.0]; - [path moveToPoint:CGPointMake(0, self.bounds.size.height/2.0)]; - [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2.0)]; - [[UIColor whiteColor] set]; - [path fill]; -} -@end - -@interface MasterViewController () { +@interface MasterViewController () { NSMutableArray *_objects; Storage *storage; PasswordManager *passwordManager; @@ -110,18 +88,12 @@ } - (void)createNewFolder:(id)sender { - NewFolderAlert *alert = [[NewFolderAlert alloc] initWithTitle:@"Folder name:" - message:@"\n" - delegate:self - cancelButtonTitle:@"Cancel" - otherButtonTitles:@"Done", nil]; - BackgroundView *back = [[BackgroundView alloc] initWithFrame:CGRectMake(20.0, 43.0, 245.0, 25.0)]; - [alert addSubview:back]; - UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; - [alert addSubview:textField]; - - [alert show]; -// [textField becomeFirstResponder]; + NewFolderAlert *alert = [[NewFolderAlert alloc] initWithDefault]; + NSString *folderName = [alert showAndWait]; + if (folderName) { + FileItem *item = [storage newFolder:folderName]; + [self insertItem:item]; + } } #pragma mark - Item operations @@ -361,11 +333,10 @@ - (void)editViewControllerDidCancel:(EditViewController *)controller { if (controller.isModify) { - UIAlertView *alert = [[EditCancelAlert alloc] initWithTitle:@"Confirm" - message:@"Note is modified. Are you sure to discard changes?" delegate:self - cancelButtonTitle:@"OK" - otherButtonTitles:@"Cancel", nil]; - [alert show]; + EditCancelAlert *cancel = [[EditCancelAlert alloc] initWithDefault]; + if ([cancel showAndWait]) { + [self dismissModalViewControllerAnimated:YES]; + } } else { [self dismissModalViewControllerAnimated:YES]; } @@ -378,23 +349,4 @@ [self insertItem:newItem]; } -#pragma mark - AlertViewDelegate - --(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { - if ([alertView isKindOfClass:[EditCancelAlert class]]) { - if (buttonIndex == 0) { - [self dismissModalViewControllerAnimated:YES]; - } - } else if ([alertView isKindOfClass:[NewFolderAlert class]]) { - if (buttonIndex == 1) { - // - UITextField *field = [alertView.subviews lastObject]; - NSString *folderName = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - if (folderName.length > 0) { - FileItem *item = [storage newFolder:folderName]; - [self insertItem:item]; - } - } - } -} @end diff --git a/iOS/Tombo/Tombo/NewFolderAlert.h b/iOS/Tombo/Tombo/NewFolderAlert.h index ff81409..668e6ab 100644 --- a/iOS/Tombo/Tombo/NewFolderAlert.h +++ b/iOS/Tombo/Tombo/NewFolderAlert.h @@ -1,5 +1,10 @@ #import -@interface NewFolderAlert : UIAlertView +@interface NewFolderAlert : UIAlertView +@property (strong,nonatomic) NSString *folderName; + +- (id)initWithDefault; + +- (NSString *)showAndWait; @end diff --git a/iOS/Tombo/Tombo/NewFolderAlert.m b/iOS/Tombo/Tombo/NewFolderAlert.m index 8994bdf..e6619b8 100644 --- a/iOS/Tombo/Tombo/NewFolderAlert.m +++ b/iOS/Tombo/Tombo/NewFolderAlert.m @@ -1,5 +1,49 @@ #import "NewFolderAlert.h" -@implementation NewFolderAlert +@implementation NewFolderAlert { + BOOL finished; +} + +@synthesize folderName=_folderName; + +- (id)initWithDefault { + return [self initWithTitle:@"New folder" + message:@"Input folder name" + delegate:self + cancelButtonTitle:@"Cancel" + otherButtonTitles:@"Done", nil]; +} + +- (NSString *)showAndWait { + self.alertViewStyle = UIAlertViewStylePlainTextInput; + [self show]; + finished = NO; + + // Wait in runloop till a button is clicked. + while (!finished) { + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode + beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]]; + } + return self.folderName; +} + +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { + finished = YES; + + UITextField *field = [self textFieldAtIndex:0]; + if (buttonIndex == 0) { + // Cancel + self.folderName = nil; + } else if (buttonIndex == 1) { + // OK + NSString *fname = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + if (fname.length == 0) { + self.folderName = nil; + } else { + self.folderName = fname; + } + } + [field resignFirstResponder]; +} @end -- 2.11.0