Skip to content

Commit

Permalink
[Slider] Move static inner classes outside base slider class
Browse files Browse the repository at this point in the history
Resolves #1342

Resolves #1471

PiperOrigin-RevId: 320607441
(cherry picked from commit 34f621d)
  • Loading branch information
ymarian authored and hunterstich committed Jul 15, 2020
1 parent 2664ee9 commit a1e0883
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package io.material.catalog.slider;

import io.material.catalog.R;

import android.os.Bundle;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import com.google.android.material.slider.BasicLabelFormatter;
import com.google.android.material.slider.LabelFormatter;
import com.google.android.material.slider.Slider;
import io.material.catalog.R;
import io.material.catalog.feature.DemoFragment;

/**
Expand All @@ -44,14 +45,14 @@ public View onCreateDemoView(
setUpSlider(view, R.id.switch_button_1, R.id.slider_1, null);
setUpSlider(view, R.id.switch_button_2, R.id.slider_2, null);
setUpSlider(view, R.id.switch_button_3, R.id.slider_3, null);
setUpSlider(view, R.id.switch_button_4, R.id.slider_4, new Slider.BasicLabelFormatter());
setUpSlider(view, R.id.switch_button_4, R.id.slider_4, new BasicLabelFormatter());
setUpSlider(view, R.id.switch_button_5, R.id.slider_5, null);

return view;
}

private void setUpSlider(
View view, @IdRes int switchId, @IdRes int sliderId, Slider.LabelFormatter labelFormatter) {
View view, @IdRes int switchId, @IdRes int sliderId, LabelFormatter labelFormatter) {
final Slider slider = view.findViewById(sliderId);
slider.setLabelFormatter(labelFormatter);
SwitchCompat switchButton = view.findViewById(switchId);
Expand Down
47 changes: 4 additions & 43 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.RangeInfoCompat.RANGE_TYPE_FLOAT;
import static androidx.core.math.MathUtils.clamp;
import static com.google.android.material.slider.LabelFormatter.LABEL_FLOATING;
import static com.google.android.material.slider.LabelFormatter.LABEL_GONE;
import static com.google.android.material.slider.LabelFormatter.LABEL_WITHIN_BOUNDS;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
import static java.lang.Float.compare;
import static java.lang.Math.abs;
Expand Down Expand Up @@ -83,7 +86,6 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

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

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

public static final int LABEL_FLOATING = 0;
public static final int LABEL_WITHIN_BOUNDS = 1;
public static final int LABEL_GONE = 2;
private float touchPosition;

/**
Expand All @@ -272,45 +271,7 @@ private interface TooltipDrawableFactory {
*/
@IntDef({LABEL_FLOATING, LABEL_WITHIN_BOUNDS, LABEL_GONE})
@Retention(RetentionPolicy.SOURCE)
public @interface LabelBehavior {}

/**
* Interface definition for applying custom formatting to the text displayed inside the bubble
* shown when a slider is used in discrete mode.
*/
public interface LabelFormatter {
@NonNull
String getFormattedValue(float value);
}

/**
* A simple implementation of the {@link LabelFormatter} interface, that limits the number
* displayed inside a discrete slider's bubble to three digits, and a single-character suffix that
* denotes magnitude (e.g.: 1.5K, 2.2M, 1.3B, 2T).
*/
public static final class BasicLabelFormatter implements LabelFormatter {

private static final long TRILLION = 1000000000000L;
private static final int BILLION = 1000000000;
private static final int MILLION = 1000000;
private static final int THOUSAND = 1000;

@NonNull
@Override
public String getFormattedValue(float value) {
if (value >= TRILLION) {
return String.format(Locale.US, "%.1fT", value / TRILLION);
} else if (value >= BILLION) {
return String.format(Locale.US, "%.1fB", value / BILLION);
} else if (value >= MILLION) {
return String.format(Locale.US, "%.1fM", value / MILLION);
} else if (value >= THOUSAND) {
return String.format(Locale.US, "%.1fK", value / THOUSAND);
}

return String.format(Locale.US, "%.0f", value);
}
}
@interface LabelBehavior {}

public BaseSlider(@NonNull Context context) {
this(context, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.material.slider;

import androidx.annotation.NonNull;
import java.util.Locale;

/**
* A simple implementation of the {@link LabelFormatter} interface, that limits the number
* displayed inside a discrete slider's bubble to three digits, and a single-character suffix that
* denotes magnitude (e.g.: 1.5K, 2.2M, 1.3B, 2T).
*/
public final class BasicLabelFormatter implements LabelFormatter {

private static final long TRILLION = 1000000000000L;
private static final int BILLION = 1000000000;
private static final int MILLION = 1000000;
private static final int THOUSAND = 1000;

@NonNull
@Override
public String getFormattedValue(float value) {
if (value >= TRILLION) {
return String.format(Locale.US, "%.1fT", value / TRILLION);
} else if (value >= BILLION) {
return String.format(Locale.US, "%.1fB", value / BILLION);
} else if (value >= MILLION) {
return String.format(Locale.US, "%.1fM", value / MILLION);
} else if (value >= THOUSAND) {
return String.format(Locale.US, "%.1fK", value / THOUSAND);
}

return String.format(Locale.US, "%.0f", value);
}
}
33 changes: 33 additions & 0 deletions lib/java/com/google/android/material/slider/LabelFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.material.slider;

import androidx.annotation.NonNull;

/**
* Interface definition for applying custom formatting to the text displayed inside the bubble
* shown when a slider is used in discrete mode.
*/
public interface LabelFormatter {

int LABEL_FLOATING = 0;
int LABEL_WITHIN_BOUNDS = 1;
int LABEL_GONE = 2;

@NonNull
String getFormattedValue(float value);
}

0 comments on commit a1e0883

Please sign in to comment.