New architecture

This commit is contained in:
Sergey Solovyev 2012-09-23 19:02:35 +04:00
parent f03c2496a6
commit bb2bf70cc2
8 changed files with 66 additions and 16 deletions

View File

@ -27,6 +27,8 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
void evaluate(); void evaluate();
void evaluate(@NotNull Long sequenceId);
void simplify(); void simplify();
@NotNull @NotNull

View File

@ -165,7 +165,7 @@ public class CalculatorEngineImpl implements CalculatorEngine {
@Override @Override
public void softReset() { public void softReset() {
// do nothing CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null);
} }
/* /*

View File

@ -17,6 +17,10 @@ public enum CalculatorEventType {
********************************************************************** **********************************************************************
*/ */
// @NotNull CalculatorEditorViewState
manual_calculation_requested,
// @NotNull org.solovyev.android.calculator.CalculatorInput // @NotNull org.solovyev.android.calculator.CalculatorInput
calculation_started, calculation_started,
@ -54,7 +58,17 @@ public enum CalculatorEventType {
editor_state_changed, editor_state_changed,
// @NotNull CalculatorDisplayChangeEventData // @NotNull CalculatorDisplayChangeEventData
display_state_changed; display_state_changed,
/*
**********************************************************************
*
* ENGINE
*
**********************************************************************
*/
engine_preferences_changed;
public boolean isOfType(@NotNull CalculatorEventType... types) { public boolean isOfType(@NotNull CalculatorEventType... types) {
for (CalculatorEventType type : types) { for (CalculatorEventType type : types) {

View File

@ -96,12 +96,23 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Override @Override
public void evaluate() { public void evaluate() {
this.evaluate(JsclOperation.numeric, getEditor().getViewState().getText()); final CalculatorEditorViewState viewState = getEditor().getViewState();
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
this.evaluate(JsclOperation.numeric, viewState.getText());
}
@Override
public void evaluate(@NotNull Long sequenceId) {
final CalculatorEditorViewState viewState = getEditor().getViewState();
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId);
this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId);
} }
@Override @Override
public void simplify() { public void simplify() {
this.evaluate(JsclOperation.simplify, getEditor().getViewState().getText()); final CalculatorEditorViewState viewState = getEditor().getViewState();
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
this.evaluate(JsclOperation.simplify, viewState.getText());
} }
@NotNull @NotNull
@ -335,15 +346,21 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Override @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
if (calculatorEventType == CalculatorEventType.editor_state_changed) {
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
final String newText = changeEventData.getNewState().getText(); switch (calculatorEventType) {
final String oldText = changeEventData.getOldState().getText(); case editor_state_changed:
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
if (!newText.equals(oldText)) { final String newText = changeEventData.getNewState().getText();
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId()); final String oldText = changeEventData.getOldState().getText();
}
if (!newText.equals(oldText)) {
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
}
break;
case engine_preferences_changed:
evaluate(calculatorEventData.getSequenceId());
break;
} }
} }

View File

@ -14,6 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed; import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed; import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.manual_calculation_requested;
/** /**
* User: Solovyev_S * User: Solovyev_S
@ -179,7 +180,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorEventType calculatorEventType, @NotNull CalculatorEventType calculatorEventType,
@Nullable Object data) { @Nullable Object data) {
if (calculatorEventType.isOfType(editor_state_changed, display_state_changed)) { if (calculatorEventType.isOfType(editor_state_changed, display_state_changed, manual_calculation_requested)) {
if (calculatorEventData.isAfter(this.lastEventDataId)) { if (calculatorEventData.isAfter(this.lastEventDataId)) {
final boolean sameSequence = calculatorEventData.isSameSequence(this.lastEventDataId); final boolean sameSequence = calculatorEventData.isSameSequence(this.lastEventDataId);
@ -188,6 +189,9 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
this.lastEventDataId = calculatorEventData; this.lastEventDataId = calculatorEventData;
switch (calculatorEventType) { switch (calculatorEventType) {
case manual_calculation_requested:
lastEditorViewState = (CalculatorEditorViewState) data;
break;
case editor_state_changed: case editor_state_changed:
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data; final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
lastEditorViewState = editorChangeData.getNewState(); lastEditorViewState = editorChangeData.getNewState();

View File

@ -118,6 +118,11 @@ public class AndroidCalculator implements Calculator {
calculator.evaluate(); calculator.evaluate();
} }
@Override
public void evaluate(@NotNull Long sequenceId) {
calculator.evaluate(sequenceId);
}
@Override @Override
public void simplify() { public void simplify() {
calculator.simplify(); calculator.simplify();

View File

@ -186,9 +186,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
} }
getEngine().softReset();
initMultiplicationButton(); initMultiplicationButton();
fixThemeParameters(true); fixThemeParameters(true);

View File

@ -39,7 +39,7 @@ import java.util.List;
* Time: 11:38 PM * Time: 11:38 PM
*/ */
public class AndroidCalculatorEngine implements CalculatorEngine { public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferences.OnSharedPreferenceChangeListener {
private static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator"; private static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator";
@ -104,6 +104,9 @@ public class AndroidCalculatorEngine implements CalculatorEngine {
public AndroidCalculatorEngine(@NotNull Application application) { public AndroidCalculatorEngine(@NotNull Application application) {
this.context = application; this.context = application;
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
this.lock = new Object(); this.lock = new Object();
final JsclMathEngine engine = JsclMathEngine.instance; final JsclMathEngine engine = JsclMathEngine.instance;
@ -274,4 +277,12 @@ public class AndroidCalculatorEngine implements CalculatorEngine {
return calculatorEngine.getMultiplicationSign(); return calculatorEngine.getMultiplicationSign();
} }
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if ( Preferences.getPreferenceKeys().contains(key) ) {
this.softReset();
}
}
} }