From d1c068edff8dbfef0c393d24adc3bc68a86758a3 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 7 Jan 2016 21:36:01 +0100 Subject: [PATCH] EditorView refactored --- .../AndroidCalculatorEditorView.java | 179 ------------------ .../android/calculator/CalculatorEditor.java | 32 ++-- .../CalculatorEditorChangeEventData.java | 18 +- .../calculator/CalculatorEditorFragment.java | 5 +- .../calculator/CalculatorEditorImpl.java | 76 ++++---- .../calculator/CalculatorEditorView.java | 35 ---- .../calculator/CalculatorEditorViewState.java | 43 ----- .../android/calculator/CalculatorImpl.java | 28 ++- .../calculator/CalculatorKeyboardImpl.java | 2 +- .../solovyev/android/calculator/Editor.java | 43 ----- ...torViewStateImpl.java => EditorState.java} | 35 ++-- .../android/calculator/EditorView.java | 157 +++++++++++++++ .../history/CalculatorHistoryImpl.java | 26 +-- .../history/CalculatorHistoryState.java | 6 +- .../history/EditorHistoryState.java | 4 +- .../history/TextViewEditorAdapter.java | 68 ------- .../onscreen/CalculatorOnscreenView.java | 20 +- .../calculator/view/LongClickEraser.java | 4 +- .../calculator/widget/CalculatorWidget.java | 12 +- .../main/res/layout-large/cpp_app_editor.xml | 2 +- .../main/res/layout-xlarge/cpp_app_editor.xml | 2 +- app/src/main/res/layout/cpp_app_editor.xml | 2 +- .../main/res/layout/cpp_app_editor_mobile.xml | 2 +- app/src/main/res/layout/onscreen_editor.xml | 2 +- .../main/res/layout/onscreen_editor_light.xml | 2 +- app/src/main/res/values/styles.xml | 1 + .../AndroidCalculatorEditorViewTest.java | 2 +- .../calculator/CalculatorTestUtils.java | 2 +- ...tateImplTest.java => EditorStateTest.java} | 6 +- .../history/CalculatorHistoryImplTest.java | 4 +- .../calculator/history/HistoryUtilsTest.java | 11 +- 31 files changed, 295 insertions(+), 536 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorEditorView.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewState.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/Editor.java rename app/src/main/java/org/solovyev/android/calculator/{CalculatorEditorViewStateImpl.java => EditorState.java} (58%) create mode 100644 app/src/main/java/org/solovyev/android/calculator/EditorView.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/history/TextViewEditorAdapter.java rename app/src/test/java/org/solovyev/android/calculator/{CalculatorEditorViewStateImplTest.java => EditorStateTest.java} (79%) diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java deleted file mode 100644 index 956398bb..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorEditorView.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import android.content.Context; -import android.os.Build; -import android.os.Handler; -import android.text.Editable; -import android.text.TextWatcher; -import android.util.AttributeSet; -import android.view.ContextMenu; -import android.widget.EditText; - -import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; -import org.solovyev.common.collections.Collections; - -import javax.annotation.Nonnull; - -/** - * User: serso - * Date: 9/17/11 - * Time: 12:25 AM - */ -public class AndroidCalculatorEditorView extends EditText implements CalculatorEditorView { - - @Nonnull - private final Handler uiHandler = new Handler(); - private volatile boolean initialized = false; - @SuppressWarnings("UnusedDeclaration") - @Nonnull - private volatile CalculatorEditorViewState viewState = CalculatorEditorViewStateImpl.newDefaultInstance(); - private volatile boolean viewStateChange = false; - - public AndroidCalculatorEditorView(Context context) { - super(context); - } - - public AndroidCalculatorEditorView(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public AndroidCalculatorEditorView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @Override - public boolean onCheckIsTextEditor() { - // NOTE: code below can be used carefully and should not be copied without special intention - // The main purpose of code is to disable soft input (virtual keyboard) but leave all the TextEdit functionality, like cursor, scrolling, copy/paste menu etc - - if (Build.VERSION.SDK_INT >= 11) { - // fix for missing cursor in android 3 and higher - try { - // IDEA: return false always except if method was called from TextView.isCursorVisible() method - for (StackTraceElement stackTraceElement : Collections.asList(Thread.currentThread().getStackTrace())) { - if ("isCursorVisible".equals(stackTraceElement.getMethodName())) { - return true; - } - } - } catch (RuntimeException e) { - // just in case... - } - - return false; - } else { - return false; - } - } - - @Override - protected void onCreateContextMenu(ContextMenu menu) { - super.onCreateContextMenu(menu); - - menu.removeItem(android.R.id.selectAll); - } - - public void setHighlightText(boolean highlightText) { - //this.highlightText = highlightText; - Locator.getInstance().getEditor().updateViewState(); - } - - public synchronized void init() { - if (!initialized) { - this.addTextChangedListener(new TextWatcherImpl()); - - initialized = true; - } - } - - @Override - public void setState(@Nonnull final CalculatorEditorViewState viewState) { - synchronized (this) { - - uiHandler.post(new Runnable() { - @Override - public void run() { - final AndroidCalculatorEditorView editorView = AndroidCalculatorEditorView.this; - synchronized (AndroidCalculatorEditorView.this) { - try { - editorView.viewStateChange = true; - editorView.viewState = viewState; - if (App.getTheme().light && getContext() instanceof CalculatorOnscreenService) { - // don't need formatting - editorView.setText(viewState.getText()); - } else { - editorView.setText(viewState.getTextAsCharSequence(), BufferType.EDITABLE); - } - final int selection = CalculatorEditorImpl.correctSelection(viewState.getSelection(), editorView.getText()); - editorView.setSelection(selection); - } finally { - editorView.viewStateChange = false; - } - } - } - }); - } - } - - @Override - protected void onSelectionChanged(int selStart, int selEnd) { - synchronized (this) { - if (initialized && !viewStateChange) { - // external text change => need to notify editor - super.onSelectionChanged(selStart, selEnd); - - if (selStart == selEnd) { - // only if cursor moving, if selection do nothing - Locator.getInstance().getEditor().setSelection(selStart); - } - } - } - } - - public void handleTextChange(Editable s) { - synchronized (this) { - if (initialized && !viewStateChange) { - // external text change => need to notify editor - Locator.getInstance().getEditor().setText(String.valueOf(s)); - } - } - } - - private final class TextWatcherImpl implements TextWatcher { - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - handleTextChange(s); - } - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java index 508c6249..04f34c11 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java @@ -36,14 +36,14 @@ public interface CalculatorEditor extends CalculatorEventListener { @Nonnull String TAG = CalculatorEditor.class.getSimpleName(); - void setView(@Nonnull CalculatorEditorView view); + void setView(@Nonnull EditorView view); - void clearView(@Nonnull CalculatorEditorView view); + void clearView(@Nonnull EditorView view); @Nonnull - CalculatorEditorViewState getViewState(); + EditorState getViewState(); - void setViewState(@Nonnull CalculatorEditorViewState viewState); + void setViewState(@Nonnull EditorState viewState); // updates state of view (view.setState()) void updateViewState(); @@ -60,25 +60,25 @@ public interface CalculatorEditor extends CalculatorEventListener { * Method sets the cursor to the beginning */ @Nonnull - public CalculatorEditorViewState setCursorOnStart(); + EditorState setCursorOnStart(); /** * Method sets the cursor to the end */ @Nonnull - public CalculatorEditorViewState setCursorOnEnd(); + EditorState setCursorOnEnd(); /** * Method moves cursor to the left of current position */ @Nonnull - public CalculatorEditorViewState moveCursorLeft(); + EditorState moveCursorLeft(); /** * Method moves cursor to the right of current position */ @Nonnull - public CalculatorEditorViewState moveCursorRight(); + EditorState moveCursorRight(); @Nonnull CursorControl asCursorControl(); @@ -92,26 +92,26 @@ public interface CalculatorEditor extends CalculatorEventListener { ********************************************************************** */ @Nonnull - CalculatorEditorViewState erase(); + EditorState erase(); @Nonnull - CalculatorEditorViewState clear(); + EditorState clear(); @Nonnull - CalculatorEditorViewState setText(@Nonnull String text); + EditorState setText(@Nonnull String text); @Nonnull - CalculatorEditorViewState setText(@Nonnull String text, int selection); + EditorState setText(@Nonnull String text, int selection); @Nonnull - CalculatorEditorViewState insert(@Nonnull String text); + EditorState insert(@Nonnull String text); @Nonnull - CalculatorEditorViewState insert(@Nonnull String text, int selectionOffset); + EditorState insert(@Nonnull String text, int selectionOffset); @Nonnull - CalculatorEditorViewState moveSelection(int offset); + EditorState moveSelection(int offset); @Nonnull - CalculatorEditorViewState setSelection(int selection); + EditorState setSelection(int selection); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorChangeEventData.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorChangeEventData.java index ff3c4c80..dba17ed6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorChangeEventData.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorChangeEventData.java @@ -29,34 +29,34 @@ import javax.annotation.Nonnull; * Date: 21.09.12 * Time: 13:46 */ -public final class CalculatorEditorChangeEventData implements Change { +public final class CalculatorEditorChangeEventData implements Change { @Nonnull - private CalculatorEditorViewState oldState; + private EditorState oldState; @Nonnull - private CalculatorEditorViewState newState; + private EditorState newState; - private CalculatorEditorChangeEventData(@Nonnull CalculatorEditorViewState oldState, - @Nonnull CalculatorEditorViewState newState) { + private CalculatorEditorChangeEventData(@Nonnull EditorState oldState, + @Nonnull EditorState newState) { this.oldState = oldState; this.newState = newState; } - public static CalculatorEditorChangeEventData newChangeEventData(@Nonnull CalculatorEditorViewState oldState, - @Nonnull CalculatorEditorViewState newState) { + public static CalculatorEditorChangeEventData newChangeEventData(@Nonnull EditorState oldState, + @Nonnull EditorState newState) { return new CalculatorEditorChangeEventData(oldState, newState); } @Nonnull @Override - public CalculatorEditorViewState getOldValue() { + public EditorState getOldValue() { return this.oldState; } @Nonnull @Override - public CalculatorEditorViewState getNewValue() { + public EditorState getNewValue() { return this.newState; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java index 72473461..18ab0d7b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java @@ -54,7 +54,7 @@ public class CalculatorEditorFragment extends Fragment { private ActivityMenu menu = ListActivityMenu.fromEnum(CalculatorMenu.class, AndroidMenuHelper.getInstance()); @Nonnull - private AndroidCalculatorEditorView editorView; + private EditorView editorView; public CalculatorEditorFragment() { } @@ -65,8 +65,7 @@ public class CalculatorEditorFragment extends Fragment { fragmentUi.onViewCreated(this, view); - editorView = (AndroidCalculatorEditorView) view.findViewById(R.id.calculator_editor); - editorView.init(); + editorView = (EditorView) view.findViewById(R.id.calculator_editor); Locator.getInstance().getEditor().setView(editorView); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java index 6364ede9..03eaf171 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java @@ -55,9 +55,9 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nullable private final TextProcessor textProcessor; @Nullable - private CalculatorEditorView view; + private EditorView view; @Nonnull - private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance(); + private EditorState lastViewState = EditorState.empty(); public CalculatorEditorImpl(@Nonnull Calculator calculator, @Nullable TextProcessor textProcessor) { this.calculator = calculator; @@ -66,18 +66,16 @@ public class CalculatorEditorImpl implements CalculatorEditor { this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); } - public static int correctSelection(int selection, @Nonnull CharSequence text) { - return correctSelection(selection, text.length()); + public static int clamp(int selection, @Nonnull CharSequence text) { + return clamp(selection, text.length()); } - public static int correctSelection(int selection, int textLength) { - int result = Math.max(selection, 0); - result = min(result, textLength); - return result; + public static int clamp(int selection, int max) { + return min(Math.max(selection, 0), max); } @Override - public void setView(@Nonnull CalculatorEditorView view) { + public void setView(@Nonnull EditorView view) { synchronized (viewLock) { this.view = view; this.view.setState(lastViewState); @@ -85,7 +83,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { } @Override - public void clearView(@Nonnull CalculatorEditorView view) { + public void clearView(@Nonnull EditorView view) { synchronized (viewLock) { if (this.view == view) { this.view = null; @@ -95,12 +93,12 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState getViewState() { + public EditorState getViewState() { return lastViewState; } @Override - public void setViewState(@Nonnull CalculatorEditorViewState newViewState) { + public void setViewState(@Nonnull EditorState newViewState) { setViewState(newViewState, true); } @@ -109,17 +107,17 @@ public class CalculatorEditorImpl implements CalculatorEditor { setViewState(this.lastViewState, false); } - private void setViewState(@Nonnull CalculatorEditorViewState newViewState, boolean majorChanges) { + private void setViewState(@Nonnull EditorState newViewState, boolean majorChanges) { if (textProcessor != null) { try { final TextProcessorEditorResult result = textProcessor.process(newViewState.getText()); - newViewState = CalculatorEditorViewStateImpl.newInstance(result.getCharSequence(), newViewState.getSelection() + result.getOffset()); + newViewState = EditorState.create(result.getCharSequence(), newViewState.getSelection() + result.getOffset()); } catch (CalculatorParseException e) { Locator.getInstance().getLogger().error(TAG, e.getMessage(), e); } } synchronized (viewLock) { - final CalculatorEditorViewState oldViewState = this.lastViewState; + final EditorState oldViewState = this.lastViewState; this.lastViewState = newViewState; if (this.view != null) { @@ -138,7 +136,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { ********************************************************************** */ - private void fireStateChangedEvent(boolean majorChanges, @Nonnull CalculatorEditorViewState oldViewState, @Nonnull CalculatorEditorViewState newViewState) { + private void fireStateChangedEvent(boolean majorChanges, @Nonnull EditorState oldViewState, @Nonnull EditorState newViewState) { if (!Thread.holdsLock(viewLock)) throw new AssertionError(); if (majorChanges) { @@ -166,9 +164,9 @@ public class CalculatorEditorImpl implements CalculatorEditor { } @Nonnull - private CalculatorEditorViewState newSelectionViewState(int newSelection) { + private EditorState newSelectionViewState(int newSelection) { if (this.lastViewState.getSelection() != newSelection) { - final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection); + final EditorState result = EditorState.newSelection(this.lastViewState, newSelection); setViewState(result, false); return result; } else { @@ -177,21 +175,21 @@ public class CalculatorEditorImpl implements CalculatorEditor { } @Nonnull - public CalculatorEditorViewState setCursorOnStart() { + public EditorState setCursorOnStart() { synchronized (viewLock) { return newSelectionViewState(0); } } @Nonnull - public CalculatorEditorViewState setCursorOnEnd() { + public EditorState setCursorOnEnd() { synchronized (viewLock) { return newSelectionViewState(this.lastViewState.getText().length()); } } @Nonnull - public CalculatorEditorViewState moveCursorLeft() { + public EditorState moveCursorLeft() { synchronized (viewLock) { if (this.lastViewState.getSelection() > 0) { return newSelectionViewState(this.lastViewState.getSelection() - 1); @@ -210,7 +208,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { */ @Nonnull - public CalculatorEditorViewState moveCursorRight() { + public EditorState moveCursorRight() { synchronized (viewLock) { if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) { return newSelectionViewState(this.lastViewState.getSelection() + 1); @@ -228,7 +226,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState erase() { + public EditorState erase() { synchronized (viewLock) { int selection = this.lastViewState.getSelection(); final String text = this.lastViewState.getText(); @@ -236,7 +234,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { final StringBuilder newText = new StringBuilder(text.length() - 1); newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length())); - final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1); + final EditorState result = EditorState.create(newText.toString(), selection - 1); setViewState(result); return result; } else { @@ -247,7 +245,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState clear() { + public EditorState clear() { synchronized (viewLock) { return setText(""); } @@ -255,9 +253,9 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState setText(@Nonnull String text) { + public EditorState setText(@Nonnull String text) { synchronized (viewLock) { - final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length()); + final EditorState result = EditorState.create(text, text.length()); setViewState(result); return result; } @@ -265,11 +263,11 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState setText(@Nonnull String text, int selection) { + public EditorState setText(@Nonnull String text, int selection) { synchronized (viewLock) { - selection = correctSelection(selection, text); + selection = clamp(selection, text); - final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection); + final EditorState result = EditorState.create(text, selection); setViewState(result); return result; } @@ -277,7 +275,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState insert(@Nonnull String text) { + public EditorState insert(@Nonnull String text) { synchronized (viewLock) { return insert(text, 0); } @@ -285,10 +283,10 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState insert(@Nonnull String text, int selectionOffset) { + public EditorState insert(@Nonnull String text, int selectionOffset) { synchronized (viewLock) { final String oldText = lastViewState.getText(); - final int selection = correctSelection(lastViewState.getSelection(), oldText); + final int selection = clamp(lastViewState.getSelection(), oldText); int newTextLength = text.length() + oldText.length(); final StringBuilder newText = new StringBuilder(newTextLength); @@ -297,8 +295,8 @@ public class CalculatorEditorImpl implements CalculatorEditor { newText.append(text); newText.append(oldText.substring(selection)); - int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength); - final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection); + int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength); + final EditorState result = EditorState.create(newText.toString(), newSelection); setViewState(result); return result; } @@ -306,7 +304,7 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState moveSelection(int offset) { + public EditorState moveSelection(int offset) { synchronized (viewLock) { int selection = this.lastViewState.getSelection() + offset; @@ -316,11 +314,11 @@ public class CalculatorEditorImpl implements CalculatorEditor { @Nonnull @Override - public CalculatorEditorViewState setSelection(int selection) { + public EditorState setSelection(int selection) { synchronized (viewLock) { - selection = correctSelection(selection, this.lastViewState.getText()); + selection = clamp(selection, this.lastViewState.getText()); - final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection); + final EditorState result = EditorState.newSelection(this.lastViewState, selection); setViewState(result, false); return result; } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorView.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorView.java deleted file mode 100644 index 3b4d8783..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorView.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nonnull; - -/** - * User: Solovyev_S - * Date: 21.09.12 - * Time: 11:48 - */ -public interface CalculatorEditorView { - - void setState(@Nonnull CalculatorEditorViewState viewState); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewState.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewState.java deleted file mode 100644 index 42f92428..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewState.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import java.io.Serializable; - -import javax.annotation.Nonnull; - -/** - * User: Solovyev_S - * Date: 21.09.12 - * Time: 11:48 - */ -public interface CalculatorEditorViewState extends Serializable { - - @Nonnull - String getText(); - - @Nonnull - CharSequence getTextAsCharSequence(); - - int getSelection(); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 5a9ca3db..2cee7b35 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -22,6 +22,14 @@ package org.solovyev.android.calculator; +import jscl.AbstractJsclArithmeticException; +import jscl.NumeralBase; +import jscl.NumeralBaseException; +import jscl.math.Generic; +import jscl.math.function.Function; +import jscl.math.function.IConstant; +import jscl.math.operator.Operator; +import jscl.text.ParseInterruptedException; import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; @@ -37,24 +45,14 @@ import org.solovyev.common.text.Strings; import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.Conversions; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.AbstractJsclArithmeticException; -import jscl.NumeralBase; -import jscl.NumeralBaseException; -import jscl.math.Generic; -import jscl.math.function.Function; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; -import jscl.text.ParseInterruptedException; - /** * User: Solovyev_S * Date: 20.09.12 @@ -175,21 +173,21 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Override public void evaluate() { - final CalculatorEditorViewState viewState = getEditor().getViewState(); + final EditorState viewState = getEditor().getViewState(); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId()); } @Override public void evaluate(@Nonnull Long sequenceId) { - final CalculatorEditorViewState viewState = getEditor().getViewState(); + final EditorState viewState = getEditor().getViewState(); fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId); this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId); } @Override public void simplify() { - final CalculatorEditorViewState viewState = getEditor().getViewState(); + final EditorState viewState = getEditor().getViewState(); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId()); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java index 479b3814..eef5ec8a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java @@ -111,7 +111,7 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard { @Override public void roundBracketsButtonPressed() { final CalculatorEditor editor = Locator.getInstance().getEditor(); - CalculatorEditorViewState viewState = editor.getViewState(); + EditorState viewState = editor.getViewState(); final int cursorPosition = viewState.getSelection(); final String oldText = viewState.getText(); diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java deleted file mode 100644 index 8fae3b52..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 12/17/11 - * Time: 9:37 PM - */ -public interface Editor { - - @Nullable - CharSequence getText(); - - void setText(@Nullable CharSequence text); - - int getSelection(); - - void setSelection(int selection); - -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewStateImpl.java b/app/src/main/java/org/solovyev/android/calculator/EditorState.java similarity index 58% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewStateImpl.java rename to app/src/main/java/org/solovyev/android/calculator/EditorState.java index 7d21ab2b..0b1dfd54 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorViewStateImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorState.java @@ -23,62 +23,53 @@ package org.solovyev.android.calculator; import javax.annotation.Nonnull; +import java.io.Serializable; -/** - * User: Solovyev_S - * Date: 21.09.12 - * Time: 12:02 - */ -public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState { +public class EditorState implements Serializable { @Nonnull private CharSequence text = ""; private int selection = 0; - private CalculatorEditorViewStateImpl() { + private EditorState() { } - public CalculatorEditorViewStateImpl(@Nonnull CalculatorEditorViewState viewState) { - this.text = viewState.getText(); - this.selection = viewState.getSelection(); + public EditorState(@Nonnull EditorState state) { + this.text = state.getText(); + this.selection = state.getSelection(); } @Nonnull - public static CalculatorEditorViewState newDefaultInstance() { - return new CalculatorEditorViewStateImpl(); + public static EditorState empty() { + return new EditorState(); } @Nonnull - public static CalculatorEditorViewState newSelection(@Nonnull CalculatorEditorViewState viewState, int newSelection) { - final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl(viewState); - - result.selection = newSelection; - + public static EditorState newSelection(@Nonnull EditorState state, int selection) { + final EditorState result = new EditorState(state); + result.selection = selection; return result; } @Nonnull - public static CalculatorEditorViewState newInstance(@Nonnull CharSequence text, int selection) { - final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl(); + public static EditorState create(@Nonnull CharSequence text, int selection) { + final EditorState result = new EditorState(); result.text = text; result.selection = selection; return result; } @Nonnull - @Override public String getText() { return this.text.toString(); } @Nonnull - @Override public CharSequence getTextAsCharSequence() { return this.text; } - @Override public int getSelection() { return this.selection; } diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorView.java b/app/src/main/java/org/solovyev/android/calculator/EditorView.java new file mode 100644 index 00000000..4f0e5661 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -0,0 +1,157 @@ +/* + * Copyright 2013 serso aka se.solovyev + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Contact details + * + * Email: se.solovyev@gmail.com + * Site: http://se.solovyev.org + */ + +package org.solovyev.android.calculator; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.AttributeSet; +import android.util.Log; +import android.view.ContextMenu; +import android.widget.EditText; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.lang.reflect.Method; + +public class EditorView extends EditText { + + @Nonnull + private static final String TAG = App.subTag("EditorView"); + + private boolean initialized; + @Nullable + private Method setShowSoftInputOnFocusMethod; + private boolean externalChange; + + public EditorView(Context context) { + super(context); + init(); + } + + public EditorView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public EditorView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public EditorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(); + } + + private void init() { + Check.isTrue(!initialized); + addTextChangedListener(new MyTextWatcher()); + setShowSoftInputOnFocusCompat(false); + initialized = true; + } + + @Override + protected void onCreateContextMenu(ContextMenu menu) { + super.onCreateContextMenu(menu); + menu.removeItem(android.R.id.selectAll); + } + + public void setState(@Nonnull final EditorState state) { + Check.isMainThread(); + try { + externalChange = true; + if (App.getTheme().light && getContext() instanceof CalculatorOnscreenService) { + // don't need formatting + setText(state.getText()); + } else { + setText(state.getTextAsCharSequence(), BufferType.EDITABLE); + } + final int selection = CalculatorEditorImpl.clamp(state.getSelection(), length()); + setSelection(selection); + } finally { + externalChange = false; + } + } + + @Override + protected void onSelectionChanged(int start, int end) { + Check.isMainThread(); + if (!initialized || externalChange) { + return; + } + // external text change => need to notify editor + super.onSelectionChanged(start, end); + + if (start == end) { + // only if cursor moving, if selection do nothing + Locator.getInstance().getEditor().setSelection(start); + } + } + + public void setShowSoftInputOnFocusCompat(boolean show) { + Check.isMainThread(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setShowSoftInputOnFocus(show); + } else { + setShowSoftInputOnFocusPreLollipop(show); + } + } + + private void setShowSoftInputOnFocusPreLollipop(boolean show) { + try { + if (setShowSoftInputOnFocusMethod == null) { + setShowSoftInputOnFocusMethod = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class); + setShowSoftInputOnFocusMethod.setAccessible(true); + } + setShowSoftInputOnFocusMethod.invoke(this, show); + } catch (Exception e) { + Log.w(TAG, e.getMessage(), e); + } + } + + private class MyTextWatcher implements TextWatcher { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + if (!initialized || externalChange) { + return; + } + // external text change => need to notify editor + Locator.getInstance().getEditor().setText(String.valueOf(s)); + } + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java index fec202f1..ac2f057d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java @@ -22,32 +22,20 @@ package org.solovyev.android.calculator.history; -import org.solovyev.android.calculator.Calculator; -import org.solovyev.android.calculator.CalculatorDisplayChangeEventData; -import org.solovyev.android.calculator.CalculatorDisplayViewState; -import org.solovyev.android.calculator.CalculatorEditorChangeEventData; -import org.solovyev.android.calculator.CalculatorEditorViewState; -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventHolder; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorUtils; -import org.solovyev.android.calculator.Locator; +import org.solovyev.android.calculator.*; import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryHelper; import org.solovyev.common.history.SimpleHistoryHelper; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -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.manual_calculation_requested; +import static org.solovyev.android.calculator.CalculatorEventType.*; /** * User: Solovyev_S @@ -68,7 +56,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); @Nullable - private volatile CalculatorEditorViewState lastEditorViewState; + private volatile EditorState lastEditorViewState; public CalculatorHistoryImpl(@Nonnull Calculator calculator) { calculator.addCalculatorEventListener(this); @@ -267,7 +255,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { if (result.isNewAfter() && result.isNewSameOrAfterSequence()) { switch (calculatorEventType) { case manual_calculation_requested: - lastEditorViewState = (CalculatorEditorViewState) data; + lastEditorViewState = (EditorState) data; break; case editor_state_changed: final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data; @@ -276,7 +264,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { case display_state_changed: if (result.isSameSequence()) { if (lastEditorViewState != null) { - final CalculatorEditorViewState editorViewState = lastEditorViewState; + final EditorState editorViewState = lastEditorViewState; final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data; final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue(); addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState)); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java index 5aca99c9..72760cd4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java @@ -27,7 +27,7 @@ import org.simpleframework.xml.Root; import org.solovyev.android.calculator.CalculatorDisplay; import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorEditor; -import org.solovyev.android.calculator.CalculatorEditorViewState; +import org.solovyev.android.calculator.EditorState; import javax.annotation.Nonnull; @@ -61,14 +61,14 @@ public class CalculatorHistoryState extends AbstractHistoryState { @Nonnull public static CalculatorHistoryState newInstance(@Nonnull CalculatorEditor editor, @Nonnull CalculatorDisplay display) { - final CalculatorEditorViewState editorViewState = editor.getViewState(); + final EditorState editorViewState = editor.getViewState(); final CalculatorDisplayViewState displayViewState = display.getViewState(); return newInstance(editorViewState, displayViewState); } @Nonnull - public static CalculatorHistoryState newInstance(@Nonnull CalculatorEditorViewState editorViewState, + public static CalculatorHistoryState newInstance(@Nonnull EditorState editorViewState, @Nonnull CalculatorDisplayViewState displayViewState) { final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java index f8aa74ea..ff847f56 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java @@ -26,7 +26,7 @@ import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorEditor; -import org.solovyev.android.calculator.CalculatorEditorViewState; +import org.solovyev.android.calculator.EditorState; import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; @@ -47,7 +47,7 @@ public class EditorHistoryState implements Cloneable { } @Nonnull - public static EditorHistoryState newInstance(@Nonnull CalculatorEditorViewState viewState) { + public static EditorHistoryState newInstance(@Nonnull EditorState viewState) { final EditorHistoryState result = new EditorHistoryState(); result.text = String.valueOf(viewState.getText()); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/TextViewEditorAdapter.java b/app/src/main/java/org/solovyev/android/calculator/history/TextViewEditorAdapter.java deleted file mode 100644 index ea172449..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/history/TextViewEditorAdapter.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.history; - -import android.widget.EditText; -import android.widget.TextView; - -import org.solovyev.android.calculator.Editor; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 12/17/11 - * Time: 9:39 PM - */ -public class TextViewEditorAdapter implements Editor { - - @Nonnull - private final TextView textView; - - public TextViewEditorAdapter(@Nonnull TextView textView) { - this.textView = textView; - } - - @Override - public CharSequence getText() { - return textView.getText().toString(); - } - - @Override - public void setText(@Nullable CharSequence text) { - textView.setText(text); - } - - @Override - public int getSelection() { - return textView.getSelectionStart(); - } - - @Override - public void setSelection(int selection) { - if (textView instanceof EditText) { - ((EditText) textView).setSelection(selection); - } - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java index 11f158c2..3dcd4c41 100644 --- a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java +++ b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java @@ -35,21 +35,12 @@ import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; - -import org.solovyev.android.calculator.AndroidCalculatorDisplayView; -import org.solovyev.android.calculator.AndroidCalculatorEditorView; -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorButton; -import org.solovyev.android.calculator.CalculatorDisplayViewState; -import org.solovyev.android.calculator.CalculatorEditorViewState; -import org.solovyev.android.calculator.Preferences; -import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.*; import org.solovyev.android.prefs.Preference; -import java.util.Locale; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Locale; /** * User: serso @@ -100,7 +91,7 @@ public class CalculatorOnscreenView { private Drawable headerTitleDrawable; @Nonnull - private AndroidCalculatorEditorView editorView; + private EditorView editorView; @Nonnull private AndroidCalculatorDisplayView displayView; @@ -202,7 +193,7 @@ public class CalculatorOnscreenView { ********************************************************************** */ - public void updateEditorState(@Nonnull CalculatorEditorViewState editorState) { + public void updateEditorState(@Nonnull EditorState editorState) { checkInit(); editorView.setState(editorState); } @@ -253,8 +244,7 @@ public class CalculatorOnscreenView { displayView = (AndroidCalculatorDisplayView) root.findViewById(R.id.calculator_display); displayView.init(this.context, false); - editorView = (AndroidCalculatorEditorView) root.findViewById(R.id.calculator_editor); - editorView.init(); + editorView = (EditorView) root.findViewById(R.id.calculator_editor); final View onscreenFoldButton = root.findViewById(R.id.onscreen_fold_button); onscreenFoldButton.setOnClickListener(new View.OnClickListener() { diff --git a/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java b/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java index bfa694d4..96d3ba8d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java @@ -6,7 +6,7 @@ import android.view.View; import org.solovyev.android.calculator.Calculator; import org.solovyev.android.calculator.CalculatorEditor; -import org.solovyev.android.calculator.CalculatorEditorViewState; +import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.Locator; import javax.annotation.Nonnull; @@ -69,7 +69,7 @@ public final class LongClickEraser implements View.OnTouchListener { @Override public void run() { - final CalculatorEditorViewState state = editor.erase(); + final EditorState state = editor.erase(); if (isEmpty(state.getText())) { stop(); return; diff --git a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java index cbe8ceeb..b1b23d07 100644 --- a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java +++ b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java @@ -30,13 +30,16 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.res.Resources; +import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; +import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.SpannedString; import android.text.TextUtils; +import android.text.style.StyleSpan; import android.widget.RemoteViews; import org.solovyev.android.Views; import org.solovyev.android.calculator.*; @@ -78,7 +81,10 @@ public class CalculatorWidget extends AppWidgetProvider { @Nonnull private SpannedString initCursorString(@Nonnull Context context) { if (cursorString == null) { - cursorString = new SpannedString(App.colorString("|", ContextCompat.getColor(context, R.color.cpp_widget_cursor))); + final SpannableString s = App.colorString("|", ContextCompat.getColor(context, R.color.cpp_widget_cursor)); + // this will override any other style span (f.e. italic) + s.setSpan(new StyleSpan(Typeface.NORMAL), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); + cursorString = new SpannedString(s); } return cursorString; } @@ -101,7 +107,7 @@ public class CalculatorWidget extends AppWidgetProvider { private void updateWidget(@Nonnull Context context, @Nonnull AppWidgetManager manager, @Nonnull int[] widgetIds) { - final CalculatorEditorViewState editorState = Locator.getInstance().getEditor().getViewState(); + final EditorState editorState = Locator.getInstance().getEditor().getViewState(); final CalculatorDisplayViewState displayState = Locator.getInstance().getDisplay().getViewState(); final Resources resources = context.getResources(); @@ -197,7 +203,7 @@ public class CalculatorWidget extends AppWidgetProvider { views.setTextColor(R.id.calculator_display, ContextCompat.getColor(context, theme.getDisplayTextColor(error))); } - private void updateEditorState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorEditorViewState editorState, @Nonnull SimpleTheme theme) { + private void updateEditorState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull EditorState editorState, @Nonnull SimpleTheme theme) { final CharSequence text = editorState.getTextAsCharSequence(); final boolean unspan = App.getTheme().light != theme.light; diff --git a/app/src/main/res/layout-large/cpp_app_editor.xml b/app/src/main/res/layout-large/cpp_app_editor.xml index 5956fb30..9716b95e 100644 --- a/app/src/main/res/layout-large/cpp_app_editor.xml +++ b/app/src/main/res/layout-large/cpp_app_editor.xml @@ -34,7 +34,7 @@ a:layout_width="match_parent" a:layout_height="wrap_content" /> - - - - - diff --git a/app/src/main/res/layout/onscreen_editor_light.xml b/app/src/main/res/layout/onscreen_editor_light.xml index 6daf07b7..bf97f6a0 100644 --- a/app/src/main/res/layout/onscreen_editor_light.xml +++ b/app/src/main/res/layout/onscreen_editor_light.xml @@ -14,7 +14,7 @@ a:layout_height="match_parent" a:padding="@dimen/cpp_editor_padding"> - diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 48b01e39..06e0e99a 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -61,6 +61,7 @@ fill_parent @dimen/cpp_editor_text_size ?attr/cpp_text_color + textMultiLine|textNoSuggestions