All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.android.prefs.RangeSeekBarPreference Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
/*
 * Copyright 2013 serso aka se.solovyev
 *
 * 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
 *
 *    http://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.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Contact details
 *
 * Email: [email protected]
 * Site:  http://se.solovyev.org
 */

package org.solovyev.android.prefs;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import org.solovyev.android.view.AbstractRangeSeekBar;
import org.solovyev.android.view.NumberRangeSeekBar;
import org.solovyev.common.interval.Interval;
import org.solovyev.common.interval.Intervals;
import org.solovyev.common.text.NumberIntervalMapper;

import javax.annotation.Nonnull;

/**
 * User: serso
 * Date: 9/19/11
 * Time: 12:27 PM
 */
public abstract class RangeSeekBarPreference> extends AbstractDialogPreference> implements AbstractRangeSeekBar.OnRangeSeekBarChangeListener {

	@Nonnull
	private final Interval boundaries;

	@Nonnull
	private final T step;

	public RangeSeekBarPreference(@Nonnull Context context, AttributeSet attrs, @Nonnull NumberIntervalMapper mapper) {
		super(context, attrs, null, true, mapper);

		//noinspection ConstantConditions
		boundaries = mapper.parseValue(attrs.getAttributeValue(AbstractDialogPreference.localNameSpace, "boundaries"));

		final String stepValue = attrs.getAttributeValue(AbstractDialogPreference.localNameSpace, "step");
		if (stepValue == null) {
			step = getDefaultStep();
		} else {
			step = mapper.getMapper().parseValue(stepValue);
		}

	}

	@Nonnull
	protected abstract T getDefaultStep();

	@Nonnull
	protected View createPreferenceView(@Nonnull Context context) {
		int count = 0;
		for (T t = boundaries.getLeftLimit(); t.compareTo(boundaries.getRightLimit()) <= 0; t = add(t, step)) {
			count += 1;
		}
		final NumberRangeSeekBar result = new NumberRangeSeekBar(boundaries, count, context);

		result.setNotifyWhileDragging(true);
		result.setOnRangeSeekBarChangeListener(this);

		return result;
	}

	@Nonnull
	protected abstract T add(@Nonnull T l, @Nonnull T r);

	@Override
	protected LinearLayout.LayoutParams getParams() {
		return null;
	}

	@Override
	protected void initPreferenceView(@Nonnull View v, Interval value) {
		if (value != null) {
			((NumberRangeSeekBar) v).setSelectedMinValue(value.getLeftLimit());
			((NumberRangeSeekBar) v).setSelectedMaxValue(value.getRightLimit());
			setValueText(value);
		}
	}

	@Override
	public void rangeSeekBarValuesChanged(T minValue, T maxValue, boolean changeComplete) {
		final Interval interval = Intervals.newClosedInterval(minValue, maxValue);

		if (changeComplete) {
			persistValue(interval);
		}

		setValueText(interval);
	}

	private void setValueText(@Nonnull Interval interval) {
		final String t = String.valueOf(interval);
		final String valueText = getValueText();
		updateValueText(valueText == null ? t : t.concat(valueText));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy