Skip to content

Commit 0147ae7

Browse files
committed
Improve button grouping performance
Add test case for grouping speed measuring Fix build warning
1 parent 4cce857 commit 0147ae7

File tree

6 files changed

+99
-25
lines changed

6 files changed

+99
-25
lines changed

DLRadioButton.podspec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "DLRadioButton",
3-
"version": "1.4.4",
3+
"version": "1.4.5",
44
"summary": "A highly customizable Radio Button for iOS",
55
"description": "A highly customizable Radio Button for iOS.\n\n* Buttons are drew by UIBezierPath, customize it however you want.\n* You can also use pictures to indicate buttons' selection state.\n",
66
"homepage": "https://github.com/DavydLiu/DLRadioButton",
@@ -17,7 +17,7 @@
1717
},
1818
"source": {
1919
"git": "https://github.com/DavydLiu/DLRadioButton.git",
20-
"tag": "1.4.4"
20+
"tag": "1.4.5"
2121
},
2222
"source_files": "DLRadioButton/**/*.{h,m}",
2323
"exclude_files": "Classes/Exclude",

DLRadioButton/DLRadioButton.m

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,22 @@
33
static const CGFloat kDefaulIconSize = 15.0;
44
static const CGFloat kDefaultMarginWidth = 5.0;
55
static NSString *const kGeneratedIconName = @"Generated Icon";
6-
7-
@interface DLRadioButton()
8-
9-
@property BOOL isChaining;
10-
11-
@end
6+
static BOOL _groupModifing = NO;
127

138
@implementation DLRadioButton
149

