Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain arranged subviews array #56

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions Example/Tests/OAStackViewSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,58 @@
});


context(@"Arranged Subviews", ^{

__block UIView *view1, *view2, *view3;

beforeEach(^{
view1 = createView(100, 100);
view2 = createView(100, 100);
view3 = createView(100, 100);

stackView = [[OAStackView alloc] init];
stackView.translatesAutoresizingMaskIntoConstraints = NO;
});

it(@"Initializes arrangedSubviews to an empty array", ^{
[[stackView.arrangedSubviews should] beEmpty];
});

it(@"Initalizes arrangedSubviews to the given views when initialized with arranged subviess", ^{
stackView = [[OAStackView alloc] initWithArrangedSubviews:@[view1, view2, view3]];
[[stackView.arrangedSubviews should] containObjectsInArray:@[view1, view2, view3]];
});

it(@"Maintains the correct array when adding views", ^{
[stackView addArrangedSubview:view1];
[[stackView.arrangedSubviews should] containObjectsInArray:@[view1]];

[stackView addArrangedSubview:view2];
[[stackView.arrangedSubviews should] containObjectsInArray:@[view2]];

[stackView addArrangedSubview:view3];
[[stackView.arrangedSubviews should] containObjectsInArray:@[view3]];
});

it(@"Maintains the correct array when removing views", ^{
[stackView addArrangedSubview:view1];
[stackView addArrangedSubview:view2];
[stackView addArrangedSubview:view3];

[[stackView.arrangedSubviews should] containObjectsInArray:@[view1, view2, view3]];

[stackView removeArrangedSubview:view2];
[[stackView.arrangedSubviews should] containObjectsInArray:@[view1, view3]];

[stackView removeArrangedSubview:view1];
[[stackView.arrangedSubviews should] containObjectsInArray:@[view3]];

[stackView removeArrangedSubview:view3];
[[stackView.arrangedSubviews should] containObjectsInArray:@[]];
});
});


context(@"bug fixes", ^{

__block UIView *view1, *view2, *view3;
Expand Down
21 changes: 16 additions & 5 deletions Pod/Classes/OAStackView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@interface OAStackView ()
@property(nonatomic, copy) NSArray *arrangedSubviews;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this property is not used anymore I have removed it after rebasing.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this property is used in line 76. It's provided to be consist with iOS9 API.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes its used in line 76 but the backing property and its Ivar are not needed. I only removed the property from the .m file, the .h file still has the readonly one.

@property(nonatomic, copy) NSMutableArray *mutableArrangedSubviews;

@property(nonatomic) OAStackViewAlignmentStrategy *alignmentStrategy;
@property(nonatomic) OAStackViewDistributionStrategy *distributionStrategy;
Expand All @@ -33,18 +34,17 @@ - (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];

if (self) {
[self commonInit];
[self commonInitWithInitalSubviews:@[]];
}

return self;
}

- (instancetype)initWithArrangedSubviews:(NSArray*)views {
- (instancetype)initWithArrangedSubviews:(NSArray *)views {
self = [super initWithFrame:CGRectZero];

if (self) {
[self addViewsAsSubviews:views];
[self commonInit];
[self commonInitWithInitalSubviews:views];
}

return self;
Expand All @@ -54,7 +54,10 @@ - (instancetype)initWithFrame:(CGRect)frame {
return [self initWithArrangedSubviews:@[]];
}

- (void)commonInit {
- (void)commonInitWithInitalSubviews:(NSArray *)initialSubviews {
_mutableArrangedSubviews = [initialSubviews mutableCopy];
[self addViewsAsSubviews:initialSubviews];

_axis = UILayoutConstraintAxisVertical;
_alignment = OAStackViewAlignmentFill;
_distribution = OAStackViewDistributionFill;
Expand All @@ -70,6 +73,10 @@ - (void)commonInit {

#pragma mark - Properties

- (NSArray *)arrangedSubviews {
return self.mutableArrangedSubviews.copy;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
// Does not have any effect because `CATransformLayer` is not rendered.
}
Expand Down Expand Up @@ -182,6 +189,7 @@ - (void)addArrangedSubview:(UIView *)view {
- (void)removeArrangedSubview:(UIView *)view {

if (self.subviews.count == 1) {
[self.mutableArrangedSubviews removeObject:view];
[view removeFromSuperview];
return;
}
Expand Down Expand Up @@ -211,6 +219,7 @@ - (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex newI
}

if (newItem) {
[self.mutableArrangedSubviews addObject:view];
[self addSubview:view];
}

Expand Down Expand Up @@ -238,6 +247,7 @@ - (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex newI
[self removeConstraints:constraints];

if (newItem) {
[self.mutableArrangedSubviews insertObject:view atIndex:stackIndex];
[self insertSubview:view atIndex:stackIndex];
}
}
Expand All @@ -257,6 +267,7 @@ - (void)removeViewFromArrangedViews:(UIView*)view permanently:(BOOL)permanently
id nextView = [self visibleViewAfterView:view];

if (permanently) {
[self.mutableArrangedSubviews removeObject:view];
[view removeFromSuperview];
} else {
NSArray *constraint = [self constraintsAffectingView:view];
Expand Down