numeral base/angle units button fix

This commit is contained in:
Solovyev_S 2013-07-02 13:23:28 +04:00
parent e0bce886f4
commit 4dae4bbca4
9 changed files with 333 additions and 45 deletions

View File

@ -274,12 +274,12 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen
}
@Nonnull
public NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) {
public static NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) {
return Preferences.numeralBase.getPreference(preferences);
}
@Nonnull
public AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) {
public static AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) {
return Preferences.angleUnit.getPreference(preferences);
}

View File

@ -27,6 +27,7 @@ import android.content.res.Resources;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import jscl.AngleUnit;
import javax.annotation.Nonnull;
@ -41,8 +42,12 @@ import org.solovyev.android.view.drag.DirectionDragButton;
*/
public class AngleUnitsButton extends DirectionDragButton {
@Nonnull
private AngleUnit angleUnit;
public AngleUnitsButton(Context context, @Nonnull AttributeSet attrs) {
super(context, attrs);
this.angleUnit = Locator.getInstance().getEngine().getAngleUnits();
}
@Override
@ -52,11 +57,32 @@ public class AngleUnitsButton extends DirectionDragButton {
super.initDirectionTextPaint(basePaint, directionTextData, resources);
final TextPaint directionTextPaint = directionTextData.getPaint();
if (Locator.getInstance().getEngine().getAngleUnits().name().equals(directionTextData.getText())) {
directionTextPaint.setColor(resources.getColor(R.color.cpp_selected_angle_unit_text_color));
} else {
directionTextPaint.setColor(resources.getColor(R.color.cpp_default_text_color));
final int color = getDirectionTextColor(directionTextData.getText());
directionTextPaint.setColor(color);
if (!isCurrentAngleUnits(directionTextData.getText())) {
directionTextPaint.setAlpha(getDirectionTextAlpha());
}
}
int getDirectionTextColor(@Nonnull String directionText) {
final int color;
final Resources resources = getResources();
if (isCurrentAngleUnits(directionText)) {
color = resources.getColor(R.color.cpp_selected_angle_unit_text_color);
} else {
color = resources.getColor(R.color.cpp_default_text_color);
}
return color;
}
boolean isCurrentAngleUnits(@Nonnull String directionText) {
return this.angleUnit.name().equals(directionText);
}
public void setAngleUnit(@Nonnull AngleUnit angleUnit) {
if (this.angleUnit != angleUnit) {
this.angleUnit = angleUnit;
invalidate();
}
}
}

View File

@ -27,6 +27,7 @@ import android.content.res.Resources;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import jscl.NumeralBase;
import javax.annotation.Nonnull;
@ -41,8 +42,12 @@ import org.solovyev.android.view.drag.DirectionDragButton;
*/
public class NumeralBasesButton extends DirectionDragButton {
@Nonnull
private NumeralBase numeralBase;
public NumeralBasesButton(Context context, @Nonnull AttributeSet attrs) {
super(context, attrs);
this.numeralBase = Locator.getInstance().getEngine().getNumeralBase();
}
@Override
@ -52,11 +57,33 @@ public class NumeralBasesButton extends DirectionDragButton {
super.initDirectionTextPaint(basePaint, directionTextData, resources);
final TextPaint directionTextPaint = directionTextData.getPaint();
if (Locator.getInstance().getEngine().getNumeralBase().name().equals(directionTextData.getText())) {
directionTextPaint.setColor(resources.getColor(R.color.cpp_selected_angle_unit_text_color));
} else {
directionTextPaint.setColor(resources.getColor(R.color.cpp_default_text_color));
final int color = getDirectionTextColor(directionTextData.getText());
directionTextPaint.setColor(color);
if (!isCurrentNumberBase(directionTextData.getText())) {
directionTextPaint.setAlpha(getDirectionTextAlpha());
}
}
int getDirectionTextColor(@Nonnull String directionText) {
final int color;
if (isCurrentNumberBase(directionText)) {
color = getResources().getColor(R.color.cpp_selected_angle_unit_text_color);
} else {
color = getResources().getColor(R.color.cpp_default_text_color);
}
return color;
}
boolean isCurrentNumberBase(@Nonnull String directionText) {
return this.numeralBase.name().equals(directionText);
}
public void setNumeralBase(@Nonnull NumeralBase numeralBase) {
if (this.numeralBase != numeralBase) {
this.numeralBase = numeralBase;
invalidate();
}
}
}

View File

@ -0,0 +1,79 @@
/*
* 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: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator;
import android.content.Context;
import jscl.JsclMathEngine;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.mockito.Mockito;
import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.plot.CalculatorPlotter;
/**
* User: serso
* Date: 10/7/12
* Time: 8:56 PM
*/
public class CalculatorTestUtils {
public static void staticSetUp(@Nullable Context context) throws Exception {
Locator.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class), Mockito.mock(CalculatorPlotter.class), null);
Locator.getInstance().getEngine().init();
if (context != null) {
initViews(context);
}
}
public static void initViews(@Nonnull Context context) {
final AndroidCalculatorEditorView editor = new AndroidCalculatorEditorView(context);
editor.init();
Locator.getInstance().getEditor().setView(editor);
final AndroidCalculatorDisplayView display = new AndroidCalculatorDisplayView(context);
display.init(context);
Locator.getInstance().getDisplay().setView(display);
}
public static void staticSetUp() throws Exception {
staticSetUp(null);
}
@Nonnull
static CalculatorEngineImpl newCalculatorEngine() {
final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class);
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
final CalculatorVarsRegistry varsRegistry = new CalculatorVarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao);
final CalculatorFunctionsMathRegistry functionsRegistry = new CalculatorFunctionsMathRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao);
final CalculatorOperatorsMathRegistry operatorsRegistry = new CalculatorOperatorsMathRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao);
final CalculatorPostfixFunctionsRegistry postfixFunctionsRegistry = new CalculatorPostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);
return new CalculatorEngineImpl(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry, null);
}
}

