copy comments on long press

This commit is contained in:
Bnyro 2022-10-12 19:56:31 +02:00
parent 0dbe63f35f
commit ca82121d19
3 changed files with 29 additions and 11 deletions

View File

@ -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<com.github.libretube.api.obj.Comment>) {
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
}
}
}

View File

@ -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()

View File

@ -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)
}
}