OSDN Git Service

Notes ordering.
authorHirami <tomohisa.hirami@nifty.com>
Sat, 14 Apr 2012 09:50:07 +0000 (18:50 +0900)
committerHirami <tomohisa.hirami@nifty.com>
Sat, 14 Apr 2012 09:50:07 +0000 (18:50 +0900)
iOS/Tombo/Tombo.xcodeproj/project.pbxproj
iOS/Tombo/Tombo/FileItem.h
iOS/Tombo/Tombo/FileItem.m
iOS/Tombo/Tombo/MasterViewController.m
iOS/Tombo/TomboTests/FileItemTest.h [new file with mode: 0644]
iOS/Tombo/TomboTests/FileItemTest.m [new file with mode: 0644]

index 3b217f2..a1dd6d9 100644 (file)
@@ -17,6 +17,7 @@
                9243C825153487640092B506 /* key-32.png in Resources */ = {isa = PBXBuildFile; fileRef = 9243C823153487640092B506 /* key-32.png */; };
                9243C826153487640092B506 /* key-48.png in Resources */ = {isa = PBXBuildFile; fileRef = 9243C824153487640092B506 /* key-48.png */; };
                9243C83115352EB20092B506 /* SinglePasswordDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = 9243C83015352EB20092B506 /* SinglePasswordDialog.m */; };
                9243C825153487640092B506 /* key-32.png in Resources */ = {isa = PBXBuildFile; fileRef = 9243C823153487640092B506 /* key-32.png */; };
                9243C826153487640092B506 /* key-48.png in Resources */ = {isa = PBXBuildFile; fileRef = 9243C824153487640092B506 /* key-48.png */; };
                9243C83115352EB20092B506 /* SinglePasswordDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = 9243C83015352EB20092B506 /* SinglePasswordDialog.m */; };
+               9244E4D115397C300028FD10 /* FileItemTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9244E4D015397C300028FD10 /* FileItemTest.m */; };
                92DE332D151E277D00AD06EC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE332C151E277D00AD06EC /* UIKit.framework */; };
                92DE332F151E277D00AD06EC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE332E151E277D00AD06EC /* Foundation.framework */; };
                92DE3331151E277D00AD06EC /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE3330151E277D00AD06EC /* CoreGraphics.framework */; };
                92DE332D151E277D00AD06EC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE332C151E277D00AD06EC /* UIKit.framework */; };
                92DE332F151E277D00AD06EC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE332E151E277D00AD06EC /* Foundation.framework */; };
                92DE3331151E277D00AD06EC /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE3330151E277D00AD06EC /* CoreGraphics.framework */; };
