Avoid returning new state from Editor#onTextChanged

This commit is contained in:
serso 2017-08-02 15:31:09 +02:00
parent 3bc913ac81
commit f4896086fb
2 changed files with 28 additions and 36 deletions

View File

@ -22,11 +22,15 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import static java.lang.Math.min;
import android.app.Application; import android.app.Application;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.text.TextUtils; import android.text.TextUtils;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.history.RecentHistory; import org.solovyev.android.calculator.history.RecentHistory;
@ -40,8 +44,6 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import static java.lang.Math.min;
@Singleton @Singleton
public class Editor { public class Editor {
@ -93,13 +95,11 @@ public class Editor {
return state; return state;
} }
@Nonnull private void onTextChanged(@Nonnull EditorState newState) {
public EditorState onTextChanged(@Nonnull EditorState newState) { onTextChanged(newState, false);
return onTextChanged(newState, false);
} }
@Nonnull private void onTextChanged(@Nonnull EditorState newState, boolean force) {
public EditorState onTextChanged(@Nonnull EditorState newState, boolean force) {
Check.isMainThread(); Check.isMainThread();
if (textProcessor != null) { if (textProcessor != null) {
final TextProcessorEditorResult result = textProcessor.process(newState.getTextString()); final TextProcessorEditorResult result = textProcessor.process(newState.getTextString());
@ -111,7 +111,6 @@ public class Editor {
view.setState(newState); view.setState(newState);
} }
bus.post(new ChangedEvent(oldState, newState, force)); bus.post(new ChangedEvent(oldState, newState, force));
return state;
} }
@Nonnull @Nonnull
@ -125,10 +124,9 @@ public class Editor {
return state; return state;
} }
@Nonnull public void setState(@Nonnull EditorState state) {
public EditorState setState(@Nonnull EditorState state) {
Check.isMainThread(); Check.isMainThread();
return onTextChanged(state); onTextChanged(state);
} }
@Nonnull @Nonnull
@ -170,13 +168,12 @@ public class Editor {
return newSelectionViewState(state.selection + 1); return newSelectionViewState(state.selection + 1);
} }
@Nonnull public boolean erase() {
public EditorState erase() {
Check.isMainThread(); Check.isMainThread();
final int selection = state.selection; final int selection = state.selection;
final String text = state.getTextString(); final String text = state.getTextString();
if (selection <= 0 || text.length() <= 0 || selection > text.length()) { if (selection <= 0 || text.length() <= 0 || selection > text.length()) {
return state; return false;
} }
int removeStart = selection - 1; int removeStart = selection - 1;
if (MathType.getType(text, selection - 1, false, engine).type == MathType.grouping_separator) { if (MathType.getType(text, selection - 1, false, engine).type == MathType.grouping_separator) {
@ -186,45 +183,41 @@ public class Editor {
} }
final String newText = text.substring(0, removeStart) + text.substring(selection, text.length()); final String newText = text.substring(0, removeStart) + text.substring(selection, text.length());
return onTextChanged(EditorState.create(newText, removeStart)); onTextChanged(EditorState.create(newText, removeStart));
return !newText.isEmpty();
} }
@Nonnull public void clear() {
public EditorState clear() {
Check.isMainThread(); Check.isMainThread();
return setText(""); setText("");
} }
@Nonnull public void setText(@Nonnull String text) {
public EditorState setText(@Nonnull String text) {
Check.isMainThread(); Check.isMainThread();
return onTextChanged(EditorState.create(text, text.length())); onTextChanged(EditorState.create(text, text.length()));
} }
@Nonnull public void setText(@Nonnull String text, int selection) {
public EditorState setText(@Nonnull String text, int selection) {
Check.isMainThread(); Check.isMainThread();
return onTextChanged(EditorState.create(text, clamp(selection, text))); onTextChanged(EditorState.create(text, clamp(selection, text)));
} }
@Nonnull public void insert(@Nonnull String text) {
public EditorState insert(@Nonnull String text) {
Check.isMainThread(); Check.isMainThread();
return insert(text, 0); insert(text, 0);
} }
@Nonnull public void insert(@Nonnull String text, int selectionOffset) {
public EditorState insert(@Nonnull String text, int selectionOffset) {
Check.isMainThread(); Check.isMainThread();
if (TextUtils.isEmpty(text) && selectionOffset == 0) { if (TextUtils.isEmpty(text) && selectionOffset == 0) {
return state; return;
} }
final String oldText = state.getTextString(); final String oldText = state.getTextString();
final int selection = clamp(state.selection, oldText); final int selection = clamp(state.selection, oldText);
final int newTextLength = text.length() + oldText.length(); final int newTextLength = text.length() + oldText.length();
final int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength); final int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength);
final String newText = oldText.substring(0, selection) + text + oldText.substring(selection); final String newText = oldText.substring(0, selection) + text + oldText.substring(selection);
return onTextChanged(EditorState.create(newText, newSelection)); onTextChanged(EditorState.create(newText, newSelection));
} }
@Nonnull @Nonnull

View File

@ -1,12 +1,12 @@
package org.solovyev.android.calculator.view; package org.solovyev.android.calculator.view;
import android.view.View; import android.view.View;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.Editor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import static android.text.TextUtils.isEmpty;
public class EditorLongClickEraser extends BaseLongClickEraser { public class EditorLongClickEraser extends BaseLongClickEraser {
@Nonnull @Nonnull
@ -29,8 +29,7 @@ public class EditorLongClickEraser extends BaseLongClickEraser {
} }
protected boolean erase() { protected boolean erase() {
final EditorState state = editor.erase(); return editor.erase();
return !isEmpty(state.text);
} }
@Override @Override