Skip to content

Commit 9edde65

Browse files
committed
Implemented feedback
1 parent 05b6cfc commit 9edde65

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

Classes/BEMCheckBox.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ - (void)_setOn:(BOOL)on animated:(BOOL)animated notifyGroup:(BOOL)notifyGroup {
132132
}
133133
}
134134

135-
// Notify our group if we have one that something has changed
136135
if(notifyGroup){
137136
[self.group _checkBoxSelectionChanged:self];
138137
}

Classes/BEMCheckBoxGroup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
// Copyright © 2016 Boris Emorine. All rights reserved.
77
//
88

9-
#import <Foundation/Foundation.h>
9+
#import <UIKit/UIKit.h>
1010

1111
@class BEMCheckBox;
1212

1313
@interface BEMCheckBoxGroup : NSObject
1414

1515
/** An array of check boxes in this group.
1616
*/
17-
@property (nonatomic, strong, nonnull, readonly) NSArray<BEMCheckBox *> *checkBoxes;
17+
@property (nonatomic, strong, nonnull, readonly) NSOrderedSet<BEMCheckBox *> *checkBoxes;
1818

1919
/** The currently selected check box. Only can be nil if mustHaveSelection is NO. Setting this value will cause the other check boxes to deselect automatically.
2020
*/

Classes/BEMCheckBoxGroup.m

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@interface BEMCheckBoxGroup ()
1313

14-
@property (nonatomic, strong, nonnull) NSArray<BEMCheckBox *> *checkBoxes;
14+
@property (nonatomic, strong, nonnull) NSOrderedSet<BEMCheckBox *> *checkBoxes;
1515

1616
@end
1717

@@ -31,7 +31,7 @@ - (instancetype)init {
3131
self = [super init];
3232
if (self) {
3333
_mustHaveSelection = NO;
34-
_checkBoxes = @[];
34+
_checkBoxes = [NSOrderedSet orderedSet];
3535
}
3636
return self;
3737
}
@@ -46,14 +46,15 @@ + (nonnull instancetype)groupWithCheckBoxes:(nullable NSArray<BEMCheckBox *> *)c
4646
}
4747

4848
- (void)addCheckBoxToGroup:(nonnull BEMCheckBox *)checkBox {
49-
if ([checkBox group]) {
50-
// Already has a group, remove first
51-
[[checkBox group] removeCheckBoxFromGroup:checkBox];
49+
if (checkBox.group) {
50+
[checkBox.group removeCheckBoxFromGroup:checkBox];
5251
}
5352

5453
[checkBox _setOn:NO animated:NO notifyGroup:NO];
55-
[checkBox setGroup:self];
56-
self.checkBoxes = [self.checkBoxes arrayByAddingObject:checkBox];
54+
checkBox.group = self;
55+
NSMutableOrderedSet *mutableBoxes = [self.checkBoxes mutableCopy];
56+
[mutableBoxes addObject:checkBox];
57+
self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet:mutableBoxes];
5758
}
5859

5960
- (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox {
@@ -62,42 +63,46 @@ - (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox {
6263
return;
6364
}
6465

65-
[checkBox setGroup:nil];
66-
NSMutableArray *mutableBoxes = [self.checkBoxes mutableCopy];
66+
checkBox.group = nil;
67+
NSMutableOrderedSet *mutableBoxes = [self.checkBoxes mutableCopy];
6768
[mutableBoxes removeObject:checkBox];
68-
self.checkBoxes = [NSArray arrayWithArray:mutableBoxes];
69+
self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet:mutableBoxes];
6970
}
7071

72+
#pragma mark Getters
73+
7174
- (BEMCheckBox *)selectedCheckBox {
72-
BEMCheckBox *checkbox = nil;
73-
for (BEMCheckBox *b in self.checkBoxes) {
74-
if([b on]){
75-
checkbox = b;
75+
BEMCheckBox *selected = nil;
76+
for (BEMCheckBox *checkBox in self.checkBoxes) {
77+
if(checkBox.on){
78+
selected = checkBox;
7679
break;
7780
}
7881
}
7982

80-
return checkbox;
83+
return selected;
8184
}
8285

86+
#pragma mark Setters
87+
8388
- (void)setSelectedCheckBox:(BEMCheckBox *)selectedCheckBox {
8489
if (selectedCheckBox) {
8590
for (BEMCheckBox *checkBox in self.checkBoxes) {
8691
BOOL shouldBeOn = (checkBox == selectedCheckBox);
87-
if([checkBox on] != shouldBeOn){
92+
if(checkBox.on != shouldBeOn){
8893
[checkBox _setOn:shouldBeOn animated:YES notifyGroup:NO];
8994
}
9095
}
9196
} else {
9297
// Selection is nil
9398
if(self.mustHaveSelection && [self.checkBoxes count] > 0){
9499
// We must have a selected checkbox, so re-call this method with the first checkbox
95-
[self setSelectedCheckBox:[self.checkBoxes firstObject]];
100+
self.selectedCheckBox = [self.checkBoxes firstObject];
96101
} else {
97-
for (BEMCheckBox *b in self.checkBoxes) {
102+
for (BEMCheckBox *checkBox in self.checkBoxes) {
98103
BOOL shouldBeOn = NO;
99-
if([b on] != shouldBeOn){
100-
[b _setOn:shouldBeOn animated:YES notifyGroup:NO];
104+
if(checkBox.on != shouldBeOn){
105+
[checkBox _setOn:shouldBeOn animated:YES notifyGroup:NO];
101106
}
102107
}
103108
}
@@ -109,7 +114,7 @@ - (void)setMustHaveSelection:(BOOL)mustHaveSelection {
109114

110115
// If it must have a selection and we currently don't, select the first box
111116
if (mustHaveSelection && !self.selectedCheckBox) {
112-
[self setSelectedCheckBox:[self.checkBoxes firstObject]];
117+
self.selectedCheckBox = [self.checkBoxes firstObject];
113118
}
114119
}
115120

@@ -118,10 +123,10 @@ - (void)setMustHaveSelection:(BOOL)mustHaveSelection {
118123
- (void)_checkBoxSelectionChanged:(BEMCheckBox *)checkBox {
119124
if ([checkBox on]) {
120125
// Change selected checkbox to this one
121-
[self setSelectedCheckBox:checkBox];
126+
self.selectedCheckBox = checkBox;
122127
} else if(checkBox == self.selectedCheckBox) {
123128
// Selected checkbox was this one, clear it
124-
[self setSelectedCheckBox:nil];
129+
self.selectedCheckBox = nil;
125130
}
126131
}
127132

0 commit comments

Comments
 (0)