@@ -69,6 +70,8 @@
                9243C824153487640092B506 /* key-48.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "key-48.png"; sourceTree = "<group>"; };
                9243C82F15352EB20092B506 /* SinglePasswordDialog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SinglePasswordDialog.h; sourceTree = "<group>"; };
                9243C83015352EB20092B506 /* SinglePasswordDialog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SinglePasswordDialog.m; sourceTree = "<group>"; };
                9243C824153487640092B506 /* key-48.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "key-48.png"; sourceTree = "<group>"; };
                9243C82F15352EB20092B506 /* SinglePasswordDialog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SinglePasswordDialog.h; sourceTree = "<group>"; };
                9243C83015352EB20092B506 /* SinglePasswordDialog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SinglePasswordDialog.m; sourceTree = "<group>"; };
+               9244E4CF15397C300028FD10 /* FileItemTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileItemTest.h; sourceTree = "<group>"; };
+               9244E4D015397C300028FD10 /* FileItemTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileItemTest.m; sourceTree = "<group>"; };
                92DE3328151E277D00AD06EC /* Tombo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tombo.app; sourceTree = BUILT_PRODUCTS_DIR; };
                92DE332C151E277D00AD06EC /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
                92DE332E151E277D00AD06EC /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
                92DE3328151E277D00AD06EC /* Tombo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tombo.app; sourceTree = BUILT_PRODUCTS_DIR; };
                92DE332C151E277D00AD06EC /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
                92DE332E151E277D00AD06EC /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
                        children = (
                                9243C7F61532435B0092B506 /* CryptTest.h */,
                                9243C7F71532435B0092B506 /* CryptTest.m */,
                        children = (
                                9243C7F61532435B0092B506 /* CryptTest.h */,
                                9243C7F71532435B0092B506 /* CryptTest.m */,
+                               9244E4CF15397C300028FD10 /* FileItemTest.h */,
+                               9244E4D015397C300028FD10 /* FileItemTest.m */,
                                92DE3357151E277E00AD06EC /* Supporting Files */,
                        );
                        path = TomboTests;
                                92DE3357151E277E00AD06EC /* Supporting Files */,
                        );
                        path = TomboTests;
                        buildActionMask = 2147483647;
                        files = (
                                9243C7F81532435C0092B506 /* CryptTest.m in Sources */,
                        buildActionMask = 2147483647;
                        files = (
                                9243C7F81532435C0092B506 /* CryptTest.m in Sources */,
+                               9244E4D115397C300028FD10 /* FileItemTest.m in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index c5beffc..c4e4596 100644 (file)
@@ -18,4 +18,6 @@
 
 - (BOOL)isNewItem;
 
 
 - (BOOL)isNewItem;
 
+- (NSComparisonResult)compare:(FileItem*)other;
+
 @end
 @end
index 7206d8b..9bd18a3 100644 (file)
 - (BOOL)isNewItem {
     return self.name == nil;
 }
 - (BOOL)isNewItem {
     return self.name == nil;
 }
+
+- (NSComparisonResult)compare:(FileItem*)other {
+    if (self.isUp) return NSOrderedAscending;
+    if (other.isUp) return NSOrderedDescending;
+    
+    if (self.isDirectory) {
+        if (other.isDirectory) {
+            return [self.name compare:other.name];
+        } else {
+            return NSOrderedAscending;
+        }
+    } else {
+        if (other.isDirectory) {
+            return NSOrderedDescending;
+        } else {
+            return [self.name compare:other.name];            
+        }
+    }
+}
+
 @end
 @end
index bc7fdf6..3079b58 100644 (file)
     if (!_objects) {
         _objects = [[NSMutableArray alloc] init];
     }
     if (!_objects) {
         _objects = [[NSMutableArray alloc] init];
     }
-    [_objects insertObject:item atIndex:0];
-    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
+    NSUInteger n = [_objects count];
+    NSUInteger i;
+    for (i = 0; i < n; i++) {
+        if (item.isUp) break;
+        
+        FileItem *cur = [_objects objectAtIndex:i];
+        NSComparisonResult r = [item compare:cur];
+        if (r == NSOrderedAscending || r == NSOrderedSame) {
+            break;
+        }
+    }
+    
+    [_objects insertObject:item atIndex:i];
+    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                           withRowAnimation:UITableViewRowAnimationAutomatic];
 }
     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                           withRowAnimation:UITableViewRowAnimationAutomatic];
 }
diff --git a/iOS/Tombo/TomboTests/FileItemTest.h b/iOS/Tombo/TomboTests/FileItemTest.h
new file mode 100644 (file)
index 0000000..ae83742
--- /dev/null
@@ -0,0 +1,5 @@
+#import <SenTestingKit/SenTestingKit.h>
+
+@interface FileItemTest : SenTestCase
+
+@end
diff --git a/iOS/Tombo/TomboTests/FileItemTest.m b/iOS/Tombo/TomboTests/FileItemTest.m
new file mode 100644 (file)
index 0000000..a3deb05
--- /dev/null
@@ -0,0 +1,44 @@
+#import "FileItemTest.h"
+#import "FileItem.h"
+
+@implementation FileItemTest
+
+- (void)testCompare1 {
+    FileItem *f1 = [FileItem allocWithName:@"abc"];
+    FileItem *f2 = [FileItem allocWithName:@"def"];
+    
+    NSComparisonResult r = [f1 compare:f2];
+    STAssertTrue(r == NSOrderedAscending, @"A1");
+    
+    r = [f1 compare:f1];
+    STAssertTrue(r == NSOrderedSame, @"A2");
+
+    r = [f2 compare:f1];
+    STAssertTrue(r == NSOrderedDescending, @"A3");    
+}
+
+- (void)testCompare2 {
+    FileItem *f1 = [FileItem allocWithName:@"abc"];
+    FileItem *f2 = [FileItem allocWithName:@"def"];
+    f2.isDirectory = YES;
+    
+    NSComparisonResult r = [f1 compare:f2];
+    STAssertTrue(r == NSOrderedDescending, @"A4");
+    
+    r = [f2 compare:f1];
+    STAssertTrue(r == NSOrderedAscending, @"A5");
+}
+
+- (void)testCompare3 {
+    FileItem *f1 = [FileItem allocWithName:@"abc"];
+    FileItem *f2 = [FileItem allocWithName:@"def"];
+    f2.isUp = YES;
+    
+    NSComparisonResult r = [f1 compare:f2];
+    STAssertTrue(r == NSOrderedDescending, @"A6");
+
+    r = [f2 compare:f1];
+    STAssertTrue(r == NSOrderedAscending, @"A7");
+    
+}
+@end