From 16ec24e1b8d5e4c94d9c13275db02ec51966e180 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 22 Feb 2016 17:37:17 +0100 Subject: [PATCH] Fixed issues with selection --- .../org/solovyev/android/calculator/Editor.java | 10 +++++++--- .../solovyev/android/calculator/EditorView.java | 5 +++-- .../org/solovyev/android/views/Adjuster.java | 17 +++++++++++------ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index 71adbea7..b8c984c4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -22,6 +22,8 @@ package org.solovyev.android.calculator; +import static java.lang.Math.min; + import android.content.SharedPreferences; import com.squareup.otto.Bus; @@ -36,8 +38,6 @@ import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; -import static java.lang.Math.min; - @Singleton public class Editor { @@ -167,7 +167,8 @@ public class Editor { if (selection <= 0 || text.length() <= 0 || selection > text.length()) { return state; } - final String newText = text.substring(0, selection - 1) + text.substring(selection, text.length()); + final String newText = text.substring(0, selection - 1) + text.substring(selection, + text.length()); return onTextChanged(EditorState.create(newText, selection - 1)); } @@ -215,6 +216,9 @@ public class Editor { @Nonnull public EditorState setSelection(int selection) { Check.isMainThread(); + if (state.selection == selection) { + return state; + } return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text))); } diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorView.java b/app/src/main/java/org/solovyev/android/calculator/EditorView.java index 0a996d85..0f2be980 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorView.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -26,6 +26,8 @@ import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.text.Editable; +import android.text.Selection; +import android.text.Spannable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.ContextMenu; @@ -95,8 +97,8 @@ public class EditorView extends EditTextCompat { } else { setText(state.text, BufferType.EDITABLE); } - setSelection(Editor.clamp(state.selection, length())); reportChanges = true; + setSelection(Editor.clamp(state.selection, length())); } private boolean isFloatingCalculator() { @@ -117,7 +119,6 @@ public class EditorView extends EditTextCompat { return; } if (editor == null) { - Check.shouldNotHappen(); return; } editor.setSelection(start); diff --git a/app/src/main/java/org/solovyev/android/views/Adjuster.java b/app/src/main/java/org/solovyev/android/views/Adjuster.java index 7f906221..63097ca4 100644 --- a/app/src/main/java/org/solovyev/android/views/Adjuster.java +++ b/app/src/main/java/org/solovyev/android/views/Adjuster.java @@ -5,6 +5,7 @@ import static android.graphics.Matrix.MSCALE_Y; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.view.ViewCompat; import android.util.TypedValue; import android.view.View; import android.view.ViewTreeObserver; @@ -52,7 +53,6 @@ public class Adjuster { private final TextView view; private final float percentage; private final float minTextSizePxs; - private int lastHeight; public TextViewAdjuster(@NonNull TextView view, float percentage, float minTextSizePxs) { this.view = view; @@ -63,10 +63,13 @@ public class Adjuster { @Override public boolean onPreDraw() { final int height = view.getHeight(); - if (lastHeight == height || height <= 0) { + if (!ViewCompat.isLaidOut(view) || height <= 0) { return true; } - lastHeight = height; + final ViewTreeObserver treeObserver = getTreeObserver(view); + if (treeObserver != null) { + treeObserver.removeOnPreDrawListener(this); + } final float oldTextSize = Math.round(view.getTextSize()); final float newTextSize = Math.max(minTextSizePxs, Math.round(height * percentage)); if (oldTextSize == newTextSize) { @@ -81,7 +84,6 @@ public class Adjuster { @NonNull private final ImageView view; private final float percentage; - private int lastHeight; public ImageViewAdjuster(@NonNull ImageView view, float percentage) { this.view = view; @@ -91,10 +93,13 @@ public class Adjuster { @Override public boolean onPreDraw() { final int height = view.getHeight(); - if (lastHeight == height || height <= 0) { + if (!ViewCompat.isLaidOut(view) || height <= 0) { return true; } - lastHeight = height; + final ViewTreeObserver treeObserver = getTreeObserver(view); + if (treeObserver != null) { + treeObserver.removeOnPreDrawListener(this); + } final Drawable d = view.getDrawable(); if (d == null) { return true;