Merge the Share process of the two classes into one

A new class has been added in the util package: NewPipeTextViewHelper.
It shares the selected text of a TextView with ShareUtils#shareText (with the created shareSelectedTextWithShareUtils static method).
Only this static method can be used by other classes, other methods are private.
This commit is contained in:
TiA4f8R 2021-09-24 20:14:31 +02:00
parent 3ded6feddb
commit aab09c0c65
No known key found for this signature in database
GPG key ID: E6D3E7F5949450DD
3 changed files with 94 additions and 47 deletions

View file

@ -1,8 +1,6 @@
package org.schabi.newpipe.views;
import android.content.Context;
import android.text.Selection;
import android.text.Spannable;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
@ -11,6 +9,8 @@ import androidx.appcompat.widget.AppCompatEditText;
import org.schabi.newpipe.util.external_communication.ShareUtils;
import static org.schabi.newpipe.util.NewPipeTextViewHelper.shareSelectedTextWithShareUtils;
/**
* An {@link AppCompatEditText} which uses {@link ShareUtils#shareText(Context, String, String)}
* when sharing selected text by using the {@code Share} command of the floating actions.
@ -38,27 +38,8 @@ public class NewPipeEditText extends AppCompatEditText {
@Override
public boolean onTextContextMenuItem(final int id) {
if (id == android.R.id.shareText) {
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);
return shareSelectedTextWithShareUtils(this);
}
}
@Nullable
private CharSequence getSelectedText(@Nullable final CharSequence text) {
if (!hasSelection() || text == null) {
return null;
}
final int start = getSelectionStart();
final int end = getSelectionEnd();
return String.valueOf(start > end ? text.subSequence(end, start)
: text.subSequence(start, end));
return super.onTextContextMenuItem(id);
}
}

View file

@ -1,8 +1,6 @@
package org.schabi.newpipe.views;
import android.content.Context;
import android.text.Selection;
import android.text.Spannable;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
@ -11,6 +9,8 @@ import androidx.appcompat.widget.AppCompatTextView;
import org.schabi.newpipe.util.external_communication.ShareUtils;
import static org.schabi.newpipe.util.NewPipeTextViewHelper.shareSelectedTextWithShareUtils;
/**
* An {@link AppCompatTextView} which uses {@link ShareUtils#shareText(Context, String, String)}
* when sharing selected text by using the {@code Share} command of the floating actions.
@ -38,28 +38,8 @@ public class NewPipeTextView extends AppCompatTextView {
@Override
public boolean onTextContextMenuItem(final int id) {
if (id == android.R.id.shareText) {
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());
return true;
} else {
return super.onTextContextMenuItem(id);
return shareSelectedTextWithShareUtils(this);
}
}
@Nullable
private CharSequence getSelectedText(@Nullable final CharSequence text) {
if (!hasSelection() || text == null) {
return null;
}
final int start = getSelectionStart();
final int end = getSelectionEnd();
return String.valueOf(start > end ? text.subSequence(end, start)
: text.subSequence(start, end));
return super.onTextContextMenuItem(id);
}
}