• R/O
  • HTTP
  • SSH
  • HTTPS

Tombo: Commit

TOMBO source code


Commit MetaInfo

Revision3687471fafdd7ed316b193c3b5997c4101d47f97 (tree)
Time2012-04-11 23:34:54
AuthorHirami <tomohisa.hirami@nift...>
CommiterHirami

Log Message

AlertView related refactoring.

NewFolderAlert and EditCancelAlert

Change Summary

Incremental Difference

--- a/iOS/Tombo/Tombo/EditCancelAlert.h
+++ b/iOS/Tombo/Tombo/EditCancelAlert.h
@@ -1,4 +1,9 @@
11 #import <UIKit/UIKit.h>
22
3-@interface EditCancelAlert : UIAlertView
3+@interface EditCancelAlert : UIAlertView <UIAlertViewDelegate>
4+
5+- (id)initWithDefault;
6+
7+- (BOOL)showAndWait;
8+
49 @end
--- a/iOS/Tombo/Tombo/EditCancelAlert.m
+++ b/iOS/Tombo/Tombo/EditCancelAlert.m
@@ -1,4 +1,39 @@
11 #import "EditCancelAlert.h"
22
3-@implementation EditCancelAlert
3+@implementation EditCancelAlert {
4+ BOOL finished;
5+ BOOL isOK;
6+}
7+
8+- (id)initWithDefault {
9+ return [self initWithTitle:@"Confirm"
10+ message:@"Note is modified. Are you sure to discard changes?" delegate:self
11+ cancelButtonTitle:@"Cancel"
12+ otherButtonTitles:@"OK", nil];
13+}
14+
15+- (BOOL)showAndWait {
16+ [self show];
17+ finished = NO;
18+
19+ // Wait in runloop till a button is clicked.
20+ while (!finished) {
21+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
22+ beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]];
23+ }
24+ return isOK;
25+}
26+
27+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
28+ finished = YES;
29+
30+ if (buttonIndex == 0) {
31+ // Cancel
32+ isOK = NO;
33+ } else if (buttonIndex == 1) {
34+ isOK = YES;
35+ }
36+}
37+
38+
439 @end
--- a/iOS/Tombo/Tombo/MasterViewController.m
+++ b/iOS/Tombo/Tombo/MasterViewController.m
@@ -8,29 +8,7 @@
88 #import "FileItem.h"
99 #import "PasswordManager.h"
1010
11-@interface BackgroundView : UIView {
12-}
13-@end
14-
15-@implementation BackgroundView
16-- (id)initWithFrame:(CGRect)frame {
17- self = [super initWithFrame:frame];
18- if (self) {
19- self.backgroundColor = [UIColor clearColor];
20- }
21- return self;
22-}
23-
24-- (void)drawRect:(CGRect)rect {
25- UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:5.0];
26- [path moveToPoint:CGPointMake(0, self.bounds.size.height/2.0)];
27- [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2.0)];
28- [[UIColor whiteColor] set];
29- [path fill];
30-}
31-@end
32-
33-@interface MasterViewController () <UIAlertViewDelegate, UITableViewDelegate, UISplitViewControllerDelegate> {
11+@interface MasterViewController () <UITableViewDelegate, UISplitViewControllerDelegate> {
3412 NSMutableArray *_objects;
3513 Storage *storage;
3614 PasswordManager *passwordManager;
@@ -110,18 +88,12 @@
11088 }
11189
11290 - (void)createNewFolder:(id)sender {
113- NewFolderAlert *alert = [[NewFolderAlert alloc] initWithTitle:@"Folder name:"
114- message:@"\n"
115- delegate:self
116- cancelButtonTitle:@"Cancel"
117- otherButtonTitles:@"Done", nil];
118- BackgroundView *back = [[BackgroundView alloc] initWithFrame:CGRectMake(20.0, 43.0, 245.0, 25.0)];
119- [alert addSubview:back];
120- UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
121- [alert addSubview:textField];
122-
123- [alert show];
124-// [textField becomeFirstResponder];
91+ NewFolderAlert *alert = [[NewFolderAlert alloc] initWithDefault];
92+ NSString *folderName = [alert showAndWait];
93+ if (folderName) {
94+ FileItem *item = [storage newFolder:folderName];
95+ [self insertItem:item];
96+ }
12597 }
12698 #pragma mark - Item operations
12799
@@ -361,11 +333,10 @@
361333
362334 - (void)editViewControllerDidCancel:(EditViewController *)controller {
363335 if (controller.isModify) {
364- UIAlertView *alert = [[EditCancelAlert alloc] initWithTitle:@"Confirm"
365- message:@"Note is modified. Are you sure to discard changes?" delegate:self
366- cancelButtonTitle:@"OK"
367- otherButtonTitles:@"Cancel", nil];
368- [alert show];
336+ EditCancelAlert *cancel = [[EditCancelAlert alloc] initWithDefault];
337+ if ([cancel showAndWait]) {
338+ [self dismissModalViewControllerAnimated:YES];
339+ }
369340 } else {
370341 [self dismissModalViewControllerAnimated:YES];
371342 }
@@ -378,23 +349,4 @@
378349 [self insertItem:newItem];
379350 }
380351
381-#pragma mark - AlertViewDelegate
382-
383--(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
384- if ([alertView isKindOfClass:[EditCancelAlert class]]) {
385- if (buttonIndex == 0) {
386- [self dismissModalViewControllerAnimated:YES];
387- }
388- } else if ([alertView isKindOfClass:[NewFolderAlert class]]) {
389- if (buttonIndex == 1) {
390- //
391- UITextField *field = [alertView.subviews lastObject];
392- NSString *folderName = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
393- if (folderName.length > 0) {
394- FileItem *item = [storage newFolder:folderName];
395- [self insertItem:item];
396- }
397- }
398- }
399-}
400352 @end
--- a/iOS/Tombo/Tombo/NewFolderAlert.h
+++ b/iOS/Tombo/Tombo/NewFolderAlert.h
@@ -1,5 +1,10 @@
11 #import <UIKit/UIKit.h>
22
3-@interface NewFolderAlert : UIAlertView
3+@interface NewFolderAlert : UIAlertView <UIAlertViewDelegate>
44
5+@property (strong,nonatomic) NSString *folderName;
6+
7+- (id)initWithDefault;
8+
9+- (NSString *)showAndWait;
510 @end
--- a/iOS/Tombo/Tombo/NewFolderAlert.m
+++ b/iOS/Tombo/Tombo/NewFolderAlert.m
@@ -1,5 +1,49 @@
11 #import "NewFolderAlert.h"
22
3-@implementation NewFolderAlert
3+@implementation NewFolderAlert {
4+ BOOL finished;
5+}
6+
7+@synthesize folderName=_folderName;
8+
9+- (id)initWithDefault {
10+ return [self initWithTitle:@"New folder"
11+ message:@"Input folder name"
12+ delegate:self
13+ cancelButtonTitle:@"Cancel"
14+ otherButtonTitles:@"Done", nil];
15+}
16+
17+- (NSString *)showAndWait {
18+ self.alertViewStyle = UIAlertViewStylePlainTextInput;
19+ [self show];
20+ finished = NO;
21+
22+ // Wait in runloop till a button is clicked.
23+ while (!finished) {
24+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
25+ beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]];
26+ }
27+ return self.folderName;
28+}
29+
30+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
31+ finished = YES;
32+
33+ UITextField *field = [self textFieldAtIndex:0];
34+ if (buttonIndex == 0) {
35+ // Cancel
36+ self.folderName = nil;
37+ } else if (buttonIndex == 1) {
38+ // OK
39+ NSString *fname = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
40+ if (fname.length == 0) {
41+ self.folderName = nil;
42+ } else {
43+ self.folderName = fname;
44+ }
45+ }
46+ [field resignFirstResponder];
47+}
448
549 @end
Show on old repository browser