From 6e092253663914d5d03579f74eedc0301c99ad8f Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Mon, 3 Dec 2012 14:38:56 +0400 Subject: [PATCH] user exceptions fixed --- .../AndroidCalculatorEditorView.java | 3 +- .../calculator/CalculatorEditorImpl.java | 4 +- .../history/CalculatorHistoryImpl.java | 48 ++++++++++--------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/android-app-core/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java b/android-app-core/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java index 60a7a0c2..ca7149b8 100644 --- a/android-app-core/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java +++ b/android-app-core/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java @@ -144,7 +144,8 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe editorView.viewStateChange = true; editorView.viewState = viewState; editorView.setText(text, BufferType.EDITABLE); - editorView.setSelection(viewState.getSelection()); + final int selection = CalculatorEditorImpl.correctSelection(viewState.getSelection(), editorView.getText()); + editorView.setSelection(selection); } finally { editorView.viewStateChange = false; } diff --git a/core/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java b/core/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java index 64b344ff..990555a0 100644 --- a/core/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java +++ b/core/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java @@ -269,11 +269,11 @@ public class CalculatorEditorImpl implements CalculatorEditor { } } - private int correctSelection(int selection, @NotNull String text) { + public static int correctSelection(int selection, @NotNull CharSequence text) { return correctSelection(selection, text.length()); } - private int correctSelection(int selection, int textLength) { + public static int correctSelection(int selection, int textLength) { int result = Math.max(selection, 0); result = Math.min(result, textLength); return result; diff --git a/core/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java b/core/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java index 46fe97ad..92e09af0 100644 --- a/core/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java +++ b/core/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java @@ -114,33 +114,35 @@ public class CalculatorHistoryImpl implements CalculatorHistory { @NotNull @Override public List getStates(boolean includeIntermediateStates) { - if (includeIntermediateStates) { - return getStates(); - } else { - final List states = getStates(); + synchronized (history) { + if (includeIntermediateStates) { + return getStates(); + } else { + final List states = getStates(); - final List result = new LinkedList(); + final List result = new LinkedList(); - CalculatorHistoryState laterState = null; - for (CalculatorHistoryState state : CollectionsUtils.reversed(states)) { - if ( laterState != null ) { - final String laterEditorText = laterState.getEditorState().getText(); - final String editorText = state.getEditorState().getText(); - if ( laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) { - // intermediate result => skip from add - } else { - result.add(0, state); - } - } else { - result.add(0, state); - } + CalculatorHistoryState laterState = null; + for (CalculatorHistoryState state : CollectionsUtils.reversed(states)) { + if ( laterState != null ) { + final String laterEditorText = laterState.getEditorState().getText(); + final String editorText = state.getEditorState().getText(); + if ( laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) { + // intermediate result => skip from add + } else { + result.add(0, state); + } + } else { + result.add(0, state); + } - laterState = state; - } + laterState = state; + } - return result; - } - } + return result; + } + } + } private boolean isIntermediate(@NotNull String laterEditorText, @NotNull String editorText) {