From 31737ee6a1069c16f688dc334dfa02642d3d7243 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 4 Feb 2016 13:30:22 +0100 Subject: [PATCH] Buttons refactor --- .../calculator/ButtonOnClickListener.java | 2 +- .../solovyev/android/calculator/Keyboard.java | 20 ++++------ .../android/calculator/buttons/CppButton.java | 13 ++++--- .../calculator/buttons/CppSpecialButton.java | 38 +++++++++---------- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/ButtonOnClickListener.java b/app/src/main/java/org/solovyev/android/calculator/ButtonOnClickListener.java index 01f2759e..d5a818c0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ButtonOnClickListener.java +++ b/app/src/main/java/org/solovyev/android/calculator/ButtonOnClickListener.java @@ -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) { 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 d890ba3d..a1dd9c0e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java +++ b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java @@ -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() { diff --git a/app/src/main/java/org/solovyev/android/calculator/buttons/CppButton.java b/app/src/main/java/org/solovyev/android/calculator/buttons/CppButton.java index 09719f1f..2db37351 100644 --- a/app/src/main/java/org/solovyev/android/calculator/buttons/CppButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/buttons/CppButton.java @@ -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); } } } 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 65bf9bf8..29ba892c 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 @@ -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 buttonsByActionCodes = new HashMap<>(); + private static Map 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 localButtonsByActionCodes = new HashMap(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);