Skip to content

Commit 0812d00

Browse files
committed
fix: add refreshThumb to refresh every time thumbSize, thumbImage or thumbImage change. Remove comments via fb
1 parent 31950cf commit 0812d00

File tree

7 files changed

+53
-23
lines changed

7 files changed

+53
-23
lines changed

package/android/src/main/java/com/reactnativecommunity/slider/ReactSlider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,19 @@ public BitmapDrawable call() {
313313

314314
public void setThumbImage(@Nullable final String uri) {
315315
mThumbImageUri = uri;
316-
updateThumbImage();
316+
refreshThumb();
317317
}
318318

319319
public void setThumbSize(final double size) {
320320
float density = getResources().getDisplayMetrics().density;
321321
mThumbSizePx = size > 0 ? Math.round((float) size * density) : 0;
322-
updateThumbImage();
322+
refreshThumb();
323323
}
324324

325325
public void setThumbTintColor(@Nullable final Integer color) {
326326
mThumbTintColor = color;
327327
if (mThumbImageUri != null || mThumbSizePx > 0) {
328-
updateThumbImage();
328+
refreshThumb();
329329
} else {
330330
applyThumbTintColorFilter();
331331
}
@@ -343,7 +343,7 @@ private void applyThumbTintColorFilter() {
343343
}
344344
}
345345

346-
private void updateThumbImage() {
346+
private void refreshThumb() {
347347
if (mThumbImageUri != null) {
348348
BitmapDrawable drawable = getBitmapDrawable(mThumbImageUri);
349349
if (drawable != null) {

package/ios/RNCSlider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626

2727
- (float) discreteValue:(float)value;
2828
- (void) setDisabled:(bool)disabled;
29-
- (void) updateThumbImage;
29+
- (void) refreshThumb;
3030

3131
@end

package/ios/RNCSlider.m

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ @implementation RNCSlider
77
bool _maximumTrackImageSet;
88
UIImage *_thumbImage;
99
CGFloat _thumbSize;
10+
UIColor *_thumbTintColor;
1011
}
1112

1213
- (instancetype)init {
@@ -119,7 +120,7 @@ - (UIImage *)maximumTrackImage
119120
- (void)setThumbImage:(UIImage *)thumbImage
120121
{
121122
_thumbImage = thumbImage;
122-
[self updateThumbImage];
123+
[self refreshThumb];
123124
}
124125

125126
- (UIImage *)thumbImage
@@ -130,11 +131,26 @@ - (UIImage *)thumbImage
130131
- (void)setThumbSize:(CGFloat)thumbSize
131132
{
132133
_thumbSize = thumbSize;
133-
[self updateThumbImage];
134+
[self refreshThumb];
134135
}
135136

136-
- (void)updateThumbImage
137+
- (void)setThumbTintColor:(UIColor *)thumbTintColor
137138
{
139+
_thumbTintColor = thumbTintColor;
140+
[super setThumbTintColor:thumbTintColor];
141+
142+
[self refreshThumb];
143+
}
144+
145+
- (void)refreshThumb
146+
{
147+
if (![NSThread isMainThread]) {
148+
dispatch_async(dispatch_get_main_queue(), ^{
149+
[self refreshThumb];
150+
});
151+
return;
152+
}
153+
138154
UIImage *imageToSet = nil;
139155

140156
if (_thumbSize > 0) {
@@ -145,7 +161,7 @@ - (void)updateThumbImage
145161
if (_thumbImage) {
146162
[_thumbImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
147163
} else {
148-
UIColor *fillColor = self.thumbTintColor ?: [UIColor whiteColor];
164+
UIColor *fillColor = _thumbTintColor ?: self.thumbTintColor ?: [UIColor whiteColor];
149165
CGContextSetFillColorWithColor(context, fillColor.CGColor);
150166
CGContextFillEllipseInRect(context, CGRectMake(0, 0, newSize.width, newSize.height));
151167

@@ -167,6 +183,34 @@ - (void)updateThumbImage
167183
[self setThumbImage:imageToSet forState:UIControlStateHighlighted];
168184
[self setThumbImage:imageToSet forState:UIControlStateSelected];
169185
}
186+
187+
[UIView performWithoutAnimation:^{
188+
float currentValue = super.value;
189+
float minimumValue = super.minimumValue;
190+
float maximumValue = super.maximumValue;
191+
192+
float eps = (maximumValue - minimumValue) / 1000.0f;
193+
if (eps <= 0) {
194+
eps = 0.0001f;
195+
}
196+
197+
float nudgedValue = currentValue + eps;
198+
if (nudgedValue > maximumValue) {
199+
nudgedValue = currentValue - eps;
200+
}
201+
if (nudgedValue < minimumValue) {
202+
nudgedValue = minimumValue;
203+
}
204+
205+
if (nudgedValue != currentValue) {
206+
[super setValue:nudgedValue animated:NO];
207+
}
208+
[super setValue:currentValue animated:NO];
209+
210+
[self setNeedsLayout];
211+
[self layoutSubviews];
212+
[self layoutIfNeeded];
213+
}];
170214
}
171215

172216
- (void)setInverted:(BOOL)inverted

package/ios/RNCSliderComponentView.mm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,6 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
239239
[self->slider setThumbImage:nil];
240240
}];
241241
}
242-
if (oldScreenProps.thumbTintColor != newScreenProps.thumbTintColor && slider.thumbSize > 0) {
243-
[slider updateThumbImage];
244-
}
245242
if (oldScreenProps.trackImage != newScreenProps.trackImage) {
246243
[self loadImageFromImageSource:newScreenProps.trackImage completionBlock:^(NSError *error, UIImage *image) {
247244
dispatch_async(dispatch_get_main_queue(), ^{

package/src/RNCSliderNativeComponent.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ export interface NativeProps extends ViewProps {
3434
testID?: string;
3535
thumbImage?: ImageSource;
3636
thumbTintColor?: ColorValue;
37-
/**
38-
* Sets the size (width and height) of the thumb, in points (dp on Android).
39-
* If you also set `thumbImage`, the image will be scaled to this size.
40-
*/
4137
thumbSize?: Double;
4238
trackImage?: ImageSource;
4339
value?: Float;

package/src/Slider.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ const SliderComponent = (
346346
? 'transparent'
347347
: props.thumbTintColor
348348
}
349-
thumbSize={props.thumbSize}
350349
/>
351350
</View>
352351
);

package/typings/index.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ export interface SliderPropsIOS extends ReactNative.ViewProps {
6060
*/
6161
thumbImage?: ReactNative.ImageURISource;
6262

63-
/**
64-
* Sets the size (width and height) of the thumb.
65-
* If `thumbImage` is provided, it will be scaled to this size.
66-
*/
67-
thumbSize?: number;
68-
6963
/**
7064
* Assigns a single image for the track. Only static images
7165
* are supported. The center pixel of the image will be stretched

0 commit comments

Comments
 (0)