View File

@ -0,0 +1,75 @@
package org.solovyev.android.calculator.view;
import android.app.Activity;
import jscl.AngleUnit;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.res.Attribute;
import org.robolectric.shadows.ShadowActivity;
import static jscl.AngleUnit.deg;
import static jscl.AngleUnit.grad;
import static jscl.AngleUnit.rad;
import static jscl.AngleUnit.turns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.solovyev.android.calculator.CalculatorTestUtils.staticSetUp;
@RunWith(RobolectricTestRunner.class)
public class AngleUnitsButtonTest {
private AngleUnitsButton button;
@Before
public void setUp() throws Exception {
staticSetUp();
final Activity context = Robolectric.buildActivity(Activity.class).create().get();
final ShadowActivity activity = Robolectric.shadowOf(context);
button = new AngleUnitsButton(context, activity.createAttributeSet(new ArrayList<Attribute>(), AngleUnitsButton.class));
}
@Test
public void testShouldReturnDifferentColorsForDifferentAngleUnits() throws Exception {
button.setAngleUnit(deg);
assertEquals(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(deg.name()));
assertEquals(button.getDirectionTextColor(grad.name()), button.getDirectionTextColor(rad.name()));
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(rad.name()));
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(grad.name()));
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(turns.name()));
}
@Test
public void testIsCurrentAngleUnits() throws Exception {
button.setAngleUnit(rad);
assertTrue(button.isCurrentAngleUnits(rad.name()));
assertFalse(button.isCurrentAngleUnits(deg.name()));
assertFalse(button.isCurrentAngleUnits(grad.name()));
}
@Test
public void testInvalidateShouldBeCalledOnlyWhenChangeIsDone() throws Exception {
button.setAngleUnit(rad);
button = Mockito.spy(button);
button.setAngleUnit(deg);
verify(button, times(1)).invalidate();
button.setAngleUnit(deg);
verify(button, times(1)).invalidate();
}
}

View File

@ -0,0 +1,76 @@
package org.solovyev.android.calculator.view;
import android.app.Activity;
import jscl.NumeralBase;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.res.Attribute;
import org.robolectric.shadows.ShadowActivity;
import static jscl.AngleUnit.deg;
import static jscl.AngleUnit.rad;
import static jscl.NumeralBase.bin;
import static jscl.NumeralBase.dec;
import static jscl.NumeralBase.hex;
import static jscl.NumeralBase.oct;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.solovyev.android.calculator.CalculatorTestUtils.staticSetUp;
@RunWith(RobolectricTestRunner.class)
public class NumeralBasesButtonTest {
private NumeralBasesButton button;
@Before
public void setUp() throws Exception {
staticSetUp();
final Activity context = Robolectric.buildActivity(Activity.class).create().get();
final ShadowActivity activity = Robolectric.shadowOf(context);
button = new NumeralBasesButton(context, activity.createAttributeSet(new ArrayList<Attribute>(), NumeralBasesButton.class));
}
@Test
public void testShouldReturnDifferentColorsForDifferentNumeralBase() throws Exception {
button.setNumeralBase(dec);
assertEquals(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(dec.name()));
assertEquals(button.getDirectionTextColor(hex.name()), button.getDirectionTextColor(bin.name()));
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(bin.name()));
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(hex.name()));
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(oct.name()));
}
@Test
public void testIsCurrentNumeralBase() throws Exception {
button.setNumeralBase(dec);
assertTrue(button.isCurrentNumberBase(dec.name()));
assertFalse(button.isCurrentNumberBase(hex.name()));
assertFalse(button.isCurrentNumberBase(bin.name()));
}
@Test
public void testInvalidateShouldBeCalledOnlyWhenChangeIsDone() throws Exception {
button.setNumeralBase(dec);
button = Mockito.spy(button);
button.setNumeralBase(hex);
verify(button, times(1)).invalidate();
button.setNumeralBase(hex);
verify(button, times(1)).invalidate();
}
}

