cpp-131: Long tap on erase button should remove text faster

Fixes #131
This commit is contained in:
serso 2015-02-16 12:04:35 +01:00
parent b2eed7a0c3
commit a68d93b75a
8 changed files with 130 additions and 48 deletions

View File

@ -104,6 +104,11 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
return calculator.evaluate(operation, expression, sequenceId);
}
@Override
public void setCalculateOnFly(boolean calculateOnFly) {
calculator.setCalculateOnFly(calculateOnFly);
}
@Override
public boolean isCalculateOnFly() {
return calculator.isCalculateOnFly();

View File

@ -39,6 +39,7 @@ import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.history.HistoryDragProcessor;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.DragListenerVibrator;
import org.solovyev.android.calculator.view.LongClickEraser;
import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.calculator.view.ViewsCache;
import org.solovyev.common.math.Point2d;
@ -174,6 +175,11 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
angleUnitsButton.setOnDragListener(new DragListenerVibrator(newDragListener(new CalculatorButtons.AngleUnitsChanger(activity), activity), vibrator, preferences));
}
final View eraseButton = getButton(views, R.id.cpp_button_erase);
if (eraseButton != null) {
LongClickEraser.createAndAttach(eraseButton);
}
clearButton = getButton(views, R.id.cpp_button_clear);
if (clearButton != null) {
clearButton.setOnDragListener(new DragListenerVibrator(newDragListener(new CalculatorButtons.NumeralBasesChanger(activity), activity), vibrator, preferences));
@ -292,8 +298,8 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
}
@Nullable
private <T extends DragButton> T getButton(@Nonnull ViewsCache views, int buttonId) {
return (T) views.findViewById(buttonId);
private <V extends View> V getButton(@Nonnull ViewsCache views, int buttonId) {
return (V) views.findViewById(buttonId);
}
@Override

View File

@ -115,14 +115,12 @@ public enum CalculatorButton {
}
public void onLongClick() {
Locator.getInstance().getNotifier().showDebugMessage("Calculator++ Widget", "Button pressed: " + onLongClickText);
if (onLongClickText != null) {
Locator.getInstance().getKeyboard().buttonPressed(onLongClickText);
}
}
public void onClick() {
Locator.getInstance().getNotifier().showDebugMessage("Calculator++ Widget", "Button pressed: " + onClickText);
Locator.getInstance().getKeyboard().buttonPressed(onClickText);
}

View File

@ -24,14 +24,12 @@ package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.NumeralBaseButtons.toggleNumericDigits;
import static org.solovyev.android.calculator.Preferences.Gui.hideNumeralBaseDigits;
@ -47,68 +45,55 @@ import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Pref
public class CalculatorKeyboardFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener {
@Nonnull
private Preferences.Gui.Theme theme;
@Nonnull
private FragmentUi fragmentHelper;
private FragmentUi ui;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
final SharedPreferences preferences = App.getPreferences();
final Preferences.Gui.Layout layout = Preferences.Gui.getLayout(preferences);
if (!layout.isOptimized()) {
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(R.layout.cpp_app_keyboard_mobile);
ui = CalculatorApplication.getInstance().createFragmentHelper(R.layout.cpp_app_keyboard_mobile);
} else {
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(R.layout.cpp_app_keyboard);
ui = CalculatorApplication.getInstance().createFragmentHelper(R.layout.cpp_app_keyboard);
}
fragmentHelper.onCreate(this);
ui.onCreate(this);
preferences.registerOnSharedPreferenceChangeListener(this);
theme = Preferences.Gui.theme.getPreferenceNoError(preferences);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return fragmentHelper.onCreateView(this, inflater, container);
return ui.onCreateView(this, inflater, container);
}
@Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
fragmentHelper.onViewCreated(this, root);
ui.onViewCreated(this, root);
}
@Override
public void onResume() {
super.onResume();
this.fragmentHelper.onResume(this);
this.ui.onResume(this);
}
@Override
public void onPause() {
this.fragmentHelper.onPause(this);
this.ui.onPause(this);
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
fragmentHelper.onDestroy(this);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
preferences.unregisterOnSharedPreferenceChangeListener(this);
ui.onDestroy(this);
App.getPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
@ -130,21 +115,5 @@ public class CalculatorKeyboardFragment extends Fragment implements SharedPrefer
CalculatorButtons.initMultiplicationButton(getView());
}
}
@Nullable
private static AndroidCalculatorDisplayView getCalculatorDisplayView() {
return (AndroidCalculatorDisplayView) Locator.getInstance().getDisplay().getView();
}
@Nonnull
private Calculator getCalculator() {
return Locator.getInstance().getCalculator();
}
@Nonnull
private static CalculatorKeyboard getKeyboard() {
return Locator.getInstance().getKeyboard();
}
}

View File

@ -0,0 +1,96 @@
package org.solovyev.android.calculator.view;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorEditor;
import org.solovyev.android.calculator.Locator;
import javax.annotation.Nonnull;
public final class LongClickEraser implements View.OnTouchListener {
@Nonnull
private final View view;
@Nonnull
private final GestureDetector gestureDetector;
@Nonnull
private final CalculatorEditor editor = Locator.getInstance().getEditor();
@Nonnull
private final Calculator calculator = Locator.getInstance().getCalculator();
@Nonnull
private final Eraser eraser = new Eraser();
public static void createAndAttach(@Nonnull View view) {
view.setOnTouchListener(new LongClickEraser(view));
}
private LongClickEraser(@Nonnull final View view) {
this.view = view;
this.gestureDetector = new GestureDetector(view.getContext(), new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
eraser.start();
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
eraser.stop();
break;
default:
gestureDetector.onTouchEvent(event);
break;
}
return true;
}
private class Eraser implements Runnable {
private static final int DELAY = 500;
private long delay;
private boolean wasCalculatingOnFly;
private boolean erasing;
@Override
public void run() {
editor.erase();
delay = Math.max(50, 2 * delay / 3);
view.postDelayed(this, delay);
}
void start() {
if (erasing) {
stop();
}
erasing = true;
delay = DELAY;
wasCalculatingOnFly = calculator.isCalculateOnFly();
if (wasCalculatingOnFly) {
calculator.setCalculateOnFly(false);
}
view.removeCallbacks(this);
view.post(this);
}
void stop() {
view.removeCallbacks(this);
if (!erasing) {
return;
}
erasing = false;
if (wasCalculatingOnFly) {
calculator.setCalculateOnFly(true);
}
}
}
}

View File

@ -64,6 +64,8 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
@Nonnull String expression,
@Nonnull Long sequenceId);
void setCalculateOnFly(boolean calculateOnFly);
boolean isCalculateOnFly();
/*

View File

@ -207,8 +207,14 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
Locator.getInstance().getHistory().load();
}
@Override
public void setCalculateOnFly(boolean calculateOnFly) {
if (this.calculateOnFly != calculateOnFly) {
this.calculateOnFly = calculateOnFly;
if (this.calculateOnFly) {
evaluate();
}
}
}
@Override

View File

@ -169,7 +169,7 @@ public enum CalculatorSpecialButton {
};
@Nonnull
private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<String, CalculatorSpecialButton>();
private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<>();
@Nonnull
private final String actionCode;