Skip to content

Commit a1e0883

Browse files
ymarianhunterstich
authored andcommitted
[Slider] Move static inner classes outside base slider class
Resolves #1342 Resolves #1471 PiperOrigin-RevId: 320607441 (cherry picked from commit 34f621d)
1 parent 2664ee9 commit a1e0883

File tree

4 files changed

+94
-50
lines changed

4 files changed

+94
-50
lines changed

catalog/java/io/material/catalog/slider/SliderDiscreteDemoFragment.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package io.material.catalog.slider;
1818

19-
import io.material.catalog.R;
20-
2119
import android.os.Bundle;
22-
import androidx.annotation.IdRes;
23-
import androidx.annotation.Nullable;
24-
import androidx.appcompat.widget.SwitchCompat;
2520
import android.view.LayoutInflater;
2621
import android.view.View;
2722
import android.view.ViewGroup;
23+
import androidx.annotation.IdRes;
24+
import androidx.annotation.Nullable;
25+
import androidx.appcompat.widget.SwitchCompat;
26+
import com.google.android.material.slider.BasicLabelFormatter;
27+
import com.google.android.material.slider.LabelFormatter;
2828
import com.google.android.material.slider.Slider;
29+
import io.material.catalog.R;
2930
import io.material.catalog.feature.DemoFragment;
3031

3132
/**
@@ -44,14 +45,14 @@ public View onCreateDemoView(
4445
setUpSlider(view, R.id.switch_button_1, R.id.slider_1, null);
4546
setUpSlider(view, R.id.switch_button_2, R.id.slider_2, null);
4647
setUpSlider(view, R.id.switch_button_3, R.id.slider_3, null);
47-
setUpSlider(view, R.id.switch_button_4, R.id.slider_4, new Slider.BasicLabelFormatter());
48+
setUpSlider(view, R.id.switch_button_4, R.id.slider_4, new BasicLabelFormatter());
4849
setUpSlider(view, R.id.switch_button_5, R.id.slider_5, null);
4950

5051
return view;
5152
}
5253

5354
private void setUpSlider(
54-
View view, @IdRes int switchId, @IdRes int sliderId, Slider.LabelFormatter labelFormatter) {
55+
View view, @IdRes int switchId, @IdRes int sliderId, LabelFormatter labelFormatter) {
5556
final Slider slider = view.findViewById(sliderId);
5657
slider.setLabelFormatter(labelFormatter);
5758
SwitchCompat switchButton = view.findViewById(switchId);

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.RangeInfoCompat.RANGE_TYPE_FLOAT;
2222
import static androidx.core.math.MathUtils.clamp;
23+
import static com.google.android.material.slider.LabelFormatter.LABEL_FLOATING;
24+
import static com.google.android.material.slider.LabelFormatter.LABEL_GONE;
25+
import static com.google.android.material.slider.LabelFormatter.LABEL_WITHIN_BOUNDS;
2326
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
2427
import static java.lang.Float.compare;
2528
import static java.lang.Math.abs;
@@ -83,7 +86,6 @@
8386
import java.util.Collections;
8487
import java.util.Iterator;
8588
import java.util.List;
86-
import java.util.Locale;
8789

8890
/**
8991
* The slider can function either as a continuous slider, or as a discrete slider. The mode of
@@ -252,9 +254,6 @@ private interface TooltipDrawableFactory {
252254

253255
@NonNull private final MaterialShapeDrawable thumbDrawable = new MaterialShapeDrawable();
254256

255-
public static final int LABEL_FLOATING = 0;
256-
public static final int LABEL_WITHIN_BOUNDS = 1;
257-
public static final int LABEL_GONE = 2;
258257
private float touchPosition;
259258

260259
/**
@@ -272,45 +271,7 @@ private interface TooltipDrawableFactory {
272271
*/
273272
@IntDef({LABEL_FLOATING, LABEL_WITHIN_BOUNDS, LABEL_GONE})
274273
@Retention(RetentionPolicy.SOURCE)
275-
public @interface LabelBehavior {}
276-
277-
/**
278-
* Interface definition for applying custom formatting to the text displayed inside the bubble
279-
* shown when a slider is used in discrete mode.
280-
*/
281-
public interface LabelFormatter {
282-
@NonNull
283-
String getFormattedValue(float value);
284-
}
285-
286-
/**
287-
* A simple implementation of the {@link LabelFormatter} interface, that limits the number
288-
* displayed inside a discrete slider's bubble to three digits, and a single-character suffix that
289-
* denotes magnitude (e.g.: 1.5K, 2.2M, 1.3B, 2T).
290-
*/
291-
public static final class BasicLabelFormatter implements LabelFormatter {
292-
293-
private static final long TRILLION = 1000000000000L;
294-
private static final int BILLION = 1000000000;
295-
private static final int MILLION = 1000000;
296-
private static final int THOUSAND = 1000;
297-
298-
@NonNull
299-
@Override
300-
public String getFormattedValue(float value) {
301-
if (value >= TRILLION) {
302-
return String.format(Locale.US, "%.1fT", value / TRILLION);
303-
} else if (value >= BILLION) {
304-
return String.format(Locale.US, "%.1fB", value / BILLION);
305-
} else if (value >= MILLION) {
306-
return String.format(Locale.US, "%.1fM", value / MILLION);
307-
} else if (value >= THOUSAND) {
308-
return String.format(Locale.US, "%.1fK", value / THOUSAND);
309-
}
310-
311-
return String.format(Locale.US, "%.0f", value);
312-
}
313-
}
274+
@interface LabelBehavior {}
314275

315276
public BaseSlider(@NonNull Context context) {
316277
this(context, null);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.slider;
18+
19+
import androidx.annotation.NonNull;
20+
import java.util.Locale;
21+
22+
/**
23+
* A simple implementation of the {@link LabelFormatter} interface, that limits the number
24+
* displayed inside a discrete slider's bubble to three digits, and a single-character suffix that
25+
* denotes magnitude (e.g.: 1.5K, 2.2M, 1.3B, 2T).
26+
*/
27+
public final class BasicLabelFormatter implements LabelFormatter {
28+
29+
private static final long TRILLION = 1000000000000L;
30+
private static final int BILLION = 1000000000;
31+
private static final int MILLION = 1000000;
32+
private static final int THOUSAND = 1000;
33+
34+
@NonNull
35+
@Override
36+
public String getFormattedValue(float value) {
37+
if (value >= TRILLION) {
38+
return String.format(Locale.US, "%.1fT", value / TRILLION);
39+
} else if (value >= BILLION) {
40+
return String.format(Locale.US, "%.1fB", value / BILLION);
41+
} else if (value >= MILLION) {
42+
return String.format(Locale.US, "%.1fM", value / MILLION);
43+
} else if (value >= THOUSAND) {
44+
return String.format(Locale.US, "%.1fK", value / THOUSAND);
45+
}
46+
47+
return String.format(Locale.US, "%.0f", value);
48+
}
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.slider;
18+
19+
import androidx.annotation.NonNull;
20+
21+
/**
22+
* Interface definition for applying custom formatting to the text displayed inside the bubble
23+
* shown when a slider is used in discrete mode.
24+
*/
25+
public interface LabelFormatter {
26+
27+
int LABEL_FLOATING = 0;
28+
int LABEL_WITHIN_BOUNDS = 1;
29+
int LABEL_GONE = 2;
30+
31+
@NonNull
32+
String getFormattedValue(float value);
33+
}

0 commit comments

Comments
 (0)