Skip to content

Commit

Permalink
Format brackets position
Browse files Browse the repository at this point in the history
  • Loading branch information
kirpichenko committed Nov 21, 2013
1 parent 135a885 commit 2e28fb8
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 58 deletions.
3 changes: 2 additions & 1 deletion EKKeyboardAvoiding-UnitTests/ContenInsetRotationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ - (void)testLandscapeShowKeyboardAndRotate

#pragma mark - helpers

- (void)updateContentInset:(UIEdgeInsets)contentInset {
- (void)updateContentInset:(UIEdgeInsets)contentInset
{
[[self scrollView] setContentInset:contentInset];
}

Expand Down
27 changes: 18 additions & 9 deletions EKKeyboardAvoiding-UnitTests/ContentInsetsCalculationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#import "EKFakeKeyboard.h"
#import "EKFakeKeyboardFrameListener.h"

@interface EKKeyboardAvoidingProviderTest : XCTestCase {
@interface EKKeyboardAvoidingProviderTest : XCTestCase
{
UIScrollView *scrollView;
EKKeyboardAvoidingProvider *avoidingProvider;
}
Expand All @@ -22,7 +23,8 @@ @interface EKKeyboardAvoidingProviderTest : XCTestCase {

@implementation EKKeyboardAvoidingProviderTest

- (void)setUp {
- (void)setUp
{
[super setUp];

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 160, 320, 200)];
Expand All @@ -34,11 +36,13 @@ - (void)setUp {
[avoidingProvider startAvoiding];
}

- (void)tearDown {
- (void)tearDown
{
[super tearDown];
}

- (void)testBottomIntersectionWithoutCoverage {
- (void)testBottomIntersectionWithoutCoverage
{
[EKFakeKeyboard showFromBottom];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(40, 0, 100, 0);
Expand All @@ -48,7 +52,8 @@ - (void)testBottomIntersectionWithoutCoverage {
XCTAssertEqual(expectedInset, calculatedInset);
}

- (void)testBottomIntersectionWithCoverage {
- (void)testBottomIntersectionWithCoverage
{
[EKFakeKeyboard showWithFrame:CGRectMake(0, 240, 320, 216)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(40, 0, 120, 0);
Expand All @@ -58,7 +63,8 @@ - (void)testBottomIntersectionWithCoverage {
XCTAssertEqual(expectedInset, calculatedInset);
}

- (void)testTopIntersectionWithoutCoverage {
- (void)testTopIntersectionWithoutCoverage
{
[EKFakeKeyboard showWithFrame:CGRectMake(0, -40, 320, 216)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(40, 0, 100, 0);
Expand All @@ -68,7 +74,8 @@ - (void)testTopIntersectionWithoutCoverage {
XCTAssertEqual(expectedInset, calculatedInset);
}

- (void)testTopIntersectionWithCoverage {
- (void)testTopIntersectionWithCoverage
{
[EKFakeKeyboard showFromTop];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(56, 0, 100, 0);
Expand All @@ -78,7 +85,8 @@ - (void)testTopIntersectionWithCoverage {
XCTAssertEqual(expectedInset, calculatedInset);
}

- (void)testMiddleIntersection {
- (void)testMiddleIntersection
{
[EKFakeKeyboard showWithFrame:CGRectMake(0, 200, 320, 100)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(40, 0, 100, 0);
Expand All @@ -88,7 +96,8 @@ - (void)testMiddleIntersection {
XCTAssertEqual(expectedInset, calculatedInset);
}

- (void)testAppearanceWithoutIntersection {
- (void)testAppearanceWithoutIntersection
{
[EKFakeKeyboard showWithFrame:CGRectMake(0, 480, 320, 216)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(40, 0, 100, 0);
Expand Down
21 changes: 14 additions & 7 deletions EKKeyboardAvoiding/EKAvoidingInsetCalculator.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ @implementation EKAvoidingInsetCalculator

#pragma mark - public methods

- (UIEdgeInsets)calculateAvoidingInset {
- (UIEdgeInsets)calculateAvoidingInset
{
UIEdgeInsets contentInset = UIEdgeInsetsZero;
if ([self intersectionExists]) {
if ([self keyboardAtTheTop]) {
if ([self intersectionExists])
{
if ([self keyboardAtTheTop])
{
CGFloat coverage = CGRectGetMaxY(keyboardFrame) - CGRectGetMinY(scrollViewFrame);
contentInset.top = MAX(coverage - scrollViewInset.top, 0);
}
else if ([self keyboardAtTheBottom] ) {
else if ([self keyboardAtTheBottom])
{
CGFloat coverage = CGRectGetMaxY(scrollViewFrame) - CGRectGetMinY(keyboardFrame);
contentInset.bottom = MAX(coverage - scrollViewInset.bottom, 0);
}
Expand All @@ -33,7 +37,8 @@ - (UIEdgeInsets)calculateAvoidingInset {

#pragma mark - find intersection

- (BOOL)intersectionExists {
- (BOOL)intersectionExists
{
BOOL intersect = CGRectIntersectsRect(keyboardFrame, scrollViewFrame);
BOOL keyboardContains = CGRectContainsRect(keyboardFrame, scrollViewFrame);
BOOL sameHeight = scrollViewFrame.size.height == keyboardFrame.size.height;
Expand All @@ -42,11 +47,13 @@ - (BOOL)intersectionExists {
return (intersect && !keyboardContains && !sameHeight && !empty);
}

- (BOOL)keyboardAtTheTop {
- (BOOL)keyboardAtTheTop
{
return keyboardFrame.origin.y <= scrollViewFrame.origin.y;
}

- (BOOL)keyboardAtTheBottom {
- (BOOL)keyboardAtTheBottom
{
return (keyboardFrame.origin.y <= CGRectGetMaxY(scrollViewFrame) &&
CGRectGetMaxY(keyboardFrame) >= CGRectGetMaxY(scrollViewFrame));
}
Expand Down
44 changes: 28 additions & 16 deletions EKKeyboardAvoiding/EKKeyboardAvoidingProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,35 @@ @interface EKKeyboardAvoidingProvider ()

@implementation EKKeyboardAvoidingProvider

- (id)initWithScrollView:(UIScrollView *)scrollView {
if (self = [super init]) {
- (id)initWithScrollView:(UIScrollView *)scrollView
{
if (self = [super init])
{
[self setScrollView:scrollView];
}
return self;
}

- (void)dealloc {
NSLog(@"ekavoiding dealloc");

- (void)dealloc
{
[self stopAvoiding];
}

#pragma mark - public methods

- (void)startAvoiding {
if (!self.avoidingStarted) {
- (void)startAvoiding
{
if (!self.avoidingStarted)
{
[self beginKeyboardFrameObserving];
[self setAvoidingStarted:YES];
}
}

- (void)stopAvoiding {
if (self.avoidingStarted) {
- (void)stopAvoiding
{
if (self.avoidingStarted)
{
[self resetAvoidingContentInset];
[self endKeyboardFrameObserving];
[self setAvoidingStarted:NO];
Expand All @@ -53,38 +58,44 @@ - (void)stopAvoiding {

#pragma mark - private methods

- (void)beginKeyboardFrameObserving {
- (void)beginKeyboardFrameObserving
{
[[self keyboardListener] addObserver:self forKeyPath:kKeyboardFrameKey];
}

- (void)endKeyboardFrameObserving {
- (void)endKeyboardFrameObserving
{
[[self keyboardListener] removeObserver:self forKeyPath:kKeyboardFrameKey];
}

#pragma mark - update inset

- (void)addExtraContentInset:(UIEdgeInsets)extraContentInset {
- (void)addExtraContentInset:(UIEdgeInsets)extraContentInset
{
UIEdgeInsets currentInset = [self.scrollView contentInset];
currentInset.top += extraContentInset.top;
currentInset.bottom += extraContentInset.bottom;

[self applyAvoidingContentInset:currentInset];
}

- (void)resetAvoidingContentInset {
- (void)resetAvoidingContentInset
{
UIEdgeInsets currentInset = [self.scrollView contentInset];
currentInset.top -= self.extraContentInset.top;
currentInset.bottom -= self.extraContentInset.bottom;

[self applyAvoidingContentInset:currentInset];
}

- (void)applyAvoidingContentInset:(UIEdgeInsets)avoidingInset {
- (void)applyAvoidingContentInset:(UIEdgeInsets)avoidingInset
{
[[self scrollView] setContentInset:avoidingInset];
[[self scrollView] setScrollIndicatorInsets:avoidingInset];
}

- (UIEdgeInsets)calculateExtraContentInset {
- (UIEdgeInsets)calculateExtraContentInset
{
EKAvoidingInsetCalculator *calculator = [[EKAvoidingInsetCalculator alloc] init];

CGRect keyboardFrame = [self.keyboardListener convertedKeyboardFrameForView:self.scrollView];
Expand All @@ -100,7 +111,8 @@ - (UIEdgeInsets)calculateExtraContentInset {
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (keyPath == kKeyboardFrameKey) {
if (keyPath == kKeyboardFrameKey)
{
[self resetAvoidingContentInset];

UIEdgeInsets newInset = [self calculateExtraContentInset];
Expand Down
21 changes: 14 additions & 7 deletions EKKeyboardAvoiding/EKKeyboardFrameListener.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,45 @@ @implementation EKKeyboardFrameListener

#pragma mark life cycle

- (id)init {
if (self = [super init]) {
- (id)init
{
if (self = [super init])
{
[self startNotificationsObseving];
}
return self;
}

- (void)dealloc {
- (void)dealloc
{
[self stopNotificationsObserving];
}

#pragma mark - public methods

- (CGRect)convertedKeyboardFrameForView:(UIView *)view {
- (CGRect)convertedKeyboardFrameForView:(UIView *)view
{
CGRect convertedFrame = [[view superview] convertRect:[self keyboardFrame] fromView:nil];
return convertedFrame;
}

#pragma mark - private methods

- (void)startNotificationsObseving {
- (void)startNotificationsObseving
{
[self observeNotificationNamed:UIKeyboardDidChangeFrameNotification
action:@selector(keyboardDidChangeFrame:)];
}

#pragma mark - observe keyboard frame

- (void)keyboardDidChangeFrame:(NSNotification *)notification {
- (void)keyboardDidChangeFrame:(NSNotification *)notification
{
self.keyboardInfo = [notification userInfo];

NSValue *frameValue = [self.keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
if (!CGRectEqualToRect(self.keyboardFrame, [frameValue CGRectValue])) {
if (!CGRectEqualToRect(self.keyboardFrame, [frameValue CGRectValue]))
{
self.keyboardFrame = [frameValue CGRectValue];
}
}
Expand Down
15 changes: 10 additions & 5 deletions EKKeyboardAvoiding/NSObject+EKKeyboardAvoiding.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,34 @@ @implementation NSObject (EKKeyboardAvoiding)

#pragma mark - associate objects

- (void)associateObject:(id)object forKey:(NSString *)key {
- (void)associateObject:(id)object forKey:(NSString *)key
{
objc_setAssociatedObject(self, [key UTF8String], object, OBJC_ASSOCIATION_RETAIN);
}

- (id)associatedObjectForKey:(NSString *)key {
- (id)associatedObjectForKey:(NSString *)key
{
return objc_getAssociatedObject(self, [key UTF8String]);
}

#pragma mark - observe notifications

- (void)observeNotificationNamed:(NSString *)notificationName action:(SEL)action {
- (void)observeNotificationNamed:(NSString *)notificationName action:(SEL)action
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:action name:notificationName object:nil];
}

- (void)stopNotificationsObserving {
- (void)stopNotificationsObserving
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}

#pragma mark - observe key path

- (void)addObserver:(id)target forKeyPath:(NSString *)keyPath {
- (void)addObserver:(id)target forKeyPath:(NSString *)keyPath
{
[self addObserver:target forKeyPath:keyPath
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
Expand Down
Loading

0 comments on commit 2e28fb8

Please sign in to comment.