Fix for losing editor state

This commit is contained in:
serso 2016-03-01 12:34:14 +01:00
parent 0b4318f05f
commit 7585aaa2fb

View File

@ -26,11 +26,10 @@ import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.text.Editable; import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.ContextMenu; import android.view.ContextMenu;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.floating.FloatingCalculatorService; import org.solovyev.android.calculator.floating.FloatingCalculatorService;
import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.android.calculator.view.EditTextCompat;
@ -41,7 +40,7 @@ import javax.annotation.Nullable;
public class EditorView extends EditTextCompat { public class EditorView extends EditTextCompat {
private boolean reportChanges; private boolean editorChange;
@Nullable @Nullable
private Editor editor; private Editor editor;
@ -73,12 +72,18 @@ public class EditorView extends EditTextCompat {
} }
addTextChangedListener(new MyTextWatcher()); addTextChangedListener(new MyTextWatcher());
dontShowSoftInputOnFocusCompat(); dontShowSoftInputOnFocusCompat();
// changes should only be reported after the view has been set up completely, i.e. now // the state is controlled by Editor
reportChanges = true; setSaveEnabled(false);
} }
public void setEditor(@Nullable Editor editor) { public void setEditor(@Nullable Editor editor) {
if (this.editor == editor) {
return;
}
this.editor = editor; this.editor = editor;
if (editor != null) {
setState(editor.getState());
}
} }
@Override @Override
@ -90,14 +95,14 @@ public class EditorView extends EditTextCompat {
public void setState(@Nonnull final EditorState state) { public void setState(@Nonnull final EditorState state) {
Check.isMainThread(); Check.isMainThread();
// we don't want to be notified about changes we make ourselves // we don't want to be notified about changes we make ourselves
reportChanges = false; editorChange = true;
if (App.getTheme().light && isFloatingCalculator()) { if (App.getTheme().light && isFloatingCalculator()) {
// don't need formatting // don't need formatting
setText(state.getTextString()); setText(state.getTextString());
} else { } else {
setText(state.text, BufferType.EDITABLE); setText(state.text, BufferType.EDITABLE);
} }
reportChanges = true; editorChange = false;
setSelection(Editor.clamp(state.selection, length())); setSelection(Editor.clamp(state.selection, length()));
} }
@ -108,7 +113,7 @@ public class EditorView extends EditTextCompat {
@Override @Override
protected void onSelectionChanged(int start, int end) { protected void onSelectionChanged(int start, int end) {
Check.isMainThread(); Check.isMainThread();
if (!reportChanges) { if (!editorChange) {
return; return;
} }
// external text change => need to notify editor // external text change => need to notify editor
@ -118,7 +123,7 @@ public class EditorView extends EditTextCompat {
if (start != end) { if (start != end) {
return; return;
} }
if (editor == null) { if (editor == null || editorChange) {
return; return;
} }
editor.setSelection(start); editor.setSelection(start);
@ -135,12 +140,7 @@ public class EditorView extends EditTextCompat {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
// external text change => need to notify editor if (editor == null || editorChange) {
if (!reportChanges) {
return;
}
if (editor == null) {
Check.shouldNotHappen();
return; return;
} }
editor.setText(String.valueOf(s)); editor.setText(String.valueOf(s));