Convert ClipboardHelper to an object.

Also improve some related code.
This commit is contained in:
Isira Seneviratne 2023-02-01 20:16:10 +05:30
parent 75a3f3e53b
commit b2b444d3e5
4 changed files with 8 additions and 16 deletions

View File

@ -3,15 +3,12 @@ package com.github.libretube.helpers
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import androidx.core.content.getSystemService
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
object ClipboardHelper {
fun save(context: Context, text: String) {
val clip = ClipData.newPlainText(context.getString(R.string.copied), text)
clipboard.setPrimaryClip(clip)
context.getSystemService<ClipboardManager>()!!.setPrimaryClip(clip)
}
}

View File

@ -1,8 +1,5 @@
package com.github.libretube.ui.activities
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
@ -15,6 +12,7 @@ import com.github.libretube.constants.PIPED_GITHUB_URL
import com.github.libretube.constants.WEBLATE_URL
import com.github.libretube.constants.WEBSITE_URL
import com.github.libretube.databinding.ActivityAboutBinding
import com.github.libretube.helpers.ClipboardHelper
import com.github.libretube.helpers.IntentHelper
import com.github.libretube.ui.base.BaseActivity
import com.google.android.material.card.MaterialCardView
@ -75,10 +73,7 @@ class AboutActivity : BaseActivity() {
private fun onLongClick(href: String) {
// copy the link to the clipboard
val clipboard: ClipboardManager =
getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(getString(R.string.copied), href)
clipboard.setPrimaryClip(clip)
ClipboardHelper.save(this, href)
// show the snackBar with open action
Snackbar.make(
binding.root,

View File

@ -113,7 +113,7 @@ class CommentsAdapter(
}
root.setOnLongClickListener {
ClipboardHelper(root.context).save(comment.commentText ?: "")
ClipboardHelper.save(root.context, comment.commentText ?: "")
Toast.makeText(root.context, R.string.copied, Toast.LENGTH_SHORT).show()
true
}

View File

@ -22,7 +22,7 @@ class ErrorDialog : DialogFragment() {
.setMessage(errorLog)
.setNegativeButton(R.string.okay, null)
.setPositiveButton(R.string.copy) { _, _ ->
ClipboardHelper(requireContext()).save(errorLog)
ClipboardHelper.save(requireContext(), errorLog)
Toast.makeText(context, R.string.copied, Toast.LENGTH_SHORT).show()
}
.show()