Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored orientation management code for IndexViewController.

  • Loading branch information...
commit 0267cf0b360d17c50c1c5acdcf9e86f59f9e26aa 1 parent c905d43
@Simbul Simbul authored
View
5 Classes/IndexViewController.h
@@ -36,11 +36,14 @@
NSString *bookBundlePath;
NSString *fileName;
UIViewController<UIWebViewDelegate> *webViewDelegate;
- UIInterfaceOrientation currentOrientation;
+
+ int pageWidth;
+ int pageHeight;
}
- (id)initWithBookBundlePath:(NSString *)path fileName:(NSString *)name webViewDelegate:(UIViewController *)delegate;
- (void)loadContent;
+- (void)setPageSizeForOrientation:(UIInterfaceOrientation)orientation;
- (BOOL)isIndexViewHidden;
- (void)setIndexViewHidden:(BOOL)hidden withAnimation:(BOOL)animation;
- (void)rotateFromOrientation:(UIInterfaceOrientation)fromInterfaceOrientation toOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
View
57 Classes/IndexViewController.m
@@ -31,7 +31,7 @@
#import "IndexViewController.h"
-#define NAVIGATION_HEIGHT 150
+#define INDEX_HEIGHT 150
@implementation IndexViewController
@@ -40,7 +40,8 @@ - (id)initWithBookBundlePath:(NSString *)path fileName:(NSString *)name webViewD
bookBundlePath = path;
fileName = name;
webViewDelegate = delegate;
- currentOrientation = UIInterfaceOrientationPortrait;
+
+ [self setPageSizeForOrientation:UIInterfaceOrientationPortrait];
return [self initWithNibName:nil bundle:nil];
}
@@ -72,7 +73,7 @@ - (void)didReceiveMemoryWarning
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
- UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 1024, 768, NAVIGATION_HEIGHT)];
+ UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 1024, 768, INDEX_HEIGHT)];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
webView.delegate = self;
@@ -82,46 +83,36 @@ - (void)loadView
[self loadContent];
}
-- (BOOL)isIndexViewHidden {
- if (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown) {
- return self.view.frame.origin.y > 1024 - NAVIGATION_HEIGHT;
+- (void)setPageSizeForOrientation:(UIInterfaceOrientation)orientation {
+ CGRect screenBounds = [[UIScreen mainScreen] bounds];
+
+ if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
+ pageWidth = screenBounds.size.height;
+ pageHeight = screenBounds.size.width;
} else {
- return self.view.frame.origin.y > 768 - NAVIGATION_HEIGHT;
- }
+ pageWidth = screenBounds.size.width;
+ pageHeight = screenBounds.size.height;
+ }
+ NSLog(@"Set IndexView size to %dx%d", pageWidth, pageHeight);
+}
+- (BOOL)isIndexViewHidden {
+ return self.view.frame.origin.y > pageHeight - INDEX_HEIGHT;
}
- (void)setIndexViewHidden:(BOOL)hidden withAnimation:(BOOL)animation {
- if (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown) {
- if (hidden) {
- self.view.frame = CGRectMake(0, 1024, 768, NAVIGATION_HEIGHT);
- } else {
- self.view.frame = CGRectMake(0, 1024 - NAVIGATION_HEIGHT, 768, NAVIGATION_HEIGHT);
- }
+ if (hidden) {
+ self.view.frame = CGRectMake(0, pageHeight, pageWidth, INDEX_HEIGHT);
} else {
- if (hidden) {
- self.view.frame = CGRectMake(0, 768, 1024, NAVIGATION_HEIGHT);
- } else {
- self.view.frame = CGRectMake(0, 768 - NAVIGATION_HEIGHT, 1024, NAVIGATION_HEIGHT);
- }
+ self.view.frame = CGRectMake(0, pageHeight - INDEX_HEIGHT, pageWidth, INDEX_HEIGHT);
}
}
- (void)rotateFromOrientation:(UIInterfaceOrientation)fromInterfaceOrientation toOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
- if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
- if ([self isIndexViewHidden]) {
- self.view.frame = CGRectMake(0, 768, 1024, NAVIGATION_HEIGHT);
- } else {
- self.view.frame = CGRectMake(0, 768 - NAVIGATION_HEIGHT, 1024, NAVIGATION_HEIGHT);
- }
- } else {
- if ([self isIndexViewHidden]) {
- self.view.frame = CGRectMake(0, 1024, 768, NAVIGATION_HEIGHT);
- } else {
- self.view.frame = CGRectMake(0, 1024 - NAVIGATION_HEIGHT, 768, NAVIGATION_HEIGHT);
- }
- }
- currentOrientation = toInterfaceOrientation;
+ BOOL hidden = [self isIndexViewHidden]; // cache hidden status before setting page size
+
+ [self setPageSizeForOrientation:toInterfaceOrientation];
+ [self setIndexViewHidden:hidden withAnimation:NO];
}
- (void)loadContent {
View
30 IndexViewControllerTest.h
@@ -0,0 +1,30 @@
+//
+// IndexViewControllerTest.h
+// Baker
+//
+// Created by Alessandro Morandi on 10/05/2011.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+// See Also: http://developer.apple.com/iphone/library/documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html
+
+// Application unit tests contain unit test code that must be injected into an application to run correctly.
+// Define USE_APPLICATION_UNIT_TEST to 0 if the unit test code is designed to be linked into an independent test executable.
+
+#define USE_APPLICATION_UNIT_TEST 1
+
+#import <SenTestingKit/SenTestingKit.h>
+#import <UIKit/UIKit.h>
+//#import "application_headers" as required
+
+
+@interface IndexViewControllerTest : SenTestCase {
+
+}
+
+#if USE_APPLICATION_UNIT_TEST
+- (void)testAppDelegate; // simple test on application
+#else
+- (void)testMath; // simple standalone test
+#endif
+
+@end
View
33 IndexViewControllerTest.m
@@ -0,0 +1,33 @@
+//
+// IndexViewControllerTest.m
+// Baker
+//
+// Created by Alessandro Morandi on 10/05/2011.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "IndexViewControllerTest.h"
+
+
+@implementation IndexViewControllerTest
+
+#if USE_APPLICATION_UNIT_TEST // all code under test is in the iPhone Application
+
+- (void)testAppDelegate {
+
+ id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];
+ STAssertNotNil(yourApplicationDelegate, @"UIApplication failed to find the AppDelegate");
+
+}
+
+#else // all code under test must be linked into the Unit Test bundle
+
+- (void)testMath {
+
+ STAssertTrue((1+1)==2, @"Compiler isn't feeling well today :-(" );
+
+}
+
+#endif
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.