From b2b444d3e5d31d6625c8206225bd14e3193d8174 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 1 Feb 2023 20:16:10 +0530 Subject: [PATCH] Convert ClipboardHelper to an object. Also improve some related code. --- .../com/github/libretube/helpers/ClipboardHelper.kt | 11 ++++------- .../github/libretube/ui/activities/AboutActivity.kt | 9 ++------- .../github/libretube/ui/adapters/CommentsAdapter.kt | 2 +- .../com/github/libretube/ui/dialogs/ErrorDialog.kt | 2 +- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/ClipboardHelper.kt b/app/src/main/java/com/github/libretube/helpers/ClipboardHelper.kt index 791b8d3a9..fd20a9039 100644 --- a/app/src/main/java/com/github/libretube/helpers/ClipboardHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ClipboardHelper.kt @@ -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()!!.setPrimaryClip(clip) } } diff --git a/app/src/main/java/com/github/libretube/ui/activities/AboutActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/AboutActivity.kt index 61a80bdd5..c9e2aecc9 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/AboutActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/AboutActivity.kt @@ -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, 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 38b6df834..c06319679 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 @@ -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 } 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 8d3de5b60..6a71f24f9 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 @@ -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()