From ebdfbc328ce10969606c86ed0af5050d40ef02ac Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 4 Feb 2016 13:53:20 +0100 Subject: [PATCH] Avoid using Locator/App in CppSpecialButton --- .../solovyev/android/calculator/Display.java | 15 +- .../solovyev/android/calculator/Keyboard.java | 89 +++++++++- .../calculator/buttons/CppSpecialButton.java | 159 +++--------------- 3 files changed, 118 insertions(+), 145 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/Display.java b/app/src/main/java/org/solovyev/android/calculator/Display.java index fee0a75c..67035194 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Display.java +++ b/app/src/main/java/org/solovyev/android/calculator/Display.java @@ -27,10 +27,10 @@ import android.content.Context; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; + import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; -import dagger.Lazy; -import jscl.math.Generic; + import org.solovyev.android.Check; import org.solovyev.android.calculator.calculations.CalculationCancelledEvent; import org.solovyev.android.calculator.calculations.CalculationFailedEvent; @@ -44,6 +44,9 @@ import javax.annotation.Nullable; import javax.inject.Inject; 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.CalculatorEventType.conversion_failed; import static org.solovyev.android.calculator.CalculatorEventType.conversion_result; @@ -58,8 +61,6 @@ public class Display implements CalculatorEventListener, View.OnClickListener, V @Inject Application application; @Inject - Lazy keyboard; - @Inject Lazy clipboard; @Inject Lazy notifier; @@ -78,6 +79,10 @@ public class Display implements CalculatorEventListener, View.OnClickListener, V @Subscribe public void onCopy(@Nonnull CopyOperation o) { + copy(); + } + + private void copy() { if (!state.valid) { return; } @@ -226,7 +231,7 @@ public class Display implements CalculatorEventListener, View.OnClickListener, V final Generic result = state.getResult(); switch (item.getItemId()) { case R.string.c_copy: - keyboard.get().copyButtonPressed(); + copy(); return true; case R.string.convert_to_bin: ConversionMenuItem.convert_to_bin.onClick(state, App.getApplication()); diff --git a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java index a1dd9c0e..9855dca8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java +++ b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java @@ -22,10 +22,12 @@ package org.solovyev.android.calculator; +import android.support.annotation.NonNull; import android.text.TextUtils; import com.squareup.otto.Bus; +import org.solovyev.android.Check; import org.solovyev.android.calculator.buttons.CppSpecialButton; import org.solovyev.android.calculator.math.MathType; @@ -45,6 +47,10 @@ public class Keyboard { @Inject Editor editor; @Inject + Display display; + @Inject + Calculator calculator; + @Inject Lazy clipboard; @Inject Lazy bus; @@ -106,10 +112,91 @@ public class Keyboard { if (button == null) { return false; } - button.onClick(this); + onSpecialButtonPressed(button); 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() { EditorState viewState = editor.getState(); diff --git a/app/src/main/java/org/solovyev/android/calculator/buttons/CppSpecialButton.java b/app/src/main/java/org/solovyev/android/calculator/buttons/CppSpecialButton.java index 29ba892c..9c0173a6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/buttons/CppSpecialButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/buttons/CppSpecialButton.java @@ -23,12 +23,6 @@ package org.solovyev.android.calculator.buttons; 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.Map; @@ -38,137 +32,26 @@ import javax.annotation.Nullable; public enum CppSpecialButton { - history("history") { - @Override - public void onClick(@Nonnull Keyboard keyboard) { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history, null); - } - }, - history_detached("history_detached") { - @Override - public void onClick(@Nonnull Keyboard keyboard) { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history_detached, null); - } - }, - cursor_right("▷") { - @Override - public void onClick(@Nonnull Keyboard keyboard) { - keyboard.moveCursorRight(); - } - }, - cursor_left("◁") { - @Override - 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); - } - }; + history("history"), + history_detached("history_detached"), + cursor_right("▷"), + cursor_left("◁"), + settings("settings"), + settings_detached("settings_detached"), + settings_widget("settings_widget"), + like("like"), + erase("erase"), + paste("paste"), + copy("copy"), + equals("="), + clear("clear"), + functions("functions"), + functions_detached("functions_detached"), + open_app("open_app"), + vars("vars"), + vars_detached("vars_detached"), + operators("operators"), + operators_detached("operators_detached"); @Nonnull private static Map buttonsByActions = new HashMap<>(); @@ -200,6 +83,4 @@ public enum CppSpecialButton { public String getAction() { return action; } - - public abstract void onClick(@Nonnull Keyboard keyboard); }