Buttons refactor

This commit is contained in:
serso 2016-02-04 13:30:22 +01:00
parent e0528ae340
commit 31737ee6a1
4 changed files with 32 additions and 41 deletions

View File

@ -76,7 +76,7 @@ final class ButtonOnClickListener implements View.OnClickListener {
} }
private void onClick(@Nonnull View v, @Nonnull CppSpecialButton b) { private void onClick(@Nonnull View v, @Nonnull CppSpecialButton b) {
onClick(v, b.getActionCode()); onClick(v, b.action);
} }
private void onClick(@Nonnull View v, @Nonnull String s) { private void onClick(@Nonnull View v, @Nonnull String s) {

View File

@ -58,10 +58,7 @@ public class Keyboard {
return false; return false;
} }
App.getGa().onButtonPressed(text); App.getGa().onButtonPressed(text);
// process special buttons if (!processSpecialAction(text)) {
boolean processed = processSpecialButtons(text);
if (!processed) {
processText(prepareText(text)); processText(prepareText(text));
} }
return true; return true;
@ -104,16 +101,13 @@ public class Keyboard {
} }
} }
private boolean processSpecialButtons(@Nonnull String text) { private boolean processSpecialAction(@Nonnull String action) {
boolean result = false; final CppSpecialButton button = CppSpecialButton.getByAction(action);
if (button == null) {
final CppSpecialButton button = CppSpecialButton.getByActionCode(text); return false;
if (button != null) {
button.onClick(this);
result = true;
} }
button.onClick(this);
return result; return true;
} }
public void roundBracketsButtonPressed() { public void roundBracketsButtonPressed() {

View File

@ -94,8 +94,8 @@ public enum CppButton {
@Nullable @Nullable
public final String actionLong; public final String actionLong;
CppButton(int id, @Nonnull CppSpecialButton onClickButton, @Nullable CppSpecialButton onLongClickButton) { CppButton(int id, @Nonnull CppSpecialButton button, @Nullable CppSpecialButton buttonLong) {
this(id, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode()); this(id, button.action, buttonLong == null ? null : buttonLong.getAction());
} }
CppButton(int id, @Nonnull CppSpecialButton onClickButton) { CppButton(int id, @Nonnull CppSpecialButton onClickButton) {
@ -121,10 +121,11 @@ public enum CppButton {
private static void initButtonsByIdsMap() { private static void initButtonsByIdsMap() {
Check.isMainThread(); Check.isMainThread();
if (buttonsByIds.size() == 0) { if (buttonsByIds.size() != 0) {
return;
}
for (CppButton button : values()) { for (CppButton button : values()) {
buttonsByIds.append(button.id, button); buttonsByIds.append(button.id, button);
} }
} }
} }
}

View File

@ -22,6 +22,7 @@
package org.solovyev.android.calculator.buttons; package org.solovyev.android.calculator.buttons;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Calculator; import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorEventType; import org.solovyev.android.calculator.CalculatorEventType;
@ -170,39 +171,34 @@ public enum CppSpecialButton {
}; };
@Nonnull @Nonnull
private static Map<String, CppSpecialButton> buttonsByActionCodes = new HashMap<>(); private static Map<String, CppSpecialButton> buttonsByActions = new HashMap<>();
@Nonnull @Nonnull
private final String actionCode; public final String action;
CppSpecialButton(@Nonnull String actionCode) { CppSpecialButton(@Nonnull String action) {
this.actionCode = actionCode; this.action = action;
} }
@Nullable @Nullable
public static CppSpecialButton getByActionCode(@Nonnull String actionCode) { public static CppSpecialButton getByAction(@Nonnull String action) {
initButtonsByActionCodesMap(); initButtonsByActionsMap();
return buttonsByActionCodes.get(actionCode); return buttonsByActions.get(action);
} }
private static void initButtonsByActionCodesMap() { private static void initButtonsByActionsMap() {
if (buttonsByActionCodes.isEmpty()) { Check.isMainThread();
// if not initialized if (!buttonsByActions.isEmpty()) {
return;
final CppSpecialButton[] specialButtons = values();
final Map<String, CppSpecialButton> localButtonsByActionCodes = new HashMap<String, CppSpecialButton>(specialButtons.length);
for (CppSpecialButton specialButton : specialButtons) {
localButtonsByActionCodes.put(specialButton.getActionCode(), specialButton);
} }
for (CppSpecialButton specialButton : values()) {
buttonsByActionCodes = localButtonsByActionCodes; buttonsByActions.put(specialButton.action, specialButton);
} }
} }
@Nonnull @Nonnull
public String getActionCode() { public String getAction() {
return actionCode; return action;
} }
public abstract void onClick(@Nonnull Keyboard keyboard); public abstract void onClick(@Nonnull Keyboard keyboard);