From 7f192df83f10d88a510bd9fb5a7951b14b03b8d2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 22 Feb 2020 15:58:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/about/AboutFragment.kt | 17 +++------------ .../io/legado/app/ui/about/DonateFragment.kt | 17 +++------------ .../app/ui/book/read/ReadBookActivity.kt | 10 +++++++++ .../legado/app/ui/book/read/TextActionMenu.kt | 21 +++++++++++++------ .../io/legado/app/utils/ContextExtensions.kt | 14 ++++++++++--- 5 files changed, 42 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt b/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt index 454fda8e9..8579a0783 100644 --- a/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt +++ b/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt @@ -1,8 +1,5 @@ package io.legado.app.ui.about -import android.content.ClipData -import android.content.ClipboardManager -import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle @@ -14,6 +11,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.lib.dialogs.alert import io.legado.app.ui.widget.dialog.TextDialog +import io.legado.app.utils.sendToClip import io.legado.app.utils.toast class AboutFragment : PreferenceFragmentCompat() { @@ -52,7 +50,7 @@ class AboutFragment : PreferenceFragmentCompat() { "license" -> openIntent(Intent.ACTION_VIEW, licenseUrl) "disclaimer" -> openIntent(Intent.ACTION_VIEW, disclaimerUrl) "qq" -> showQqGroups() - "gzGzh" -> sendToClip("开源阅读软件") + "gzGzh" -> requireContext().sendToClip("开源阅读软件") } return super.onPreferenceTreeClick(preference) } @@ -86,7 +84,7 @@ class AboutFragment : PreferenceFragmentCompat() { items(names) { _, index -> qqGroups[names[index]]?.let { if (!joinQQGroup(it)) { - sendToClip(it) + requireContext().sendToClip(it) } } } @@ -107,13 +105,4 @@ class AboutFragment : PreferenceFragmentCompat() { } } - private fun sendToClip(text: String) { - val clipboard = - requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager - val clipData = ClipData.newPlainText(null, text) - clipboard?.let { - clipboard.setPrimaryClip(clipData) - toast(R.string.copy_complete) - } - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt b/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt index a202946b6..08daf95ab 100644 --- a/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt +++ b/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt @@ -1,7 +1,5 @@ package io.legado.app.ui.about -import android.content.ClipData -import android.content.ClipboardManager import android.content.Context import android.content.Intent import android.net.Uri @@ -11,6 +9,7 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.utils.ACache +import io.legado.app.utils.sendToClip import io.legado.app.utils.toast import org.jetbrains.anko.longToast import java.net.URLEncoder @@ -39,7 +38,7 @@ class DonateFragment : PreferenceFragmentCompat() { "qqSkRwm" -> openIntent(Intent.ACTION_VIEW, qqSkRwmUrl) "zfbSk" -> aliDonate(requireContext()) "zfbHbSsm" -> getZfbHb(requireContext()) - "gzGzh" -> sendToClip("开源阅读软件") + "gzGzh" -> requireContext().sendToClip("开源阅读软件") } return super.onPreferenceTreeClick(preference) } @@ -55,18 +54,8 @@ class DonateFragment : PreferenceFragmentCompat() { } } - private fun sendToClip(text: String) { - val clipboard = - requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager - val clipData = ClipData.newPlainText(null, text) - clipboard?.let { - clipboard.setPrimaryClip(clipData) - toast(R.string.copy_complete) - } - } - private fun getZfbHb(context: Context) { - sendToClip("537954522") + requireContext().sendToClip("537954522") context.longToast("高级功能已开启\n红包码已复制\n支付宝首页搜索“537954522” 立即领红包") try { val packageManager = context.applicationContext.packageManager diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 61beca0e1..5b8d94322 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.os.Bundle import android.os.Handler import android.view.* +import androidx.appcompat.view.menu.MenuItemImpl import androidx.core.view.get import androidx.core.view.isVisible import androidx.core.view.size @@ -384,6 +385,15 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } } + override fun getSelectedText(): String { + return page_view.selectedText + } + + override fun onMenuItemSelected(item: MenuItemImpl): Boolean { + + return false + } + /** * 音量键翻页 */ diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index 2437600ba..506be8653 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -13,12 +13,13 @@ import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.utils.sendToClip import kotlinx.android.synthetic.main.item_fillet_text.view.* import kotlinx.android.synthetic.main.popup_action_menu.view.* import org.jetbrains.anko.sdk27.listeners.onClick -class TextActionMenu(context: Context, callBack: CallBack) : +class TextActionMenu(private val context: Context, private val callBack: CallBack) : PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) { init { @@ -55,17 +56,25 @@ class TextActionMenu(context: Context, callBack: CallBack) : } override fun registerListener(holder: ItemViewHolder) { - holder.itemView.apply { - onClick { - getItem(holder.layoutPosition)?.let { - + holder.itemView.onClick { + getItem(holder.layoutPosition)?.let { + if (!callBack.onMenuItemSelected(it)) { + onMenuItemSelected(it) } } } } } - interface CallBack { + private fun onMenuItemSelected(item: MenuItemImpl) { + when (item.itemId) { + R.id.menu_copy -> context.sendToClip(callBack.getSelectedText()) + } + } + + interface CallBack { + fun onMenuItemSelected(item: MenuItemImpl): Boolean + fun getSelectedText(): String } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 1ad897272..25c74ec23 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -1,9 +1,7 @@ package io.legado.app.utils import android.annotation.SuppressLint -import android.content.Context -import android.content.Intent -import android.content.IntentFilter +import android.content.* import android.content.res.ColorStateList import android.content.res.Configuration import android.graphics.Bitmap @@ -132,6 +130,16 @@ fun Context.shareWithQr(title: String, text: String) { } } +fun Context.sendToClip(text: String) { + val clipboard = + getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager + val clipData = ClipData.newPlainText(null, text) + clipboard?.let { + clipboard.setPrimaryClip(clipData) + toast(R.string.copy_complete) + } +} + fun Context.sysIsDarkMode(): Boolean { val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK return mode == Configuration.UI_MODE_NIGHT_YES