From ca82121d1941ae2729bc6a6bf1c388aa9efa7f38 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 12 Oct 2022 19:56:31 +0200 Subject: [PATCH] copy comments on long press --- .../libretube/ui/adapters/CommentsAdapter.kt | 9 ++++++++- .../github/libretube/ui/dialogs/ErrorDialog.kt | 14 ++++---------- .../github/libretube/util/ClipboardHelper.kt | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/util/ClipboardHelper.kt diff --git a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt index 23577291b..d6ecbc3bc 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt @@ -15,6 +15,7 @@ import com.github.libretube.databinding.CommentsRowBinding import com.github.libretube.extensions.TAG import com.github.libretube.extensions.formatShort import com.github.libretube.ui.viewholders.CommentsViewHolder +import com.github.libretube.util.ClipboardHelper import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper import kotlinx.coroutines.CoroutineScope @@ -30,7 +31,7 @@ class CommentsAdapter( private var isLoading = false private var nextpage = "" - private var repliesPage = com.github.libretube.api.obj.CommentsPage() + private var repliesPage = CommentsPage() fun updateItems(newItems: List) { val commentsSize = comments.size @@ -88,6 +89,12 @@ class CommentsAdapter( else -> repliesAdapter.clear() } } + + root.setOnLongClickListener { + ClipboardHelper(root.context).save(comment.commentText.toString()) + Toast.makeText(root.context, R.string.copied, Toast.LENGTH_SHORT).show() + true + } } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/ErrorDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/ErrorDialog.kt index ffbd54eb7..68a154997 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/ErrorDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/ErrorDialog.kt @@ -1,17 +1,17 @@ package com.github.libretube.ui.dialogs +import android.annotation.SuppressLint import android.app.Dialog -import android.content.ClipData -import android.content.ClipboardManager -import android.content.Context import android.os.Bundle import android.widget.Toast import androidx.fragment.app.DialogFragment import com.github.libretube.R +import com.github.libretube.util.ClipboardHelper import com.github.libretube.util.PreferenceHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder class ErrorDialog : DialogFragment() { + @SuppressLint("PrivateResource") override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val errorLog = PreferenceHelper.getErrorLog() // reset the error log @@ -22,13 +22,7 @@ class ErrorDialog : DialogFragment() { .setMessage(errorLog) .setNegativeButton(R.string.okay, null) .setPositiveButton(R.string.copy) { _, _ -> - /** - * copy the error log to the clipboard - */ - val clipboard: ClipboardManager = - context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager - val clip = ClipData.newPlainText(context?.getString(R.string.copied), errorLog) - clipboard.setPrimaryClip(clip) + ClipboardHelper(requireContext()).save(errorLog) Toast.makeText(context, R.string.copied, Toast.LENGTH_SHORT).show() } .show() diff --git a/app/src/main/java/com/github/libretube/util/ClipboardHelper.kt b/app/src/main/java/com/github/libretube/util/ClipboardHelper.kt new file mode 100644 index 000000000..47ad931ff --- /dev/null +++ b/app/src/main/java/com/github/libretube/util/ClipboardHelper.kt @@ -0,0 +1,17 @@ +package com.github.libretube.util + +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context +import com.github.libretube.R + +class ClipboardHelper( + private val context: Context +) { + fun save(text: String) { + val clipboard: ClipboardManager = + context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + val clip = ClipData.newPlainText(context.getString(R.string.copied), text) + clipboard.setPrimaryClip(clip) + } +}