Fixes + Fast conversion from menu
This commit is contained in:
parent
315a2eb604
commit
37e9354a5b
@ -10,5 +10,5 @@
|
|||||||
# Project target.
|
# Project target.
|
||||||
target=android-15
|
target=android-15
|
||||||
android.library.reference.1=../calculatorpp-service
|
android.library.reference.1=../calculatorpp-service
|
||||||
android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1.3
|
android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1.4
|
||||||
|
|
||||||
|
@ -51,21 +51,25 @@
|
|||||||
|
|
||||||
<Button a:id="@+id/unit_converter_ok_button"
|
<Button a:id="@+id/unit_converter_ok_button"
|
||||||
a:text="@android:string/ok"
|
a:text="@android:string/ok"
|
||||||
a:paddingLeft="30dp"
|
a:paddingLeft="15dp"
|
||||||
a:paddingRight="30dp"
|
a:paddingRight="15dp"
|
||||||
|
a:singleLine="true"
|
||||||
a:layout_width="wrap_content"
|
a:layout_width="wrap_content"
|
||||||
a:layout_height="wrap_content"/>
|
a:layout_height="wrap_content"/>
|
||||||
|
|
||||||
<Button a:id="@+id/unit_converter_copy_button"
|
<Button a:id="@+id/unit_converter_copy_button"
|
||||||
a:text="@android:string/copy"
|
a:text="@android:string/copy"
|
||||||
a:paddingLeft="30dp"
|
a:paddingLeft="15dp"
|
||||||
a:paddingRight="30dp"
|
a:paddingRight="15dp"
|
||||||
|
a:singleLine="true"
|
||||||
a:layout_width="wrap_content"
|
a:layout_width="wrap_content"
|
||||||
a:layout_height="wrap_content"/>
|
a:layout_height="wrap_content"/>
|
||||||
|
|
||||||
<Button a:id="@+id/unit_converter_custom_button"
|
<Button a:id="@+id/unit_converter_custom_button"
|
||||||
a:paddingLeft="30dp"
|
a:paddingLeft="15dp"
|
||||||
a:paddingRight="30dp"
|
a:paddingRight="15dp"
|
||||||
|
a:ellipsize="end"
|
||||||
|
a:singleLine="true"
|
||||||
a:layout_width="wrap_content"
|
a:layout_width="wrap_content"
|
||||||
a:layout_height="wrap_content"/>
|
a:layout_height="wrap_content"/>
|
||||||
|
|
||||||
|
@ -199,4 +199,8 @@
|
|||||||
|
|
||||||
<string name="c_convert">Перевести в…</string>
|
<string name="c_convert">Перевести в…</string>
|
||||||
|
|
||||||
|
<string name="convert_to_hex">Перевести в hex</string>
|
||||||
|
<string name="convert_to_bin">Перевести в bin</string>
|
||||||
|
<string name="convert_to_dec">Перевести в dec</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@ -199,4 +199,8 @@
|
|||||||
|
|
||||||
<string name="c_convert">Перевести в…</string>
|
<string name="c_convert">Перевести в…</string>
|
||||||
|
|
||||||
|
<string name="convert_to_hex">Перевести в hex</string>
|
||||||
|
<string name="convert_to_bin">Перевести в bin</string>
|
||||||
|
<string name="convert_to_dec">Перевести в dec</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -199,4 +199,8 @@
|
|||||||
|
|
||||||
<string name="c_convert">Convert to…</string>
|
<string name="c_convert">Convert to…</string>
|
||||||
|
|
||||||
|
<string name="convert_to_hex">Convert to hex</string>
|
||||||
|
<string name="convert_to_bin">Convert to bin</string>
|
||||||
|
<string name="convert_to_dec">Convert to dec</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@ -99,6 +99,11 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
|||||||
button.setShowText(show);
|
button.setShowText(show);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public NumeralBase getNumeralBase() {
|
||||||
|
return numeralBase;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Class<String> getUnitValueClass() {
|
public Class<String> getUnitValueClass() {
|
||||||
|
@ -10,6 +10,7 @@ import android.graphics.Color;
|
|||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import jscl.NumeralBase;
|
||||||
import jscl.math.Generic;
|
import jscl.math.Generic;
|
||||||
import jscl.math.function.Constant;
|
import jscl.math.function.Constant;
|
||||||
import jscl.math.function.IConstant;
|
import jscl.math.function.IConstant;
|
||||||
@ -19,11 +20,15 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
|||||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||||
import org.solovyev.android.calculator.model.CalculatorParseException;
|
import org.solovyev.android.calculator.model.CalculatorParseException;
|
||||||
import org.solovyev.android.calculator.model.TextProcessor;
|
import org.solovyev.android.calculator.model.TextProcessor;
|
||||||
|
import org.solovyev.android.calculator.model.ToJsclTextProcessor;
|
||||||
import org.solovyev.android.calculator.view.NumeralBaseConverterDialog;
|
import org.solovyev.android.calculator.view.NumeralBaseConverterDialog;
|
||||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
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.menu.LabeledMenuItem;
|
||||||
import org.solovyev.android.view.AutoResizeTextView;
|
import org.solovyev.android.view.AutoResizeTextView;
|
||||||
import org.solovyev.common.utils.CollectionsUtils;
|
import org.solovyev.common.utils.CollectionsUtils;
|
||||||
|
import org.solovyev.common.utils.StringUtils;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -35,7 +40,80 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{
|
public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{
|
||||||
|
|
||||||
public static enum MenuItem implements LabeledMenuItem<CalculatorDisplay> {
|
private static enum ConversionMenuItem implements AMenuItem<CalculatorDisplay> {
|
||||||
|
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 CalculatorDisplay data, @NotNull Context context) {
|
||||||
|
final NumeralBase fromNumeralBase = CalculatorEngine.instance.getEngine().getNumeralBase();
|
||||||
|
|
||||||
|
String to;
|
||||||
|
try {
|
||||||
|
to = convert(data.getGenericResult());
|
||||||
|
|
||||||
|
// 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<CalculatorDisplay> {
|
||||||
|
|
||||||
copy(R.string.c_copy) {
|
copy(R.string.c_copy) {
|
||||||
@Override
|
@Override
|
||||||
@ -44,6 +122,42 @@ public class CalculatorDisplay extends AutoResizeTextView implements ICalculator
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
convert_to_bin(R.string.convert_to_bin) {
|
||||||
|
@Override
|
||||||
|
public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
|
||||||
|
ConversionMenuItem.convert_to_bin.onClick(data, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
|
||||||
|
return ConversionMenuItem.convert_to_bin.isItemVisibleFor(generic, operation);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
convert_to_dec(R.string.convert_to_dec) {
|
||||||
|
@Override
|
||||||
|
public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
|
||||||
|
ConversionMenuItem.convert_to_dec.onClick(data, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
|
||||||
|
return ConversionMenuItem.convert_to_dec.isItemVisibleFor(generic, operation);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
convert_to_hex(R.string.convert_to_hex) {
|
||||||
|
@Override
|
||||||
|
public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
|
||||||
|
ConversionMenuItem.convert_to_hex.onClick(data, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
|
||||||
|
return ConversionMenuItem.convert_to_hex.isItemVisibleFor(generic, operation);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
convert(R.string.c_convert) {
|
convert(R.string.c_convert) {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
|
public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
|
||||||
|
@ -4,12 +4,11 @@ import android.app.AlertDialog;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import jscl.NumeralBase;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.calculator.AndroidNumeralBase;
|
|
||||||
import org.solovyev.android.Unit;
|
import org.solovyev.android.Unit;
|
||||||
import org.solovyev.android.UnitImpl;
|
import org.solovyev.android.UnitImpl;
|
||||||
|
import org.solovyev.android.calculator.AndroidNumeralBase;
|
||||||
import org.solovyev.android.calculator.CalculatorModel;
|
import org.solovyev.android.calculator.CalculatorModel;
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.android.calculator.R;
|
||||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||||
@ -70,12 +69,7 @@ public class NumeralBaseConverterDialog {
|
|||||||
String toUnitsValue = toUnits.getValue();
|
String toUnitsValue = toUnits.getValue();
|
||||||
|
|
||||||
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
|
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
|
||||||
for (NumeralBase nb : NumeralBase.values()) {
|
toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
|
||||||
if (AndroidNumeralBase.valueOf(nb).equals(toUnits.getUnitType())) {
|
|
||||||
toUnitsValue = nb.getJsclPrefix() + toUnitsValue;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false);
|
CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false);
|
||||||
|
@ -172,11 +172,32 @@ public class UnitConverterViewBuilder implements ViewBuilder<View> {
|
|||||||
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
|
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
|
||||||
|
|
||||||
final String from = fromEditText.getText().toString();
|
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)) {
|
if (StringUtils.isEmpty(from)) {
|
||||||
toEditText.setText("");
|
result = "";
|
||||||
} else {
|
} else {
|
||||||
final UnitType<String> fromUnitType = getFromUnitType(main);
|
|
||||||
final UnitType<String> toUnitType = getToUnitType(main);
|
|
||||||
|
|
||||||
String to = null;
|
String to = null;
|
||||||
try {
|
try {
|
||||||
@ -184,11 +205,13 @@ public class UnitConverterViewBuilder implements ViewBuilder<View> {
|
|||||||
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
|
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
|
||||||
}
|
}
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
to = context.getString(R.string.c_error);
|
throw new ConversionException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
toEditText.setText(to);
|
result = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
2
pom.xml
2
pom.xml
@ -33,7 +33,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.solovyev.android</groupId>
|
<groupId>org.solovyev.android</groupId>
|
||||||
<artifactId>common</artifactId>
|
<artifactId>common</artifactId>
|
||||||
<version>0.1.3</version>
|
<version>0.1.4</version>
|
||||||
<type>apklib</type>
|
<type>apklib</type>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user