Editor refactor

This commit is contained in:
serso 2016-01-10 12:27:21 +01:00
parent b120283adb
commit bf24414690

View File

@ -37,28 +37,6 @@ import static java.lang.Math.min;
public class Editor implements CalculatorEventListener { public class Editor implements CalculatorEventListener {
private static final String TAG = App.subTag("Editor"); private static final String TAG = App.subTag("Editor");
public static class ChangedEvent {
@Nonnull
public final EditorState oldState;
@Nonnull
public final EditorState newState;
private ChangedEvent(@Nonnull EditorState oldState, @Nonnull EditorState newState) {
this.oldState = oldState;
this.newState = newState;
}
}
public static class CursorMovedEvent {
@Nonnull
public final EditorState state;
public CursorMovedEvent(@Nonnull EditorState state) {
this.state = state;
}
}
@Nonnull @Nonnull
private final CalculatorEventHolder lastEventHolder; private final CalculatorEventHolder lastEventHolder;
@Nullable @Nullable
@ -67,7 +45,6 @@ public class Editor implements CalculatorEventListener {
private EditorView view; private EditorView view;
@Nonnull @Nonnull
private EditorState state = EditorState.empty(); private EditorState state = EditorState.empty();
public Editor(@Nonnull Calculator calculator, @Nullable TextProcessor<TextProcessorEditorResult, String> textProcessor) { public Editor(@Nonnull Calculator calculator, @Nullable TextProcessor<TextProcessorEditorResult, String> textProcessor) {
this.textProcessor = textProcessor; this.textProcessor = textProcessor;
calculator.addCalculatorEventListener(this); calculator.addCalculatorEventListener(this);
@ -100,7 +77,8 @@ public class Editor implements CalculatorEventListener {
return state; return state;
} }
public void onTextChanged(@Nonnull EditorState newState) { @Nonnull
public EditorState onTextChanged(@Nonnull EditorState newState) {
Check.isMainThread(); Check.isMainThread();
if (textProcessor != null) { if (textProcessor != null) {
try { try {
@ -116,15 +94,18 @@ public class Editor implements CalculatorEventListener {
view.setState(newState); view.setState(newState);
} }
App.getBus().post(new ChangedEvent(oldState, newState)); App.getBus().post(new ChangedEvent(oldState, newState));
return state;
} }
private void onSelectionChanged(@Nonnull EditorState newState) { @Nonnull
private EditorState onSelectionChanged(@Nonnull EditorState newState) {
Check.isMainThread(); Check.isMainThread();
state = newState; state = newState;
if (view != null) { if (view != null) {
view.setState(newState); view.setState(newState);
} }
App.getBus().post(new CursorMovedEvent(newState)); App.getBus().post(new CursorMovedEvent(newState));
return state;
} }
@Override @Override
@ -148,13 +129,10 @@ public class Editor implements CalculatorEventListener {
@Nonnull @Nonnull
private EditorState newSelectionViewState(int newSelection) { private EditorState newSelectionViewState(int newSelection) {
Check.isMainThread(); Check.isMainThread();
if (state.selection != newSelection) { if (state.selection == newSelection) {
final EditorState newState = EditorState.forNewSelection(state, newSelection);
onSelectionChanged(newState);
return newState;
} else {
return state; return state;
} }
return onSelectionChanged(EditorState.forNewSelection(state, newSelection));
} }
@Nonnull @Nonnull
@ -172,35 +150,31 @@ public class Editor implements CalculatorEventListener {
@Nonnull @Nonnull
public EditorState moveCursorLeft() { public EditorState moveCursorLeft() {
Check.isMainThread(); Check.isMainThread();
if (state.selection > 0) { if (state.selection <= 0) {
return newSelectionViewState(state.selection - 1);
} else {
return state; return state;
} }
return newSelectionViewState(state.selection - 1);
} }
@Nonnull @Nonnull
public EditorState moveCursorRight() { public EditorState moveCursorRight() {
Check.isMainThread(); Check.isMainThread();
if (state.selection < state.text.length()) { if (state.selection >= state.text.length()) {
return newSelectionViewState(state.selection + 1);
} else {
return state; return state;
} }
return newSelectionViewState(state.selection + 1);
} }
@Nonnull @Nonnull
public EditorState erase() { public EditorState erase() {
Check.isMainThread(); Check.isMainThread();
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()) {
final EditorState newState = EditorState.create(text.substring(0, selection - 1) + text.substring(selection, text.length()), selection - 1);
onTextChanged(newState);
return newState;
} else {
return state; return state;
} }
final String newText = text.substring(0, selection - 1) + text.substring(selection, text.length());
return onTextChanged(EditorState.create(newText, selection - 1));
} }
@Nonnull @Nonnull
@ -212,17 +186,13 @@ public class Editor implements CalculatorEventListener {
@Nonnull @Nonnull
public EditorState setText(@Nonnull String text) { public EditorState setText(@Nonnull String text) {
Check.isMainThread(); Check.isMainThread();
final EditorState result = EditorState.create(text, text.length()); return onTextChanged(EditorState.create(text, text.length()));
onTextChanged(result);
return result;
} }
@Nonnull @Nonnull
public EditorState setText(@Nonnull String text, int selection) { public EditorState setText(@Nonnull String text, int selection) {
Check.isMainThread(); Check.isMainThread();
final EditorState state = EditorState.create(text, clamp(selection, text)); return onTextChanged(EditorState.create(text, clamp(selection, text)));
onTextChanged(state);
return state;
} }
@Nonnull @Nonnull
@ -236,30 +206,42 @@ public class Editor implements CalculatorEventListener {
Check.isMainThread(); Check.isMainThread();
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();
int newTextLength = text.length() + oldText.length(); final int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength);
final String newText = oldText.substring(0, selection) + text + oldText.substring(selection);
int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength); return onTextChanged(EditorState.create(newText, newSelection));
final EditorState newState = EditorState.create(oldText.substring(0, selection) + text + oldText.substring(selection), newSelection);
onTextChanged(newState);
return newState;
} }
@Nonnull @Nonnull
public EditorState moveSelection(int offset) { public EditorState moveSelection(int offset) {
Check.isMainThread(); Check.isMainThread();
int selection = state.selection + offset; return setSelection(state.selection + offset);
return setSelection(selection);
} }
@Nonnull @Nonnull
public EditorState setSelection(int selection) { public EditorState setSelection(int selection) {
Check.isMainThread(); Check.isMainThread();
selection = clamp(selection, state.text); return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text)));
final EditorState result = EditorState.forNewSelection(state, selection);
onSelectionChanged(result);
return result;
} }
public static class ChangedEvent {
@Nonnull
public final EditorState oldState;
@Nonnull
public final EditorState newState;
private ChangedEvent(@Nonnull EditorState oldState, @Nonnull EditorState newState) {
this.oldState = oldState;
this.newState = newState;
}
}
public static class CursorMovedEvent {
@Nonnull
public final EditorState state;
public CursorMovedEvent(@Nonnull EditorState state) {
this.state = state;
}
}
} }