Calculator display changes
This commit is contained in:
@@ -10,7 +10,6 @@ import android.graphics.Color;
|
||||
import android.text.Html;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.IConstant;
|
||||
@@ -20,12 +19,9 @@ import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.view.NumeralBaseConverterDialog;
|
||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
||||
import org.solovyev.android.calculator.view.UnitConverterViewBuilder;
|
||||
import org.solovyev.android.menu.AMenuItem;
|
||||
import org.solovyev.android.menu.LabeledMenuItem;
|
||||
import org.solovyev.android.view.AutoResizeTextView;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -37,95 +33,18 @@ import java.util.Set;
|
||||
*/
|
||||
public class AndroidCalculatorDisplayView extends AutoResizeTextView implements CalculatorDisplayView {
|
||||
|
||||
private static enum ConversionMenuItem implements AMenuItem<CalculatorDisplayView> {
|
||||
convert_to_bin(NumeralBase.bin),
|
||||
convert_to_dec(NumeralBase.dec),
|
||||
convert_to_hex(NumeralBase.hex);
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase toNumeralBase;
|
||||
|
||||
private ConversionMenuItem(@NotNull NumeralBase toNumeralBase) {
|
||||
this.toNumeralBase = toNumeralBase;
|
||||
}
|
||||
|
||||
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
|
||||
boolean result = false;
|
||||
|
||||
if (operation == JsclOperation.numeric) {
|
||||
if (generic.getConstants().isEmpty()) {
|
||||
try {
|
||||
convert(generic);
|
||||
|
||||
// conversion possible => return true
|
||||
result = true;
|
||||
|
||||
} catch (UnitConverterViewBuilder.ConversionException e) {
|
||||
// conversion is not possible => return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
final NumeralBase fromNumeralBase = CalculatorEngine.instance.getEngine().getNumeralBase();
|
||||
|
||||
final Generic lastResult = CalculatorLocatorImpl.getInstance().getCalculatorDisplay().getViewState().getResult();
|
||||
|
||||
if (lastResult != null) {
|
||||
String to;
|
||||
try {
|
||||
to = convert(lastResult);
|
||||
|
||||
// add prefix
|
||||
if (fromNumeralBase != toNumeralBase) {
|
||||
to = toNumeralBase.getJsclPrefix() + to;
|
||||
}
|
||||
} catch (UnitConverterViewBuilder.ConversionException e) {
|
||||
to = context.getString(R.string.c_error);
|
||||
}
|
||||
|
||||
data.setText(to);
|
||||
//data.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String convert(@NotNull Generic generic) throws UnitConverterViewBuilder.ConversionException {
|
||||
final NumeralBase fromNumeralBase = CalculatorEngine.instance.getEngine().getNumeralBase();
|
||||
|
||||
if (fromNumeralBase != toNumeralBase) {
|
||||
String from = generic.toString();
|
||||
if (!StringUtils.isEmpty(from)) {
|
||||
try {
|
||||
from = ToJsclTextProcessor.getInstance().process(from).getExpression();
|
||||
} catch (CalculatorParseException e) {
|
||||
// ok, problems while processing occurred
|
||||
}
|
||||
}
|
||||
|
||||
return UnitConverterViewBuilder.doConversion(AndroidNumeralBase.getConverter(), from, AndroidNumeralBase.valueOf(fromNumeralBase), AndroidNumeralBase.valueOf(toNumeralBase));
|
||||
} else {
|
||||
return generic.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static enum MenuItem implements LabeledMenuItem<CalculatorDisplayView> {
|
||||
public static enum MenuItem implements LabeledMenuItem<CalculatorDisplayViewState> {
|
||||
|
||||
copy(R.string.c_copy) {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
CalculatorModel.copyResult(context, data);
|
||||
}
|
||||
},
|
||||
|
||||
convert_to_bin(R.string.convert_to_bin) {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
ConversionMenuItem.convert_to_bin.onClick(data, context);
|
||||
}
|
||||
|
||||
@@ -137,7 +56,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
|
||||
convert_to_dec(R.string.convert_to_dec) {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
ConversionMenuItem.convert_to_dec.onClick(data, context);
|
||||
}
|
||||
|
||||
@@ -149,7 +68,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
|
||||
convert_to_hex(R.string.convert_to_hex) {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
ConversionMenuItem.convert_to_hex.onClick(data, context);
|
||||
}
|
||||
|
||||
@@ -161,8 +80,8 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
|
||||
convert(R.string.c_convert) {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
final Generic result = data.getState().getResult();
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
final Generic result = data.getResult();
|
||||
if (result != null) {
|
||||
new NumeralBaseConverterDialog(result.toString()).show(context);
|
||||
}
|
||||
@@ -176,8 +95,8 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
|
||||
plot(R.string.c_plot) {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayView data, @NotNull Context context) {
|
||||
final Generic generic = data.getState().getResult();
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
final Generic generic = data.getResult();
|
||||
assert generic != null;
|
||||
|
||||
final Constant constant = CollectionsUtils.getFirstCollectionElement(getNotSystemConstants(generic));
|
||||
|
@@ -97,10 +97,9 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
||||
copyResult(context, display);
|
||||
}
|
||||
|
||||
public static void copyResult(@NotNull Context context, @NotNull final CalculatorDisplayView display) {
|
||||
final CalculatorDisplayViewState displayViewState = display.getState();
|
||||
if (displayViewState.isValid()) {
|
||||
final CharSequence text = display.getText();
|
||||
public static void copyResult(@NotNull Context context, @NotNull final CalculatorDisplayViewState viewState) {
|
||||
if (viewState.isValid()) {
|
||||
final CharSequence text = viewState.getText();
|
||||
if (!StringUtils.isEmpty(text)) {
|
||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||
clipboard.setText(text.toString());
|
||||
|
@@ -0,0 +1,63 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.Context;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
import org.solovyev.android.menu.AMenuItem;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/21/12
|
||||
* Time: 12:11 AM
|
||||
*/
|
||||
enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
|
||||
|
||||
convert_to_bin(NumeralBase.bin),
|
||||
convert_to_dec(NumeralBase.dec),
|
||||
convert_to_hex(NumeralBase.hex);
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase toNumeralBase;
|
||||
|
||||
ConversionMenuItem(@NotNull NumeralBase toNumeralBase) {
|
||||
this.toNumeralBase = toNumeralBase;
|
||||
}
|
||||
|
||||
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
|
||||
boolean result = false;
|
||||
|
||||
if (operation == JsclOperation.numeric) {
|
||||
if (generic.getConstants().isEmpty()) {
|
||||
try {
|
||||
convert(generic);
|
||||
|
||||
// conversion possible => return true
|
||||
result = true;
|
||||
|
||||
} catch (CalculatorImpl.ConversionException e) {
|
||||
// conversion is not possible => return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
final NumeralBase fromNumeralBase = CalculatorEngine.instance.getEngine().getNumeralBase();
|
||||
|
||||
final Generic lastResult = data.getResult();
|
||||
|
||||
if (lastResult != null) {
|
||||
convert(lastResult);
|
||||
}
|
||||
}
|
||||
|
||||
private void convert(@NotNull Generic generic) {
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().convert(generic, this.toNumeralBase);
|
||||
}
|
||||
}
|
@@ -1,259 +1,224 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.text.ClipboardManager;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitConverter;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.math.units.UnitType;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.view.ViewBuilder;
|
||||
import org.solovyev.android.view.ViewFromLayoutBuilder;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/20/12
|
||||
* Time: 4:50 PM
|
||||
*/
|
||||
public class UnitConverterViewBuilder implements ViewBuilder<View> {
|
||||
|
||||
@NotNull
|
||||
private List<? extends UnitType<String>> fromUnitTypes = Collections.emptyList();
|
||||
|
||||
@NotNull
|
||||
private List<? extends UnitType<String>> toUnitTypes = Collections.emptyList();
|
||||
|
||||
@Nullable
|
||||
private Unit<String> fromValue;
|
||||
|
||||
@NotNull
|
||||
private UnitConverter<String> converter = UnitConverter.Dummy.getInstance();
|
||||
|
||||
@Nullable
|
||||
private View.OnClickListener okButtonOnClickListener;
|
||||
|
||||
@Nullable
|
||||
private CustomButtonData customButtonData;
|
||||
|
||||
public void setFromUnitTypes(@NotNull List<? extends UnitType<String>> fromUnitTypes) {
|
||||
this.fromUnitTypes = fromUnitTypes;
|
||||
}
|
||||
|
||||
public void setToUnitTypes(@NotNull List<? extends UnitType<String>> toUnitTypes) {
|
||||
this.toUnitTypes = toUnitTypes;
|
||||
}
|
||||
|
||||
public void setFromValue(@Nullable Unit<String> fromValue) {
|
||||
this.fromValue = fromValue;
|
||||
}
|
||||
|
||||
public void setConverter(@NotNull UnitConverter<String> converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public void setOkButtonOnClickListener(@Nullable View.OnClickListener okButtonOnClickListener) {
|
||||
this.okButtonOnClickListener = okButtonOnClickListener;
|
||||
}
|
||||
|
||||
public void setCustomButtonData(@Nullable CustomButtonData customButtonData) {
|
||||
this.customButtonData = customButtonData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public View build(@NotNull final Context context) {
|
||||
final View main = ViewFromLayoutBuilder.newInstance(R.layout.unit_converter).build(context);
|
||||
|
||||
final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from);
|
||||
final EditText fromEditText = (EditText) main.findViewById(R.id.units_from);
|
||||
fromEditText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
doConversion(main, context, UnitConverterViewBuilder.this.converter);
|
||||
}
|
||||
});
|
||||
|
||||
fillSpinner(main, context, R.id.unit_types_from, fromUnitTypes);
|
||||
fillSpinner(main, context, R.id.unit_types_to, toUnitTypes);
|
||||
|
||||
if (fromValue != null) {
|
||||
fromEditText.setText(fromValue.getValue());
|
||||
|
||||
int i = fromUnitTypes.indexOf(fromValue.getUnitType());
|
||||
if ( i >= 0 ) {
|
||||
fromSpinner.setSelection(i);
|
||||
}
|
||||
}
|
||||
|
||||
final Button copyButton = (Button) main.findViewById(R.id.unit_converter_copy_button);
|
||||
copyButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
|
||||
|
||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||
clipboard.setText(toEditText.getText().toString());
|
||||
Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
|
||||
final Button okButton = (Button) main.findViewById(R.id.unit_converter_ok_button);
|
||||
if ( okButtonOnClickListener == null ) {
|
||||
((ViewGroup) okButton.getParent()).removeView(okButton);
|
||||
} else {
|
||||
okButton.setOnClickListener(this.okButtonOnClickListener);
|
||||
}
|
||||
|
||||
final Button customButton = (Button) main.findViewById(R.id.unit_converter_custom_button);
|
||||
if ( customButtonData == null ) {
|
||||
((ViewGroup) customButton.getParent()).removeView(customButton);
|
||||
} else {
|
||||
customButton.setText(customButtonData.text);
|
||||
customButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
customButtonData.clickListener.onClick(getFromUnit(main), getToUnit(main));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
return main;
|
||||
}
|
||||
|
||||
private void fillSpinner(@NotNull final View main,
|
||||
@NotNull final Context context,
|
||||
final int spinnerId,
|
||||
@NotNull List<? extends UnitType<String>> unitTypes) {
|
||||
final Spinner spinner = (Spinner) main.findViewById(spinnerId);
|
||||
|
||||
final ArrayAdapter<UnitType<String>> adapter = new ArrayAdapter<UnitType<String>>(context, android.R.layout.simple_spinner_item);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
for (UnitType<String> fromUnitType : unitTypes) {
|
||||
adapter.add(fromUnitType);
|
||||
}
|
||||
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
doConversion(main, context, UnitConverterViewBuilder.this.converter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
}
|
||||
});
|
||||
spinner.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private static void doConversion(@NotNull View main, @NotNull Context context, @NotNull UnitConverter<String> converter) {
|
||||
final EditText fromEditText = (EditText) main.findViewById(R.id.units_from);
|
||||
|
||||
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
|
||||
|
||||
final String from = fromEditText.getText().toString();
|
||||
try {
|
||||
toEditText.setText(doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
|
||||
} catch (ConversionException e) {
|
||||
toEditText.setText(context.getString(R.string.c_error));
|
||||
}
|
||||
}
|
||||
|
||||
public static final class ConversionException extends Exception {
|
||||
private ConversionException() {
|
||||
}
|
||||
|
||||
private ConversionException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String doConversion(@NotNull UnitConverter<String> converter,
|
||||
@Nullable String from,
|
||||
@NotNull UnitType<String> fromUnitType,
|
||||
@NotNull UnitType<String> toUnitType) throws ConversionException{
|
||||
final String result;
|
||||
|
||||
if (StringUtils.isEmpty(from)) {
|
||||
result = "";
|
||||
} else {
|
||||
|
||||
String to = null;
|
||||
try {
|
||||
if (converter.isSupported(fromUnitType, toUnitType)) {
|
||||
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
|
||||
result = to;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Unit<String> getToUnit(@NotNull View main) {
|
||||
final EditText toUnits = (EditText) main.findViewById(R.id.units_to);
|
||||
return UnitImpl.newInstance(toUnits.getText().toString(), getToUnitType(main));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static UnitType<String> getToUnitType(@NotNull View main) {
|
||||
final Spinner toSpinner = (Spinner) main.findViewById(R.id.unit_types_to);
|
||||
return (UnitType<String>) toSpinner.getSelectedItem();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Unit<String> getFromUnit(@NotNull View main) {
|
||||
final EditText fromUnits = (EditText) main.findViewById(R.id.units_from);
|
||||
return UnitImpl.newInstance(fromUnits.getText().toString(), getFromUnitType(main));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static UnitType<String> getFromUnitType(@NotNull View main) {
|
||||
final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from);
|
||||
return (UnitType<String>) fromSpinner.getSelectedItem();
|
||||
}
|
||||
|
||||
public static class CustomButtonData {
|
||||
|
||||
@NotNull
|
||||
private String text;
|
||||
|
||||
@NotNull
|
||||
private CustomButtonOnClickListener clickListener;
|
||||
|
||||
|
||||
public CustomButtonData(@NotNull String text, @NotNull CustomButtonOnClickListener clickListener) {
|
||||
this.text = text;
|
||||
this.clickListener = clickListener;
|
||||
}
|
||||
}
|
||||
|
||||
public static interface CustomButtonOnClickListener {
|
||||
void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits);
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.text.ClipboardManager;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.CalculatorImpl;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitConverter;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.math.units.UnitType;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.view.ViewBuilder;
|
||||
import org.solovyev.android.view.ViewFromLayoutBuilder;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/20/12
|
||||
* Time: 4:50 PM
|
||||
*/
|
||||
public class UnitConverterViewBuilder implements ViewBuilder<View> {
|
||||
|
||||
@NotNull
|
||||
private List<? extends UnitType<String>> fromUnitTypes = Collections.emptyList();
|
||||
|
||||
@NotNull
|
||||
private List<? extends UnitType<String>> toUnitTypes = Collections.emptyList();
|
||||
|
||||
@Nullable
|
||||
private Unit<String> fromValue;
|
||||
|
||||
@NotNull
|
||||
private UnitConverter<String> converter = UnitConverter.Dummy.getInstance();
|
||||
|
||||
@Nullable
|
||||
private View.OnClickListener okButtonOnClickListener;
|
||||
|
||||
@Nullable
|
||||
private CustomButtonData customButtonData;
|
||||
|
||||
public void setFromUnitTypes(@NotNull List<? extends UnitType<String>> fromUnitTypes) {
|
||||
this.fromUnitTypes = fromUnitTypes;
|
||||
}
|
||||
|
||||
public void setToUnitTypes(@NotNull List<? extends UnitType<String>> toUnitTypes) {
|
||||
this.toUnitTypes = toUnitTypes;
|
||||
}
|
||||
|
||||
public void setFromValue(@Nullable Unit<String> fromValue) {
|
||||
this.fromValue = fromValue;
|
||||
}
|
||||
|
||||
public void setConverter(@NotNull UnitConverter<String> converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public void setOkButtonOnClickListener(@Nullable View.OnClickListener okButtonOnClickListener) {
|
||||
this.okButtonOnClickListener = okButtonOnClickListener;
|
||||
}
|
||||
|
||||
public void setCustomButtonData(@Nullable CustomButtonData customButtonData) {
|
||||
this.customButtonData = customButtonData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public View build(@NotNull final Context context) {
|
||||
final View main = ViewFromLayoutBuilder.newInstance(R.layout.unit_converter).build(context);
|
||||
|
||||
final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from);
|
||||
final EditText fromEditText = (EditText) main.findViewById(R.id.units_from);
|
||||
fromEditText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
doConversion(main, context, UnitConverterViewBuilder.this.converter);
|
||||
}
|
||||
});
|
||||
|
||||
fillSpinner(main, context, R.id.unit_types_from, fromUnitTypes);
|
||||
fillSpinner(main, context, R.id.unit_types_to, toUnitTypes);
|
||||
|
||||
if (fromValue != null) {
|
||||
fromEditText.setText(fromValue.getValue());
|
||||
|
||||
int i = fromUnitTypes.indexOf(fromValue.getUnitType());
|
||||
if ( i >= 0 ) {
|
||||
fromSpinner.setSelection(i);
|
||||
}
|
||||
}
|
||||
|
||||
final Button copyButton = (Button) main.findViewById(R.id.unit_converter_copy_button);
|
||||
copyButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
|
||||
|
||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||
clipboard.setText(toEditText.getText().toString());
|
||||
Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
|
||||
final Button okButton = (Button) main.findViewById(R.id.unit_converter_ok_button);
|
||||
if ( okButtonOnClickListener == null ) {
|
||||
((ViewGroup) okButton.getParent()).removeView(okButton);
|
||||
} else {
|
||||
okButton.setOnClickListener(this.okButtonOnClickListener);
|
||||
}
|
||||
|
||||
final Button customButton = (Button) main.findViewById(R.id.unit_converter_custom_button);
|
||||
if ( customButtonData == null ) {
|
||||
((ViewGroup) customButton.getParent()).removeView(customButton);
|
||||
} else {
|
||||
customButton.setText(customButtonData.text);
|
||||
customButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
customButtonData.clickListener.onClick(getFromUnit(main), getToUnit(main));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
return main;
|
||||
}
|
||||
|
||||
private void fillSpinner(@NotNull final View main,
|
||||
@NotNull final Context context,
|
||||
final int spinnerId,
|
||||
@NotNull List<? extends UnitType<String>> unitTypes) {
|
||||
final Spinner spinner = (Spinner) main.findViewById(spinnerId);
|
||||
|
||||
final ArrayAdapter<UnitType<String>> adapter = new ArrayAdapter<UnitType<String>>(context, android.R.layout.simple_spinner_item);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
for (UnitType<String> fromUnitType : unitTypes) {
|
||||
adapter.add(fromUnitType);
|
||||
}
|
||||
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
doConversion(main, context, UnitConverterViewBuilder.this.converter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
}
|
||||
});
|
||||
spinner.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private static void doConversion(@NotNull View main, @NotNull Context context, @NotNull UnitConverter<String> converter) {
|
||||
final EditText fromEditText = (EditText) main.findViewById(R.id.units_from);
|
||||
|
||||
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
|
||||
|
||||
final String from = fromEditText.getText().toString();
|
||||
try {
|
||||
toEditText.setText(CalculatorImpl.doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
|
||||
} catch (CalculatorImpl.ConversionException e) {
|
||||
toEditText.setText(context.getString(R.string.c_error));
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Unit<String> getToUnit(@NotNull View main) {
|
||||
final EditText toUnits = (EditText) main.findViewById(R.id.units_to);
|
||||
return UnitImpl.newInstance(toUnits.getText().toString(), getToUnitType(main));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static UnitType<String> getToUnitType(@NotNull View main) {
|
||||
final Spinner toSpinner = (Spinner) main.findViewById(R.id.unit_types_to);
|
||||
return (UnitType<String>) toSpinner.getSelectedItem();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Unit<String> getFromUnit(@NotNull View main) {
|
||||
final EditText fromUnits = (EditText) main.findViewById(R.id.units_from);
|
||||
return UnitImpl.newInstance(fromUnits.getText().toString(), getFromUnitType(main));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static UnitType<String> getFromUnitType(@NotNull View main) {
|
||||
final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from);
|
||||
return (UnitType<String>) fromSpinner.getSelectedItem();
|
||||
}
|
||||
|
||||
public static class CustomButtonData {
|
||||
|
||||
@NotNull
|
||||
private String text;
|
||||
|
||||
@NotNull
|
||||
private CustomButtonOnClickListener clickListener;
|
||||
|
||||
|
||||
public CustomButtonData(@NotNull String text, @NotNull CustomButtonOnClickListener clickListener) {
|
||||
this.text = text;
|
||||
this.clickListener = clickListener;
|
||||
}
|
||||
}
|
||||
|
||||
public static interface CustomButtonOnClickListener {
|
||||
void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user