mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-04-30 10:41:23 +00:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user