Move units to copy/paste buttons

This commit is contained in:
serso 2016-03-10 15:21:46 +01:00
parent 5b7205b299
commit 9ea6349946
14 changed files with 137 additions and 102 deletions

View File

@ -12,18 +12,35 @@ import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.ActivityLauncher;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.PreferredPreferences;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.views.Adjuster;
import org.solovyev.android.views.dragbutton.*;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DirectionDragListener;
import org.solovyev.android.views.dragbutton.DirectionDragView;
import org.solovyev.android.views.dragbutton.DragDirection;
import org.solovyev.android.views.dragbutton.DragEvent;
import org.solovyev.android.views.dragbutton.DragView;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.view.HapticFeedbackConstants.*;
import static android.view.HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING;
import static android.view.HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING;
import static android.view.HapticFeedbackConstants.KEYBOARD_TAP;
import static org.solovyev.android.calculator.App.cast;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile;
@ -134,7 +151,7 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
Adjuster.adjustText(button, textScale);
}
protected final void hideText(@Nullable DirectionDragButton button, @Nonnull DragDirection... directions) {
protected final void hideText(@Nullable DirectionDragView button, @Nonnull DragDirection... directions) {
if (button == null) {
return;
}
@ -143,11 +160,11 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
}
}
protected final void hideText(@Nullable DirectionDragButton button, @Nonnull DragDirection direction) {
protected final void hideText(@Nullable DirectionDragView button, @Nonnull DragDirection direction) {
if (button == null) {
return;
}
button.setShowDirectionText(direction, false);
button.getText(direction).setVisible(false);
}
public void onDestroyView() {

View File

@ -10,6 +10,26 @@ import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import org.solovyev.android.calculator.ActivityLauncher;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Display;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.memory.Memory;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DirectionDragView;
import org.solovyev.android.views.dragbutton.DragDirection;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import butterknife.Bind;
import butterknife.ButterKnife;
import dagger.Lazy;
@ -17,21 +37,13 @@ import jscl.AngleUnit;
import jscl.NumeralBase;
import jscl.math.Expression;
import jscl.math.Generic;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.memory.Memory;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DragButton;
import org.solovyev.android.views.dragbutton.DragDirection;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.Engine.Preferences.*;
import static org.solovyev.android.views.dragbutton.DragDirection.*;
import static org.solovyev.android.calculator.Engine.Preferences.angleUnit;
import static org.solovyev.android.calculator.Engine.Preferences.multiplicationSign;
import static org.solovyev.android.calculator.Engine.Preferences.numeralBase;
import static org.solovyev.android.views.dragbutton.DragDirection.down;
import static org.solovyev.android.views.dragbutton.DragDirection.left;
import static org.solovyev.android.views.dragbutton.DragDirection.up;
public class KeyboardUi extends BaseKeyboardUi {
@ -48,7 +60,7 @@ public class KeyboardUi extends BaseKeyboardUi {
@Bind(R.id.cpp_button_5)
public DirectionDragButton button5;
@Bind(R.id.cpp_button_6)
public AngleUnitsButton button6;
public DirectionDragButton button6;
@Bind(R.id.cpp_button_7)
public DirectionDragButton button7;
@Bind(R.id.cpp_button_8)
@ -89,9 +101,9 @@ public class KeyboardUi extends BaseKeyboardUi {
@Bind(R.id.cpp_button_round_brackets)
DirectionDragButton bracketsButton;
@Bind(R.id.cpp_button_copy)
ImageButton copyButton;
NumeralBasesButton copyButton;
@Bind(R.id.cpp_button_paste)
ImageButton pasteButton;
AngleUnitsButton pasteButton;
@Nullable
@Bind(R.id.cpp_button_like)
ImageButton likeButton;
@ -139,13 +151,14 @@ public class KeyboardUi extends BaseKeyboardUi {
prepareButton(button4);
prepareButton(button5);
prepareButton(button6);
button6.setAngleUnit(angleUnit.getPreference(preferences));
prepareButton(button7);
prepareButton(button8);
prepareButton(button9);
prepareButton(copyButton);
copyButton.setNumeralBase(numeralBase.getPreference(preferences));
prepareButton(pasteButton);
pasteButton.setAngleUnit(angleUnit.getPreference(preferences));
prepareButton(likeButton);
prepareButton(memoryButton);
@ -155,12 +168,13 @@ public class KeyboardUi extends BaseKeyboardUi {
hideText(button3, up, down);
hideText(button4, down);
hideText(button5, down);
hideText(button6, up, down);
hideText(button7, left, up, down);
hideText(button8, left, up, down);
hideText(button9, left);
hideText(multiplicationButton, left);
hideText(plusButton, up, down);
hideText(copyButton, left, up, down);
hideText(pasteButton, left, up, down);
}
multiplicationButton.setText(engine.getMultiplicationSign());
toggleNumericDigits();
@ -176,10 +190,11 @@ public class KeyboardUi extends BaseKeyboardUi {
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
super.onSharedPreferenceChanged(preferences, key);
if (angleUnit.isSameKey(key)) {
button6.setAngleUnit(angleUnit.getPreference(preferences));
pasteButton.setAngleUnit(angleUnit.getPreference(preferences));
}
if (numeralBase.isSameKey(key)) {
toggleNumericDigits();
copyButton.setNumeralBase(numeralBase.getPreference(preferences));
}
if (multiplicationSign.isSameKey(key)) {
multiplicationButton.setText(multiplicationSign.getPreference(preferences));
@ -263,16 +278,18 @@ public class KeyboardUi extends BaseKeyboardUi {
return true;
}
return false;
case R.id.cpp_button_6:
return processAngleUnitsButton(direction, (DirectionDragButton) view);
case R.id.cpp_button_copy:
return processNumeralBaseButton(direction, (DirectionDragView) view);
case R.id.cpp_button_paste:
return processAngleUnitsButton(direction, (DirectionDragView) view);
case R.id.cpp_button_round_brackets:
if (direction == left) {
keyboard.roundBracketsButtonPressed();
return true;
}
return processDefault(direction, (DragButton) view);
return processDefault(direction, (DirectionDragView) view);
default:
return processDefault(direction, (DragButton) view);
return processDefault(direction, (DirectionDragView) view);
}
}
@ -306,11 +323,11 @@ public class KeyboardUi extends BaseKeyboardUi {
return false;
}
private boolean processAngleUnitsButton(@Nonnull DragDirection direction, @Nonnull DirectionDragButton button) {
private boolean processAngleUnitsButton(@Nonnull DragDirection direction, @Nonnull DirectionDragView button) {
if (direction == DragDirection.left) {
return processDefault(direction, button);
}
final String text = button.getTextValue(direction);
final String text = button.getText(direction).getValue();
if (TextUtils.isEmpty(text)) {
return processDefault(direction, button);
}
@ -327,8 +344,26 @@ public class KeyboardUi extends BaseKeyboardUi {
return false;
}
private boolean processDefault(@Nonnull DragDirection direction, @Nonnull DragButton button) {
final String text = ((DirectionDragButton) button).getTextValue(direction);
private boolean processDefault(@Nonnull DragDirection direction, @Nonnull DirectionDragView button) {
final String text = button.getText(direction).getValue();
return keyboard.buttonPressed(text);
}
private boolean processNumeralBaseButton(@Nonnull DragDirection direction, @Nonnull DirectionDragView button) {
final String text = button.getText(direction).getValue();
if (TextUtils.isEmpty(text)) {
return false;
}
try {
final NumeralBase newNumeralBase = NumeralBase.valueOf(text);
final NumeralBase oldNumeralBase = Engine.Preferences.numeralBase.getPreference(preferences);
if (oldNumeralBase != newNumeralBase) {
preferredPreferences.setNumeralBase(newNumeralBase);
return true;
}
} catch (IllegalArgumentException e) {
Log.d(this.getClass().getName(), "Unsupported numeral base: " + text);
}
return false;
}
}

View File

@ -6,19 +6,14 @@ import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.NumeralBase;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.view.EditorLongClickEraser;
import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DragDirection;
@ -26,10 +21,13 @@ import org.solovyev.android.views.dragbutton.DragDirection;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.Engine.Preferences.numeralBase;
import butterknife.Bind;
import butterknife.ButterKnife;
import static org.solovyev.android.calculator.Preferences.Gui.showEqualsButton;
import static org.solovyev.android.calculator.Preferences.Gui.vibrateOnKeypress;
import static org.solovyev.android.views.dragbutton.DragDirection.*;
import static org.solovyev.android.views.dragbutton.DragDirection.down;
import static org.solovyev.android.views.dragbutton.DragDirection.up;
public class PartialKeyboardUi extends BaseKeyboardUi {
@ -41,7 +39,7 @@ public class PartialKeyboardUi extends BaseKeyboardUi {
DirectionDragImageButton leftButton;
@Nullable
@Bind(R.id.cpp_button_clear)
NumeralBasesButton clearButton;
Button clearButton;
@Nullable
@Bind(R.id.cpp_button_erase)
ImageButton eraseButton;
@ -64,17 +62,11 @@ public class PartialKeyboardUi extends BaseKeyboardUi {
prepareButton(leftButton);
prepareButton(equalsButton);
prepareButton(clearButton);
if (clearButton != null) {
clearButton.setNumeralBase(numeralBase.getPreference(preferences));
}
if (eraseButton != null) {
// backspace button is too big, scale it more
prepareButton(eraseButton, IMAGE_SCALE_ERASE);
longClickEraser = EditorLongClickEraser.attachTo(eraseButton, keyboard.isVibrateOnKeypress(), editor, calculator);
}
if (isSimpleLayout()) {
hideText(clearButton, left, up, down);
}
toggleEqualsButton();
}
@ -96,9 +88,6 @@ public class PartialKeyboardUi extends BaseKeyboardUi {
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
super.onSharedPreferenceChanged(preferences, key);
if (clearButton != null && numeralBase.isSameKey(key)) {
clearButton.setNumeralBase(numeralBase.getPreference(preferences));
}
if (equalsButton != null && showEqualsButton.isSameKey(key)) {
toggleEqualsButton();
}
@ -126,8 +115,6 @@ public class PartialKeyboardUi extends BaseKeyboardUi {
}
return false;
case R.id.cpp_button_clear:
return processNumeralBaseButton(direction, (DirectionDragButton) view);
}
return false;
}
@ -152,22 +139,4 @@ public class PartialKeyboardUi extends BaseKeyboardUi {
break;
}
}
private boolean processNumeralBaseButton(@Nonnull DragDirection direction, @Nonnull DirectionDragButton button) {
final String text = button.getTextValue(direction);
if (TextUtils.isEmpty(text)) {
return false;
}
try {
final NumeralBase newNumeralBase = NumeralBase.valueOf(text);
final NumeralBase oldNumeralBase = Engine.Preferences.numeralBase.getPreference(preferences);
if (oldNumeralBase != newNumeralBase) {
preferredPreferences.setNumeralBase(newNumeralBase);
return true;
}
} catch (IllegalArgumentException e) {
Log.d(this.getClass().getName(), "Unsupported numeral base: " + text);
}
return false;
}
}

View File

@ -27,7 +27,7 @@ import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import org.solovyev.android.calculator.R;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DirectionTextView;
import org.solovyev.android.views.dragbutton.DragDirection;
@ -35,7 +35,7 @@ import javax.annotation.Nonnull;
import jscl.AngleUnit;
public class AngleUnitsButton extends DirectionDragButton {
public class AngleUnitsButton extends DirectionDragImageButton {
@Nonnull
private AngleUnit angleUnit = AngleUnit.deg;

View File

@ -27,7 +27,7 @@ import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import org.solovyev.android.calculator.R;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DirectionTextView;
import org.solovyev.android.views.dragbutton.DragDirection;
@ -35,7 +35,7 @@ import javax.annotation.Nonnull;
import jscl.NumeralBase;
public class NumeralBasesButton extends DirectionDragButton {
public class NumeralBasesButton extends DirectionDragImageButton {
@Nonnull
private NumeralBase numeralBase = NumeralBase.dec;

View File

@ -10,7 +10,7 @@ import android.util.AttributeSet;
import javax.annotation.Nonnull;
public class DirectionDragButton extends DragButton {
public class DirectionDragButton extends DragButton implements DirectionDragView {
private final DirectionTextView textView = new DirectionTextView();
@NonNull
private final TextPaint baseTextPaint = new TextPaint();
@ -69,8 +69,9 @@ public class DirectionDragButton extends DragButton {
return this;
}
@Override
@Nonnull
protected DirectionTextView.Text getText(@NonNull DragDirection direction) {
public DirectionTextView.Text getText(@NonNull DragDirection direction) {
return textView.getText(direction);
}

View File

@ -11,7 +11,7 @@ import android.widget.TextView;
import javax.annotation.Nonnull;
public class DirectionDragImageButton extends DragImageButton {
public class DirectionDragImageButton extends DragImageButton implements DirectionDragView {
private final DirectionTextView textView = new DirectionTextView();
private final TextPaint baseTextPaint = new TextPaint();
@ -48,7 +48,7 @@ public class DirectionDragImageButton extends DragImageButton {
}
@Nonnull
protected DirectionTextView.Text getText(@NonNull DragDirection direction) {
public DirectionTextView.Text getText(@NonNull DragDirection direction) {
return textView.getText(direction);
}

View File

@ -0,0 +1,10 @@
package org.solovyev.android.views.dragbutton;
import android.support.annotation.NonNull;
import javax.annotation.Nonnull;
public interface DirectionDragView extends DragView {
@Nonnull
DirectionTextView.Text getText(@NonNull DragDirection direction);
}

View File

@ -4,6 +4,5 @@ import android.support.annotation.Nullable;
public interface DragView {
void setOnDragListener(@Nullable DragListener listener);
void setVibrateOnDrag(boolean vibrateOnDrag);
}

View File

@ -22,14 +22,12 @@
~ Site: http://se.solovyev.org
-->
<org.solovyev.android.calculator.view.AngleUnitsButton
<org.solovyev.android.views.dragbutton.DirectionDragButton
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
a:id="@id/cpp_button_6"
style="?attr/cpp_button_style_digit"
a:text="6"
c:directionTextDown="rad"
c:directionTextLeft="F"
c:directionTextUp="deg"
tools:ignore="HardcodedText" />

View File

@ -22,13 +22,9 @@
~ Site: http://se.solovyev.org
-->
<org.solovyev.android.calculator.view.NumeralBasesButton
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res-auto"
<Button
a:id="@id/cpp_button_clear"
style="?attr/cpp_button_style_control_image"
xmlns:a="http://schemas.android.com/apk/res/android"
a:text="@string/c_clear"
a:textStyle="bold"
c:directionTextDown="bin"
c:directionTextLeft="hex"
c:directionTextUp="dec" />
a:textStyle="bold"/>

View File

@ -22,8 +22,13 @@
~ Site: http://se.solovyev.org
-->
<ImageButton
xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@id/cpp_button_copy"
<org.solovyev.android.calculator.view.NumeralBasesButton
android:id="@id/cpp_button_copy"
style="?attr/cpp_button_style_control_image"
a:src="@drawable/ic_content_copy_white_48dp" />
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:src="@drawable/ic_content_copy_white_48dp"
app:directionTextScale="@dimen/cpp_direction_text_scale_units"
app:directionTextDown="bin"
app:directionTextLeft="hex"
app:directionTextUp="dec"/>

View File

@ -22,8 +22,12 @@
~ Site: http://se.solovyev.org
-->
<ImageButton
xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@id/cpp_button_paste"
<org.solovyev.android.calculator.view.AngleUnitsButton
android:id="@id/cpp_button_paste"
style="?attr/cpp_button_style_control_image"
a:src="@drawable/ic_content_paste_white_48dp" />
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:directionTextScale="@dimen/cpp_direction_text_scale_units"
android:src="@drawable/ic_content_paste_white_48dp"
app:directionTextDown="rad"
app:directionTextUp="deg"/>

View File

@ -11,6 +11,7 @@
<dimen name="cpp_direction_text_default_padding">2dp</dimen>
<dimen name="cpp_direction_text_min_size">9dp</dimen>
<item name="cpp_direction_text_scale" format="float" type="dimen">0.45</item>
<item name="cpp_direction_text_scale_units" format="float" type="dimen">0.35</item>
<dimen name="cpp_button_corner">1dp</dimen>
<dimen name="cpp_keyboard_button_direction_text_padding">4dp</dimen>