Skip to content
Permalink
Browse files

[Slider] Add MaterialElevation support (#8127)

Adds conformance to MDCElevatable and MDCElevationOverriding to
MDCSlider.

Closes #8014
  • Loading branch information
codeman7 committed Jul 31, 2019
1 parent 4191d03 commit c6ab74349d0c4dc5b44d69696b3baca4a613276a
@@ -1640,8 +1640,10 @@ Pod::Spec.new do |mdc|
"components/#{component.base_name}/src/private/*.{h,m}"
]

component.dependency "MaterialComponents/Elevation"
component.dependency "MaterialComponents/Palettes"
component.dependency "MaterialComponents/ShadowElevations"
component.dependency "MaterialComponents/private/Math"
component.dependency "MaterialComponents/private/ThumbTrack"

component.test_spec 'UnitTests' do |unit_tests|
@@ -33,7 +33,9 @@ mdc_public_objc_library(
"UIKit",
],
deps = [
"//components/Elevation",
"//components/Palettes",
"//components/private/Math",
"//components/private/ThumbTrack",
],
)
@@ -15,6 +15,7 @@
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>

#import "MaterialElevation.h"
#import "MaterialShadowElevations.h"

@protocol MDCSliderDelegate;
@@ -37,7 +38,7 @@
making the slider a snap to discrete values via @c numberOfDiscreteValues.
*/
IB_DESIGNABLE
@interface MDCSlider : UIControl
@interface MDCSlider : UIControl <MDCElevatable, MDCElevationOverriding>

/** When @c YES, the forState: APIs are enabled. Defaults to @c NO. */
@property(nonatomic, assign, getter=isStatefulAPIEnabled) BOOL statefulAPIEnabled;
@@ -14,6 +14,7 @@

#import "MDCSlider.h"

#import "MaterialMath.h"
#import "MaterialPalettes.h"
#import "MaterialThumbTrack.h"
#import "private/MDCSlider+Private.h"
@@ -44,6 +45,9 @@ @implementation MDCSlider {
NSMutableDictionary *_backgroundTickColorsForState;
}

@synthesize mdc_overrideBaseElevation = _mdc_overrideBaseElevation;
@synthesize mdc_elevationDidChangeBlock = _mdc_elevationDidChangeBlock;

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
@@ -107,6 +111,8 @@ - (void)commonMDCSliderInit {
_backgroundTickColorsForState[@(UIControlStateNormal)] = UIColor.blackColor;
[self addSubview:_thumbTrack];

_mdc_overrideBaseElevation = -1;

if (@available(iOS 10.0, *)) {
_hapticsEnabled = YES;
self.feedbackGenerator =
@@ -250,13 +256,21 @@ - (CGFloat)thumbRadius {
}

- (void)setThumbElevation:(MDCShadowElevation)thumbElevation {
if (MDCCGFloatEqual(_thumbTrack.thumbElevation, thumbElevation)) {
return;
}
_thumbTrack.thumbElevation = thumbElevation;
[self mdc_elevationDidChange];
}

- (MDCShadowElevation)thumbElevation {
return _thumbTrack.thumbElevation;
}

- (CGFloat)mdc_currentElevation {
return self.thumbElevation;
}

- (NSUInteger)numberOfDiscreteValues {
return _thumbTrack.numDiscreteValues;
}
@@ -1268,6 +1268,64 @@ - (void)testTraitCollectionDidChangeBlockCalledWithExpectedParameters {
XCTAssertEqual(passedTraitCollection, fakeTraitCollection);
}

#pragma mark - MaterialElevation

- (void)testDefaultBaseElevationOverrideIsNegative {
// Then
XCTAssertLessThan(self.slider.mdc_overrideBaseElevation, 0);
}

- (void)testSettingOverrideBaseElevationReturnsSetValue {
// Given
CGFloat expectedBaseElevation = 99;

// When
self.slider.mdc_overrideBaseElevation = expectedBaseElevation;

// Then
XCTAssertEqualWithAccuracy(self.slider.mdc_overrideBaseElevation, expectedBaseElevation, 0.001);
}

- (void)testCurrentElevationMatchesElevationWhenElevationChanges {
// When
self.slider.thumbElevation = 77;

// Then
XCTAssertEqualWithAccuracy(self.slider.mdc_currentElevation, self.slider.thumbElevation, 0.001);
}

- (void)testElevationDidChangeBlockCalledWhenElevationChangesValue {
// Given
self.slider.thumbElevation = 5;
__block BOOL blockCalled = NO;
self.slider.mdc_elevationDidChangeBlock =
^(id<MDCElevatable> _Nonnull object, CGFloat absoluteElevation) {
blockCalled = YES;
};

// When
self.slider.thumbElevation = self.slider.thumbElevation + 1;

// Then
XCTAssertTrue(blockCalled);
}

- (void)testElevationDidChangeBlockNotCalledWhenElevationIsSetWithoutChangingValue {
// Given
self.slider.thumbElevation = 5;
__block BOOL blockCalled = NO;
self.slider.mdc_elevationDidChangeBlock =
^(id<MDCElevatable> _Nonnull object, CGFloat absoluteElevation) {
blockCalled = YES;
};

// When
self.slider.thumbElevation = self.slider.thumbElevation;

// Then
XCTAssertFalse(blockCalled);
}

#pragma mark Private test helpers

- (CGFloat)randomNumber {

0 comments on commit c6ab743

Please sign in to comment.
You can’t perform that action at this time.