View File

@ -52,6 +52,8 @@ import java.util.List;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple_mobile;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.angleUnit;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase;
/**
* User: serso
@ -75,6 +77,12 @@ public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSh
@Nonnull
private String logTag = "CalculatorActivity";
@Nullable
private AngleUnitsButton angleUnitsButton;
@Nullable
private NumeralBasesButton clearButton;
protected AbstractCalculatorHelper() {
}
@ -151,12 +159,12 @@ public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSh
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EqualsDragProcessor(), dragPreferences), vibrator, preferences));
}
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.cpp_button_6);
angleUnitsButton = getButton(root, R.id.cpp_button_6);
if (angleUnitsButton != null) {
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.AngleUnitsChanger(activity), dragPreferences), vibrator, preferences));
}
final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.cpp_button_clear);
clearButton = getButton(root, R.id.cpp_button_clear);
if (clearButton != null) {
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.NumeralBasesChanger(activity), dragPreferences), vibrator, preferences));
}
@ -266,12 +274,22 @@ public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSh
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) {
if (key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) {
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, CalculatorApplication.getInstance());
for (DragPreferencesChangeListener dragPreferencesChangeListener : dpclRegister.getListeners()) {
dragPreferencesChangeListener.onDragPreferencesChange(dragPreferences);
}
}
if (angleUnit.isSameKey(key) || numeralBase.isSameKey(key)) {
if (angleUnitsButton != null) {
angleUnitsButton.setAngleUnit(angleUnit.getPreference(preferences));
}
if (clearButton != null) {
clearButton.setNumeralBase(numeralBase.getPreference(preferences));
}
}
}
public void onDestroy(@Nonnull Activity activity) {

View File

@ -36,6 +36,14 @@ import javax.annotation.Nullable;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.hideNumeralBaseDigits;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.showEqualsButton;
import static org.solovyev.android.calculator.NumeralBaseButtons.toggleNumericDigits;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.angleUnit;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.multiplicationSign;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase;
/**
* User: Solovyev_S
* Date: 25.09.12
@ -108,21 +116,6 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
}
/* private static void setMarginsForView(@Nullable View view, int marginLeft, int marginBottom, @Nonnull Context context) {
// IMPORTANT: this is workaround for probably android bug
// currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually
if (view != null) {
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams();
final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight);
newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom));
view.setLayoutParams(newParams);
}
}
}*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@ -130,25 +123,15 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key) ||
CalculatorPreferences.Gui.hideNumeralBaseDigits.getKey().equals(key)) {
NumeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences);
if (numeralBase.isSameKey(key) || hideNumeralBaseDigits.isSameKey(key)) {
toggleNumericDigits(this.getActivity(), preferences);
}
if (AndroidCalculatorEngine.Preferences.angleUnit.getKey().equals(key) ||
AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) {
View view = getView();
if (view != null) {
// we should update state of angle units/numeral base button => we can achieve it by invalidating the whole view
view.invalidate();
}
}
if (CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key)) {
if (showEqualsButton.isSameKey(key)) {
CalculatorButtons.toggleEqualsButton(preferences, this.getActivity());
}
if (AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key)) {
if (multiplicationSign.isSameKey(key)) {
CalculatorButtons.initMultiplicationButton(getView());
}
}

View File

@ -30,6 +30,10 @@ import javax.annotation.Nonnull;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import static jscl.NumeralBase.hex;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.hideNumeralBaseDigits;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase;
/**
* User: serso
* Date: 4/20/12
@ -48,12 +52,12 @@ public class NumeralBaseButtons {
}
public static void toggleNumericDigits(@Nonnull Activity activity, @Nonnull SharedPreferences preferences) {
if (CalculatorPreferences.Gui.hideNumeralBaseDigits.getPreference(preferences)) {
final NumeralBase nb = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences);
if (hideNumeralBaseDigits.getPreference(preferences)) {
final NumeralBase nb = numeralBase.getPreference(preferences);
toggleNumericDigits(activity, nb);
} else {
// set HEX to show all digits
AndroidNumeralBase.valueOf(NumeralBase.hex).toggleButtons(true, activity);
AndroidNumeralBase.valueOf(hex).toggleButtons(true, activity);
}
}
}