Improve code of created views

Use the same logic as Android TextViews
This commit is contained in:
TiA4f8R 2021-09-18 14:14:36 +02:00
parent c8802fe5d0
commit 3ded6feddb
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
2 changed files with 27 additions and 35 deletions

View File

@ -37,33 +37,28 @@ public class NewPipeEditText extends AppCompatEditText {
@Override @Override
public boolean onTextContextMenuItem(final int id) { public boolean onTextContextMenuItem(final int id) {
final Spannable text = getText();
if (id == android.R.id.shareText) { if (id == android.R.id.shareText) {
if (text != null) { final Spannable text = getText();
final String selectedText = getSelectedText(text).toString(); final CharSequence selectedText = getSelectedText(text);
if (!selectedText.isEmpty()) { if (selectedText != null && selectedText.length() != 0) {
ShareUtils.shareText(getContext(), "", selectedText); ShareUtils.shareText(getContext(), "", selectedText.toString());
}
Selection.setSelection(text, getSelectionEnd());
} }
Selection.setSelection(text, getSelectionEnd());
return true; return true;
} else { } else {
return super.onTextContextMenuItem(id); return super.onTextContextMenuItem(id);
} }
} }
@NonNull @Nullable
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) { private CharSequence getSelectedText(@Nullable final CharSequence text) {
int min = 0; if (!hasSelection() || text == null) {
int max = charSequence.length(); return null;
if (isFocused()) {
final int selStart = getSelectionStart();
final int selEnd = getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
} }
return charSequence.subSequence(min, max);
final int start = getSelectionStart();
final int end = getSelectionEnd();
return String.valueOf(start > end ? text.subSequence(end, start)
: text.subSequence(start, end));
} }
} }

View File

@ -37,11 +37,11 @@ public class NewPipeTextView extends AppCompatTextView {
@Override @Override
public boolean onTextContextMenuItem(final int id) { public boolean onTextContextMenuItem(final int id) {
final CharSequence text = getText();
if (id == android.R.id.shareText) { if (id == android.R.id.shareText) {
final String selectedText = getSelectedText(text).toString(); final CharSequence text = getText();
if (!selectedText.isEmpty()) { final CharSequence selectedText = getSelectedText(text);
ShareUtils.shareText(getContext(), "", selectedText); if (selectedText != null && selectedText.length() != 0) {
ShareUtils.shareText(getContext(), "", selectedText.toString());
} }
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
Selection.setSelection(spannable, getSelectionEnd()); Selection.setSelection(spannable, getSelectionEnd());
@ -51,18 +51,15 @@ public class NewPipeTextView extends AppCompatTextView {
} }
} }
@NonNull @Nullable
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) { private CharSequence getSelectedText(@Nullable final CharSequence text) {
int min = 0; if (!hasSelection() || text == null) {
int max = charSequence.length(); return null;
if (isFocused()) {
final int selStart = getSelectionStart();
final int selEnd = getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
} }
return charSequence.subSequence(min, max);
final int start = getSelectionStart();
final int end = getSelectionEnd();
return String.valueOf(start > end ? text.subSequence(end, start)
: text.subSequence(start, end));
} }
} }