Conversion
This commit is contained in:
@@ -1,131 +1,136 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.history.HistoryAction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 5:42 PM
|
||||
*/
|
||||
public class AndroidCalculator implements Calculator {
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator = new CalculatorImpl();
|
||||
|
||||
public void init(@NotNull final Activity activity, @NotNull SharedPreferences preferences) {
|
||||
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
|
||||
editorView.init(preferences);
|
||||
preferences.registerOnSharedPreferenceChangeListener(editorView);
|
||||
CalculatorLocatorImpl.getInstance().getEditor().setView(editorView);
|
||||
|
||||
final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay);
|
||||
displayView.setOnClickListener(new CalculatorDisplayOnClickListener(activity));
|
||||
CalculatorLocatorImpl.getInstance().getDisplay().setView(displayView);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* DELEGATED TO CALCULATOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId evaluate(@NotNull JsclOperation operation, @NotNull String expression) {
|
||||
return calculator.evaluate(operation, expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId evaluate(@NotNull JsclOperation operation, @NotNull String expression, @NotNull Long sequenceId) {
|
||||
return calculator.evaluate(operation, expression, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to) {
|
||||
return calculator.convert(generic, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
return calculator.fireCalculatorEvent(calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) {
|
||||
return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.calculator.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculator.addCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculator.removeCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
calculator.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
calculator.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
calculator.doHistoryAction(historyAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) {
|
||||
calculator.setCurrentHistoryState(editorHistoryState);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return calculator.getCurrentHistoryState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
calculator.evaluate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate(@NotNull Long sequenceId) {
|
||||
calculator.evaluate(sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simplify() {
|
||||
calculator.simplify();
|
||||
}
|
||||
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.history.HistoryAction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 5:42 PM
|
||||
*/
|
||||
public class AndroidCalculator implements Calculator {
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator = new CalculatorImpl();
|
||||
|
||||
public void init(@NotNull final Activity activity, @NotNull SharedPreferences preferences) {
|
||||
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
|
||||
editorView.init(preferences);
|
||||
preferences.registerOnSharedPreferenceChangeListener(editorView);
|
||||
CalculatorLocatorImpl.getInstance().getEditor().setView(editorView);
|
||||
|
||||
final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay);
|
||||
displayView.setOnClickListener(new CalculatorDisplayOnClickListener(activity));
|
||||
CalculatorLocatorImpl.getInstance().getDisplay().setView(displayView);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* DELEGATED TO CALCULATOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData evaluate(@NotNull JsclOperation operation, @NotNull String expression) {
|
||||
return calculator.evaluate(operation, expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData evaluate(@NotNull JsclOperation operation, @NotNull String expression, @NotNull Long sequenceId) {
|
||||
return calculator.evaluate(operation, expression, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to) {
|
||||
return calculator.isConversionPossible(generic, from, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to) {
|
||||
return calculator.convert(generic, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
return calculator.fireCalculatorEvent(calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) {
|
||||
return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.calculator.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculator.addCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculator.removeCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
calculator.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
calculator.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
calculator.doHistoryAction(historyAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) {
|
||||
calculator.setCurrentHistoryState(editorHistoryState);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return calculator.getCurrentHistoryState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
calculator.evaluate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate(@NotNull Long sequenceId) {
|
||||
calculator.evaluate(sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simplify() {
|
||||
calculator.simplify();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,14 +3,10 @@ package org.solovyev.android.calculator;
|
||||
import android.app.Activity;
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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.units.CalculatorNumeralBase;
|
||||
import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
import org.solovyev.android.view.drag.DragDirection;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -20,9 +16,9 @@ import java.util.List;
|
||||
* Date: 4/21/12
|
||||
* Time: 8:00 PM
|
||||
*/
|
||||
public enum AndroidNumeralBase implements UnitType<String> {
|
||||
public enum AndroidNumeralBase {
|
||||
|
||||
bin(NumeralBase.bin) {
|
||||
bin(CalculatorNumeralBase.bin) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
@@ -30,7 +26,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
}
|
||||
},
|
||||
|
||||
oct(NumeralBase.oct) {
|
||||
oct(CalculatorNumeralBase.oct) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
@@ -40,7 +36,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
}
|
||||
},
|
||||
|
||||
dec(NumeralBase.dec) {
|
||||
dec(CalculatorNumeralBase.dec) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
@@ -50,7 +46,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
}
|
||||
},
|
||||
|
||||
hex(NumeralBase.hex) {
|
||||
hex(CalculatorNumeralBase.hex) {
|
||||
|
||||
@NotNull
|
||||
private List<Integer> specialHexButtonIds = Arrays.asList(R.id.oneDigitButton, R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton);
|
||||
@@ -72,15 +68,10 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase numeralBase;
|
||||
private final CalculatorNumeralBase calculatorNumeralBase;
|
||||
|
||||
private AndroidNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
this.numeralBase = numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Unit<String> createUnit(@NotNull String value) {
|
||||
return UnitImpl.newInstance(value, this);
|
||||
private AndroidNumeralBase(@NotNull CalculatorNumeralBase calculatorNumeralBase) {
|
||||
this.calculatorNumeralBase = calculatorNumeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -101,54 +92,13 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
|
||||
@NotNull
|
||||
public NumeralBase getNumeralBase() {
|
||||
return numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<String> getUnitValueClass() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final Converter converter = new Converter();
|
||||
|
||||
@NotNull
|
||||
public static Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public static class Converter implements UnitConverter<String> {
|
||||
|
||||
private Converter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
||||
return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.class.isAssignableFrom(to.getClass());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
|
||||
if (!isSupported(from.getUnitType(), toType)) {
|
||||
throw new IllegalArgumentException("Types are not supported!");
|
||||
}
|
||||
|
||||
final AndroidNumeralBase fromTypeAndroid = (AndroidNumeralBase) from.getUnitType();
|
||||
final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase;
|
||||
final NumeralBase toNumeralBase = ((AndroidNumeralBase) toType).numeralBase;
|
||||
final String fromValue = (String) from.getValue();
|
||||
|
||||
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
||||
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (AndroidNumeralBase) toType);
|
||||
}
|
||||
return calculatorNumeralBase.getNumeralBase();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) {
|
||||
for (AndroidNumeralBase androidNumeralBase : values()) {
|
||||
if (androidNumeralBase.numeralBase == nb) {
|
||||
if (androidNumeralBase.calculatorNumeralBase.getNumeralBase() == nb) {
|
||||
return androidNumeralBase;
|
||||
}
|
||||
}
|
||||
|
@@ -1,57 +1,52 @@
|
||||
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.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()) {
|
||||
convert(generic);
|
||||
|
||||
// conversion possible => return true
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
final NumeralBase fromNumeralBase = CalculatorLocatorImpl.getInstance().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);
|
||||
}
|
||||
}
|
||||
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.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()) {
|
||||
// conversion possible => return true
|
||||
final NumeralBase fromNumeralBase = CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase();
|
||||
if (fromNumeralBase != toNumeralBase) {
|
||||
result = CalculatorLocatorImpl.getInstance().getCalculator().isConversionPossible(generic, fromNumeralBase, this.toNumeralBase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
final Generic result = data.getResult();
|
||||
|
||||
if (result != null) {
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().convert(result, this.toNumeralBase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,94 +1,95 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.common.MutableObject;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/22/12
|
||||
* Time: 12:20 AM
|
||||
*/
|
||||
public class NumeralBaseConverterDialog {
|
||||
|
||||
@Nullable
|
||||
private String initialFromValue;
|
||||
|
||||
public NumeralBaseConverterDialog(String initialFromValue) {
|
||||
this.initialFromValue = initialFromValue;
|
||||
}
|
||||
|
||||
public void show(@NotNull Context context) {
|
||||
final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
|
||||
b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||
b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||
|
||||
if (!StringUtils.isEmpty(initialFromValue)) {
|
||||
String value = initialFromValue;
|
||||
try {
|
||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
||||
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
} catch (CalculatorParseException e) {
|
||||
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
}
|
||||
} else {
|
||||
b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
}
|
||||
|
||||
b.setConverter(AndroidNumeralBase.getConverter());
|
||||
|
||||
final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
|
||||
b.setOkButtonOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final AlertDialog alertDialog = alertDialogHolder.getObject();
|
||||
if (alertDialog != null) {
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
b.setCustomButtonData(new UnitConverterViewBuilder.CustomButtonData(context.getString(R.string.c_use_short), new UnitConverterViewBuilder.CustomButtonOnClickListener() {
|
||||
@Override
|
||||
public void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits) {
|
||||
String toUnitsValue = toUnits.getValue();
|
||||
|
||||
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase()))) {
|
||||
toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
|
||||
}
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(toUnitsValue);
|
||||
final AlertDialog alertDialog = alertDialogHolder.getObject();
|
||||
if (alertDialog != null) {
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
|
||||
alertBuilder.setView(b.build(context));
|
||||
alertBuilder.setTitle(R.string.c_conversion_tool);
|
||||
|
||||
final AlertDialog alertDialog = alertBuilder.create();
|
||||
|
||||
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
|
||||
lp.copyFrom(alertDialog.getWindow().getAttributes());
|
||||
|
||||
lp.width = WindowManager.LayoutParams.FILL_PARENT;
|
||||
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||||
|
||||
alertDialogHolder.setObject(alertDialog);
|
||||
alertDialog.show();
|
||||
alertDialog.getWindow().setAttributes(lp);
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.common.MutableObject;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/22/12
|
||||
* Time: 12:20 AM
|
||||
*/
|
||||
public class NumeralBaseConverterDialog {
|
||||
|
||||
@Nullable
|
||||
private String initialFromValue;
|
||||
|
||||
public NumeralBaseConverterDialog(String initialFromValue) {
|
||||
this.initialFromValue = initialFromValue;
|
||||
}
|
||||
|
||||
public void show(@NotNull Context context) {
|
||||
final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
|
||||
b.setFromUnitTypes(Arrays.asList(CalculatorNumeralBase.values()));
|
||||
b.setToUnitTypes(Arrays.asList(CalculatorNumeralBase.values()));
|
||||
|
||||
if (!StringUtils.isEmpty(initialFromValue)) {
|
||||
String value = initialFromValue;
|
||||
try {
|
||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
} catch (CalculatorParseException e) {
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
}
|
||||
} else {
|
||||
b.setFromValue(UnitImpl.newInstance("", CalculatorNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
}
|
||||
|
||||
b.setConverter(CalculatorNumeralBase.getConverter());
|
||||
|
||||
final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
|
||||
b.setOkButtonOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final AlertDialog alertDialog = alertDialogHolder.getObject();
|
||||
if (alertDialog != null) {
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
b.setCustomButtonData(new UnitConverterViewBuilder.CustomButtonData(context.getString(R.string.c_use_short), new UnitConverterViewBuilder.CustomButtonOnClickListener() {
|
||||
@Override
|
||||
public void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits) {
|
||||
String toUnitsValue = toUnits.getValue();
|
||||
|
||||
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase()))) {
|
||||
toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
|
||||
}
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(toUnitsValue);
|
||||
final AlertDialog alertDialog = alertDialogHolder.getObject();
|
||||
if (alertDialog != null) {
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
|
||||
alertBuilder.setView(b.build(context));
|
||||
alertBuilder.setTitle(R.string.c_conversion_tool);
|
||||
|
||||
final AlertDialog alertDialog = alertBuilder.create();
|
||||
|
||||
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
|
||||
lp.copyFrom(alertDialog.getWindow().getAttributes());
|
||||
|
||||
lp.width = WindowManager.LayoutParams.FILL_PARENT;
|
||||
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||||
|
||||
alertDialogHolder.setObject(alertDialog);
|
||||
alertDialog.show();
|
||||
alertDialog.getWindow().setAttributes(lp);
|
||||
}
|
||||
}
|
||||
|
@@ -1,224 +1,221 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
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.*;
|
||||
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(ConversionUtils.doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
|
||||
} catch (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