1510
- (void)setOtherButtons:(NSArray *)otherButtons {
16-
if (!self.isChaining) {
17-
_otherButtons = otherButtons;
18-
self.isChaining = YES;
19-
for (DLRadioButton *radioButton in self.otherButtons) {
20-
NSMutableArray *otherButtons = [[NSMutableArray alloc] initWithArray:self.otherButtons];
21-
[otherButtons addObject:self];
22-
[otherButtons removeObject:radioButton];
23-
[radioButton setOtherButtons:[otherButtons copy]];
11+
if (![DLRadioButton isGroupModifing]) {
12+
[DLRadioButton groupModifing:YES];
13+
for (DLRadioButton *radioButton in otherButtons) {
14+
NSMutableArray *otherButtonsForCurrentButton = [[NSMutableArray alloc] initWithArray:otherButtons];
15+
[otherButtonsForCurrentButton addObject:self];
16+
[otherButtonsForCurrentButton removeObject:radioButton];
17+
[radioButton setOtherButtons:[otherButtonsForCurrentButton copy]];
2418
}
25-
self.isChaining = NO;
19+
[DLRadioButton groupModifing:NO];
2620
}
21+
_otherButtons = otherButtons;
2722
}
2823

2924
- (void)setIcon:(UIImage *)icon {
@@ -38,14 +33,14 @@ - (void)setIconSelected:(UIImage *)iconSelected {
3833
}
3934

4035
- (void)setMultipleSelectionEnabled:(BOOL)multipleSelectionEnabled {
41-
if (!self.isChaining) {
42-
self.isChaining = YES;
43-
_multipleSelectionEnabled = multipleSelectionEnabled;
36+
if (![DLRadioButton isGroupModifing]) {
37+
[DLRadioButton groupModifing:YES];
4438
for (DLRadioButton *radioButton in self.otherButtons) {
4539
radioButton.multipleSelectionEnabled = multipleSelectionEnabled;
4640
}
47-
self.isChaining = NO;
41+
[DLRadioButton groupModifing:NO];
4842
}
43+
_multipleSelectionEnabled = multipleSelectionEnabled;
4944
}
5045

5146
#pragma mark - Helpers
@@ -129,6 +124,14 @@ - (void)prepareForInterfaceBuilder {
129124

130125
#pragma mark - DLRadiobutton
131126

127+
+ (void)groupModifing:(BOOL)chaining {
128+
_groupModifing = chaining;
129+
}
130+
131+
+ (BOOL)isGroupModifing {
132+
return _groupModifing;
133+
}
134+
132135
- (void)deselectOtherButtons {
133136
for (UIButton *button in self.otherButtons) {
134137
[button setSelected:NO];
@@ -217,4 +220,4 @@ - (void)drawRect:(CGRect)rect {
217220
[self drawButton];
218221
}
219222

220-
@end
223+
@end

DLRadioButtonExample/DLRadioButtonExample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
D1BE3A1119A7D263004BD3F5 /* DLAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DLAppDelegate.h; sourceTree = "<group>"; };
4848
D1BE3A1219A7D263004BD3F5 /* DLAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DLAppDelegate.m; sourceTree = "<group>"; };
4949
D1BE3A1A19A7D264004BD3F5 /* DLRadioButtonExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DLRadioButtonExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
50-
D1BE3A1B19A7D264004BD3F5 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
50+
D1BE3A1B19A7D264004BD3F5 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
5151
D1BE3A2319A7D264004BD3F5 /* DLRadioButtonExampleTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "DLRadioButtonExampleTests-Info.plist"; sourceTree = "<group>"; };
5252
D1BE3A2519A7D264004BD3F5 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
5353
D1BE3A2719A7D264004BD3F5 /* DLRadioButtonExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DLRadioButtonExampleTests.m; sourceTree = "<group>"; };
@@ -430,7 +430,6 @@
430430
buildSettings = {
431431
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/DLRadioButtonExample.app/DLRadioButtonExample";
432432
FRAMEWORK_SEARCH_PATHS = (
433-
"$(SDKROOT)/Developer/Library/Frameworks",
434433
"$(inherited)",
435434
"$(DEVELOPER_FRAMEWORKS_DIR)",
436435
);
@@ -453,7 +452,6 @@
453452
buildSettings = {
454453
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/DLRadioButtonExample.app/DLRadioButtonExample";
455454
FRAMEWORK_SEARCH_PATHS = (
456-
"$(SDKROOT)/Developer/Library/Frameworks",
457455
"$(inherited)",
458456
"$(DEVELOPER_FRAMEWORKS_DIR)",
459457
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>classNames</key>
6+
<dict>
7+
<key>DLRadioButtonExampleTests</key>
8+
<dict>
9+
<key>testButtonsChainingPerformance</key>
10+
<dict>
11+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
12+
<dict>
13+
<key>baselineAverage</key>
14+
<real>0.06</real>
15+
<key>baselineIntegrationDisplayName</key>
16+
<string>Local Baseline</string>
17+
</dict>
18+
</dict>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>runDestinationsByUUID</key>
6+
<dict>
7+
<key>2F747D6B-3143-4BAE-88E8-DE582D17B47F</key>
8+
<dict>
9+
<key>localComputer</key>
10+
<dict>
11+
<key>busSpeedInMHz</key>
12+
<integer>100</integer>
13+
<key>cpuCount</key>
14+
<integer>1</integer>
15+
<key>cpuKind</key>
16+
<string>Intel Core i7</string>
17+
<key>cpuSpeedInMHz</key>
18+
<integer>3000</integer>
19+
<key>logicalCPUCoresPerPackage</key>
20+
<integer>4</integer>
21+
<key>modelCode</key>
22+
<string>Macmini7,1</string>
23+
<key>physicalCPUCoresPerPackage</key>
24+
<integer>2</integer>
25+
<key>platformIdentifier</key>
26+
<string>com.apple.platform.macosx</string>
27+
</dict>
28+
<key>targetArchitecture</key>
29+
<string>x86_64</string>
30+
<key>targetDevice</key>
31+
<dict>
32+
<key>modelCode</key>
33+
<string>iPhone8,1</string>
34+
<key>platformIdentifier</key>
35+
<string>com.apple.platform.iphonesimulator</string>
36+
</dict>
37+
</dict>
38+
</dict>
39+
</dict>
40+
</plist>

DLRadioButtonExample/DLRadioButtonExampleTests/DLRadioButtonExampleTests.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ - (void)testMultipleButtonsSelection {
6969
XCTAssertFalse([[self.secondButton selectedButtons] containsObject:self.thirdButton]);
7070
}
7171

72+
- (void)testButtonsGroupModifingPerformance {
73+
NSMutableArray *otherButtons = [[NSMutableArray alloc] initWithCapacity:999];
74+
for (int i = 0; i < 1000; i++) {
75+
DLRadioButton *radioButton = [[DLRadioButton alloc] init];
76+
[otherButtons addObject:radioButton];
77+
}
78+
[self measureBlock:^{
79+
self.firstButton.otherButtons = otherButtons;
80+
}];
81+
}
82+
7283
@end

0 commit comments

Comments
 (0)