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) {
onClick(v, b.getActionCode());
onClick(v, b.action);
}
private void onClick(@Nonnull View v, @Nonnull String s) {

View File

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

View File

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

View File

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