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 3 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
2 changes: 1 addition & 1 deletion Pod/Classes/OAStackView.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ typedef NS_ENUM(NSInteger, OAStackViewAlignment) {
NS_ASSUME_NONNULL_BEGIN
@interface OAStackView : UIView

@property(nonatomic,readonly,copy) NSArray *arrangedSubviews;
@property(nonatomic,readonly,copy) NSMutableArray *arrangedSubviews;
Copy link

Choose a reason for hiding this comment

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

I think the idea was to replicate UIStackView API, and in Apple docs arrangedSubviews is NSArray.

You could keep public array as immutable, and provide private mutable array, of which immutable copy could be returned getter of arrangedSubviews.

Choose a reason for hiding this comment

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

Looks good except the different API. As Grubas7 said I'd keep a mutable version internally (with a different property name) and override the arrangedSubviews getter to return a (non mutable) copy of the mutable array. This will allow you to keep the arrangedSubviews property in the header as an NSArray

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do


//Default is Vertical
@property(nonatomic) UILayoutConstraintAxis axis;
Expand Down
18 changes: 12 additions & 6 deletions Pod/Classes/OAStackView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#import "OATransformLayer.h"

@interface OAStackView ()
@property(nonatomic, copy) NSArray *arrangedSubviews;
@property(nonatomic, copy) NSMutableArray *arrangedSubviews;

@property(nonatomic) OAStackViewAlignmentStrategy *alignmentStrategy;
@property(nonatomic) OAStackViewDistributionStrategy *distributionStrategy;
Expand All @@ -33,18 +33,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 +53,10 @@ - (instancetype)initWithFrame:(CGRect)frame {
return [self initWithArrangedSubviews:@[]];
}

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

_axis = UILayoutConstraintAxisVertical;
_alignment = OAStackViewAlignmentFill;
_distribution = OAStackViewDistributionFill;
Expand Down Expand Up @@ -182,6 +184,7 @@ - (void)addArrangedSubview:(UIView *)view {
- (void)removeArrangedSubview:(UIView *)view {

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

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

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

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

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