Skip to content

Commit

Permalink
Improve button grouping performance
Browse files Browse the repository at this point in the history
Add test case for grouping speed measuring
Fix build warning
  • Loading branch information
DavydLiu committed Jan 16, 2016
1 parent 4cce857 commit 0147ae7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 25 deletions.
4 changes: 2 additions & 2 deletions DLRadioButton.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DLRadioButton",
"version": "1.4.4",
"version": "1.4.5",
"summary": "A highly customizable Radio Button for iOS",
"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",
"homepage": "https://github.com/DavydLiu/DLRadioButton",
Expand All @@ -17,7 +17,7 @@
},
"source": {
"git": "https://github.com/DavydLiu/DLRadioButton.git",
"tag": "1.4.4"
"tag": "1.4.5"
},
"source_files": "DLRadioButton/**/*.{h,m}",
"exclude_files": "Classes/Exclude",
Expand Down
43 changes: 23 additions & 20 deletions DLRadioButton/DLRadioButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
static const CGFloat kDefaulIconSize = 15.0;
static const CGFloat kDefaultMarginWidth = 5.0;
static NSString *const kGeneratedIconName = @"Generated Icon";

@interface DLRadioButton()

@property BOOL isChaining;

@end
static BOOL _groupModifing = NO;

@implementation DLRadioButton

- (void)setOtherButtons:(NSArray *)otherButtons {
if (!self.isChaining) {
_otherButtons = otherButtons;
self.isChaining = YES;
for (DLRadioButton *radioButton in self.otherButtons) {
NSMutableArray *otherButtons = [[NSMutableArray alloc] initWithArray:self.otherButtons];
[otherButtons addObject:self];
[otherButtons removeObject:radioButton];
[radioButton setOtherButtons:[otherButtons copy]];
if (![DLRadioButton isGroupModifing]) {
[DLRadioButton groupModifing:YES];
for (DLRadioButton *radioButton in otherButtons) {
NSMutableArray *otherButtonsForCurrentButton = [[NSMutableArray alloc] initWithArray:otherButtons];
[otherButtonsForCurrentButton addObject:self];
[otherButtonsForCurrentButton removeObject:radioButton];
[radioButton setOtherButtons:[otherButtonsForCurrentButton copy]];
}
self.isChaining = NO;
[DLRadioButton groupModifing:NO];
}
_otherButtons = otherButtons;
}

- (void)setIcon:(UIImage *)icon {
Expand All @@ -38,14 +33,14 @@ - (void)setIconSelected:(UIImage *)iconSelected {
}

- (void)setMultipleSelectionEnabled:(BOOL)multipleSelectionEnabled {
if (!self.isChaining) {
self.isChaining = YES;
_multipleSelectionEnabled = multipleSelectionEnabled;
if (![DLRadioButton isGroupModifing]) {
[DLRadioButton groupModifing:YES];
for (DLRadioButton *radioButton in self.otherButtons) {
radioButton.multipleSelectionEnabled = multipleSelectionEnabled;
}
self.isChaining = NO;
[DLRadioButton groupModifing:NO];
}
_multipleSelectionEnabled = multipleSelectionEnabled;
}

#pragma mark - Helpers
Expand Down Expand Up @@ -129,6 +124,14 @@ - (void)prepareForInterfaceBuilder {

#pragma mark - DLRadiobutton

+ (void)groupModifing:(BOOL)chaining {
_groupModifing = chaining;
}

+ (BOOL)isGroupModifing {
return _groupModifing;
}

- (void)deselectOtherButtons {
for (UIButton *button in self.otherButtons) {
[button setSelected:NO];
Expand Down Expand Up @@ -217,4 +220,4 @@ - (void)drawRect:(CGRect)rect {
[self drawButton];
}

@end
@end
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
D1BE3A1119A7D263004BD3F5 /* DLAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DLAppDelegate.h; sourceTree = "<group>"; };
D1BE3A1219A7D263004BD3F5 /* DLAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DLAppDelegate.m; sourceTree = "<group>"; };
D1BE3A1A19A7D264004BD3F5 /* DLRadioButtonExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DLRadioButtonExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D1BE3A1B19A7D264004BD3F5 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
D1BE3A1B19A7D264004BD3F5 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
D1BE3A2319A7D264004BD3F5 /* DLRadioButtonExampleTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "DLRadioButtonExampleTests-Info.plist"; sourceTree = "<group>"; };
D1BE3A2519A7D264004BD3F5 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
D1BE3A2719A7D264004BD3F5 /* DLRadioButtonExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DLRadioButtonExampleTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -430,7 +430,6 @@
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/DLRadioButtonExample.app/DLRadioButtonExample";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
"$(DEVELOPER_FRAMEWORKS_DIR)",
);
Expand All @@ -453,7 +452,6 @@
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/DLRadioButtonExample.app/DLRadioButtonExample";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
"$(DEVELOPER_FRAMEWORKS_DIR)",
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>classNames</key>
<dict>
<key>DLRadioButtonExampleTests</key>
<dict>
<key>testButtonsChainingPerformance</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.06</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
</dict>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>runDestinationsByUUID</key>
<dict>
<key>2F747D6B-3143-4BAE-88E8-DE582D17B47F</key>
<dict>
<key>localComputer</key>
<dict>
<key>busSpeedInMHz</key>
<integer>100</integer>
<key>cpuCount</key>
<integer>1</integer>
<key>cpuKind</key>
<string>Intel Core i7</string>
<key>cpuSpeedInMHz</key>
<integer>3000</integer>
<key>logicalCPUCoresPerPackage</key>
<integer>4</integer>
<key>modelCode</key>
<string>Macmini7,1</string>
<key>physicalCPUCoresPerPackage</key>
<integer>2</integer>
<key>platformIdentifier</key>
<string>com.apple.platform.macosx</string>
</dict>
<key>targetArchitecture</key>
<string>x86_64</string>
<key>targetDevice</key>
<dict>
<key>modelCode</key>
<string>iPhone8,1</string>
<key>platformIdentifier</key>
<string>com.apple.platform.iphonesimulator</string>
</dict>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ - (void)testMultipleButtonsSelection {
XCTAssertFalse([[self.secondButton selectedButtons] containsObject:self.thirdButton]);
}

- (void)testButtonsGroupModifingPerformance {
NSMutableArray *otherButtons = [[NSMutableArray alloc] initWithCapacity:999];
for (int i = 0; i < 1000; i++) {
DLRadioButton *radioButton = [[DLRadioButton alloc] init];
[otherButtons addObject:radioButton];
}
[self measureBlock:^{
self.firstButton.otherButtons = otherButtons;
}];
}

@end

0 comments on commit 0147ae7

Please sign in to comment.