From d665a4f01688e09eecacfc815bd58f2c57bd09f4 Mon Sep 17 00:00:00 2001 From: "Yevhen Babiichuk (DustDFG)" Date: Fri, 9 Jan 2026 12:11:48 +0200 Subject: [PATCH] Convert newpipe/util/NewPipeTextViewHelper to kotlin --- .../newpipe/util/NewPipeTextViewHelper.java | 61 ------------------- .../newpipe/util/NewPipeTextViewHelper.kt | 60 ++++++++++++++++++ 2 files changed, 60 insertions(+), 61 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java create mode 100644 app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.kt diff --git a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java deleted file mode 100644 index cf1a9a03a..000000000 --- a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.schabi.newpipe.util; - -import android.content.Context; -import android.text.Selection; -import android.text.Spannable; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.schabi.newpipe.util.external_communication.ShareUtils; -import org.schabi.newpipe.views.NewPipeEditText; -import org.schabi.newpipe.views.NewPipeTextView; - -public final class NewPipeTextViewHelper { - private NewPipeTextViewHelper() { - } - - /** - * Share the selected text of {@link NewPipeTextView NewPipeTextViews} and - * {@link NewPipeEditText NewPipeEditTexts} with - * {@link ShareUtils#shareText(Context, String, String)}. - * - *

- * This allows EMUI users to get the Android share sheet instead of the EMUI share sheet when - * using the {@code Share} command of the popup menu which appears when selecting text. - *

- * - * @param textView the {@link TextView} on which sharing the selected text. It should be a - * {@link NewPipeTextView} or a {@link NewPipeEditText} (even if - * {@link TextView standard TextViews} are supported). - */ - public static void shareSelectedTextWithShareUtils(@NonNull final TextView textView) { - final CharSequence textViewText = textView.getText(); - shareSelectedTextIfNotNullAndNotEmpty(textView, getSelectedText(textView, textViewText)); - if (textViewText instanceof Spannable) { - Selection.setSelection((Spannable) textViewText, textView.getSelectionEnd()); - } - } - - @Nullable - private static CharSequence getSelectedText(@NonNull final TextView textView, - @Nullable final CharSequence text) { - if (!textView.hasSelection() || text == null) { - return null; - } - - final int start = textView.getSelectionStart(); - final int end = textView.getSelectionEnd(); - return String.valueOf(start > end ? text.subSequence(end, start) - : text.subSequence(start, end)); - } - - private static void shareSelectedTextIfNotNullAndNotEmpty( - @NonNull final TextView textView, - @Nullable final CharSequence selectedText) { - if (selectedText != null && selectedText.length() != 0) { - ShareUtils.shareText(textView.getContext(), "", selectedText.toString()); - } - } -} diff --git a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.kt b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.kt new file mode 100644 index 000000000..33046c967 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.kt @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: 2021-2026 NewPipe contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package org.schabi.newpipe.util + +import android.text.Selection +import android.text.Spannable +import android.widget.TextView +import org.schabi.newpipe.util.external_communication.ShareUtils + +object NewPipeTextViewHelper { + /** + * Share the selected text of [NewPipeTextViews][org.schabi.newpipe.views.NewPipeTextView] and + * [NewPipeEditTexts][org.schabi.newpipe.views.NewPipeEditText] with + * [ShareUtils.shareText]. + * + * + * + * This allows EMUI users to get the Android share sheet instead of the EMUI share sheet when + * using the `Share` command of the popup menu which appears when selecting text. + * + * + * @param textView the [TextView] on which sharing the selected text. It should be a + * [org.schabi.newpipe.views.NewPipeTextView] or a [org.schabi.newpipe.views.NewPipeEditText] + * (even if [standard TextViews][TextView] are supported). + */ + @JvmStatic + fun shareSelectedTextWithShareUtils(textView: TextView) { + val textViewText = textView.getText() + shareSelectedTextIfNotNullAndNotEmpty(textView, getSelectedText(textView, textViewText)) + if (textViewText is Spannable) { + Selection.setSelection(textViewText, textView.selectionEnd) + } + } + + private fun getSelectedText(textView: TextView, text: CharSequence?): CharSequence? { + if (!textView.hasSelection() || text == null) { + return null + } + + val start = textView.selectionStart + val end = textView.selectionEnd + return if (start > end) { + text.subSequence(end, start) + } else { + text.subSequence(start, end) + } + } + + private fun shareSelectedTextIfNotNullAndNotEmpty( + textView: TextView, + selectedText: CharSequence? + ) { + if (!selectedText.isNullOrEmpty()) { + ShareUtils.shareText(textView.getContext(), "", selectedText.toString()) + } + } +}