1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

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
public boolean onTextContextMenuItem(final int id) {
final Spannable text = getText();
if (id == android.R.id.shareText) {
if (text != null) {
final String selectedText = getSelectedText(text).toString();
if (!selectedText.isEmpty()) {
ShareUtils.shareText(getContext(), "", selectedText);
final Spannable text = getText();
final CharSequence selectedText = getSelectedText(text);
if (selectedText != null && selectedText.length() != 0) {
ShareUtils.shareText(getContext(), "", selectedText.toString());
}
Selection.setSelection(text, getSelectionEnd());
}
return true;
} else {
return super.onTextContextMenuItem(id);
}
}
@NonNull
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) {
int min = 0;
int max = charSequence.length();
@Nullable
private CharSequence getSelectedText(@Nullable final CharSequence text) {
if (!hasSelection() || text == null) {
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
public boolean onTextContextMenuItem(final int id) {
final CharSequence text = getText();
if (id == android.R.id.shareText) {
final String selectedText = getSelectedText(text).toString();
if (!selectedText.isEmpty()) {
ShareUtils.shareText(getContext(), "", selectedText);
final CharSequence text = getText();
final CharSequence selectedText = getSelectedText(text);
if (selectedText != null && selectedText.length() != 0) {
ShareUtils.shareText(getContext(), "", selectedText.toString());
}
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
Selection.setSelection(spannable, getSelectionEnd());
@ -51,18 +51,15 @@ public class NewPipeTextView extends AppCompatTextView {
}
}
@NonNull
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) {
int min = 0;
int max = charSequence.length();
@Nullable
private CharSequence getSelectedText(@Nullable final CharSequence text) {
if (!hasSelection() || text == null) {
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));
}
}