Avoid using Locator/App in CppSpecialButton

This commit is contained in:
serso 2016-02-04 13:53:20 +01:00
parent 31737ee6a1
commit ebdfbc328c
3 changed files with 118 additions and 145 deletions

View File

@ -27,10 +27,10 @@ import android.content.Context;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import dagger.Lazy;
import jscl.math.Generic;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.calculations.CalculationCancelledEvent; import org.solovyev.android.calculator.calculations.CalculationCancelledEvent;
import org.solovyev.android.calculator.calculations.CalculationFailedEvent; import org.solovyev.android.calculator.calculations.CalculationFailedEvent;
@ -44,6 +44,9 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import dagger.Lazy;
import jscl.math.Generic;
import static org.solovyev.android.calculator.BaseFragment.addMenu; import static org.solovyev.android.calculator.BaseFragment.addMenu;
import static org.solovyev.android.calculator.CalculatorEventType.conversion_failed; import static org.solovyev.android.calculator.CalculatorEventType.conversion_failed;
import static org.solovyev.android.calculator.CalculatorEventType.conversion_result; import static org.solovyev.android.calculator.CalculatorEventType.conversion_result;
@ -58,8 +61,6 @@ public class Display implements CalculatorEventListener, View.OnClickListener, V
@Inject @Inject
Application application; Application application;
@Inject @Inject
Lazy<Keyboard> keyboard;
@Inject
Lazy<Clipboard> clipboard; Lazy<Clipboard> clipboard;
@Inject @Inject
Lazy<Notifier> notifier; Lazy<Notifier> notifier;
@ -78,6 +79,10 @@ public class Display implements CalculatorEventListener, View.OnClickListener, V
@Subscribe @Subscribe
public void onCopy(@Nonnull CopyOperation o) { public void onCopy(@Nonnull CopyOperation o) {
copy();
}
private void copy() {
if (!state.valid) { if (!state.valid) {
return; return;
} }
@ -226,7 +231,7 @@ public class Display implements CalculatorEventListener, View.OnClickListener, V
final Generic result = state.getResult(); final Generic result = state.getResult();
switch (item.getItemId()) { switch (item.getItemId()) {
case R.string.c_copy: case R.string.c_copy:
keyboard.get().copyButtonPressed(); copy();
return true; return true;
case R.string.convert_to_bin: case R.string.convert_to_bin:
ConversionMenuItem.convert_to_bin.onClick(state, App.getApplication()); ConversionMenuItem.convert_to_bin.onClick(state, App.getApplication());

View File

@ -22,10 +22,12 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.buttons.CppSpecialButton; import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
@ -45,6 +47,10 @@ public class Keyboard {
@Inject @Inject
Editor editor; Editor editor;
@Inject @Inject
Display display;
@Inject
Calculator calculator;
@Inject
Lazy<Clipboard> clipboard; Lazy<Clipboard> clipboard;
@Inject @Inject
Lazy<Bus> bus; Lazy<Bus> bus;
@ -106,10 +112,91 @@ public class Keyboard {
if (button == null) { if (button == null) {
return false; return false;
} }
button.onClick(this); onSpecialButtonPressed(button);
return true; return true;
} }
private void onSpecialButtonPressed(@NonNull CppSpecialButton button) {
switch (button) {
case history:
calculator.fireCalculatorEvent(CalculatorEventType.show_history, null);
break;
case history_detached:
calculator.fireCalculatorEvent(CalculatorEventType.show_history_detached, null);
break;
case cursor_right:
moveCursorRight();
break;
case cursor_left:
moveCursorLeft();
break;
case settings:
calculator.fireCalculatorEvent(CalculatorEventType.show_settings, null);
break;
case settings_detached:
calculator.fireCalculatorEvent(CalculatorEventType.show_settings_detached, null);
break;
case settings_widget:
calculator.fireCalculatorEvent(CalculatorEventType.show_settings_widget, null);
break;
case like:
calculator.fireCalculatorEvent(CalculatorEventType.show_like_dialog, null);
break;
case erase:
editor.erase();
break;
case paste:
pasteButtonPressed();
break;
case copy:
copyButtonPressed();
break;
case equals:
equalsButtonPressed();
break;
case clear:
clearButtonPressed();
break;
case functions:
calculator.fireCalculatorEvent(CalculatorEventType.show_functions, null);
break;
case functions_detached:
calculator.fireCalculatorEvent(CalculatorEventType.show_functions_detached, null);
break;
case open_app:
calculator.fireCalculatorEvent(CalculatorEventType.open_app, null);
break;
case vars:
calculator.fireCalculatorEvent(CalculatorEventType.show_vars, null);
break;
case vars_detached:
calculator.fireCalculatorEvent(CalculatorEventType.show_vars_detached, null);
break;
case operators:
calculator.fireCalculatorEvent(CalculatorEventType.show_operators, null);
break;
case operators_detached:
calculator.fireCalculatorEvent(CalculatorEventType.show_operators_detached, null);
break;
default:
Check.shouldNotHappen();
}
}
private void equalsButtonPressed() {
if (!calculator.isCalculateOnFly()) {
// no automatic calculations are => equals button must be used to calculate
calculator.evaluate();
return;
}
final DisplayState state = display.getState();
if (!state.valid) {
return;
}
editor.setText(state.text);
}
public void roundBracketsButtonPressed() { public void roundBracketsButtonPressed() {
EditorState viewState = editor.getState(); EditorState viewState = editor.getState();

View File

@ -23,12 +23,6 @@
package org.solovyev.android.calculator.buttons; package org.solovyev.android.calculator.buttons;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Locator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -38,137 +32,26 @@ import javax.annotation.Nullable;
public enum CppSpecialButton { public enum CppSpecialButton {
history("history") { history("history"),
@Override history_detached("history_detached"),
public void onClick(@Nonnull Keyboard keyboard) { cursor_right(""),
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history, null); cursor_left(""),
} settings("settings"),
}, settings_detached("settings_detached"),
history_detached("history_detached") { settings_widget("settings_widget"),
@Override like("like"),
public void onClick(@Nonnull Keyboard keyboard) { erase("erase"),
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history_detached, null); paste("paste"),
} copy("copy"),
}, equals("="),
cursor_right("") { clear("clear"),
@Override functions("functions"),
public void onClick(@Nonnull Keyboard keyboard) { functions_detached("functions_detached"),
keyboard.moveCursorRight(); open_app("open_app"),
} vars("vars"),
}, vars_detached("vars_detached"),
cursor_left("") { operators("operators"),
@Override operators_detached("operators_detached");
public void onClick(@Nonnull Keyboard keyboard) {
keyboard.moveCursorLeft();
}
},
settings("settings") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null);
}
},
settings_detached("settings_detached") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null);
}
},
settings_widget("settings_widget") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_widget, null);
}
},
like("like") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_like_dialog, null);
}
},
erase("erase") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
App.getEditor().erase();
}
},
paste("paste") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
keyboard.pasteButtonPressed();
}
},
copy("copy") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
keyboard.copyButtonPressed();
}
},
equals("=") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
final Calculator calculator = Locator.getInstance().getCalculator();
if (!calculator.isCalculateOnFly()) {
// no automatic calculations are => equals button must be used to calculate
calculator.evaluate();
return;
}
final DisplayState displayState = App.getDisplay().getState();
if (!displayState.valid) {
return;
}
App.getEditor().setText(displayState.text);
}
},
clear("clear") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
keyboard.clearButtonPressed();
}
},
functions("functions") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions, null);
}
},
functions_detached("functions_detached") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions_detached, null);
}
},
open_app("open_app") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.open_app, null);
}
},
vars("vars") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars, null);
}
},
vars_detached("vars_detached") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars_detached, null);
}
},
operators("operators") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null);
}
},
operators_detached("operators_detached") {
@Override
public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators_detached, null);
}
};
@Nonnull @Nonnull
private static Map<String, CppSpecialButton> buttonsByActions = new HashMap<>(); private static Map<String, CppSpecialButton> buttonsByActions = new HashMap<>();
@ -200,6 +83,4 @@ public enum CppSpecialButton {
public String getAction() { public String getAction() {
return action; return action;
} }
public abstract void onClick(@Nonnull Keyboard keyboard);
} }