feat: 优化代码

pull/111/head
kunfei 5 years ago
parent cf0792529c
commit 7f192df83f
  1. 17
      app/src/main/java/io/legado/app/ui/about/AboutFragment.kt
  2. 17
      app/src/main/java/io/legado/app/ui/about/DonateFragment.kt
  3. 10
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 19
      app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt
  5. 14
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -1,8 +1,5 @@
package io.legado.app.ui.about package io.legado.app.ui.about
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
@ -14,6 +11,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.sendToClip
import io.legado.app.utils.toast import io.legado.app.utils.toast
class AboutFragment : PreferenceFragmentCompat() { class AboutFragment : PreferenceFragmentCompat() {
@ -52,7 +50,7 @@ class AboutFragment : PreferenceFragmentCompat() {
"license" -> openIntent(Intent.ACTION_VIEW, licenseUrl) "license" -> openIntent(Intent.ACTION_VIEW, licenseUrl)
"disclaimer" -> openIntent(Intent.ACTION_VIEW, disclaimerUrl) "disclaimer" -> openIntent(Intent.ACTION_VIEW, disclaimerUrl)
"qq" -> showQqGroups() "qq" -> showQqGroups()
"gzGzh" -> sendToClip("开源阅读软件") "gzGzh" -> requireContext().sendToClip("开源阅读软件")
} }
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
} }
@ -86,7 +84,7 @@ class AboutFragment : PreferenceFragmentCompat() {
items(names) { _, index -> items(names) { _, index ->
qqGroups[names[index]]?.let { qqGroups[names[index]]?.let {
if (!joinQQGroup(it)) { 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)
}
}
} }

@ -1,7 +1,5 @@
package io.legado.app.ui.about package io.legado.app.ui.about
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
@ -11,6 +9,7 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.ACache import io.legado.app.utils.ACache
import io.legado.app.utils.sendToClip
import io.legado.app.utils.toast import io.legado.app.utils.toast
import org.jetbrains.anko.longToast import org.jetbrains.anko.longToast
import java.net.URLEncoder import java.net.URLEncoder
@ -39,7 +38,7 @@ class DonateFragment : PreferenceFragmentCompat() {
"qqSkRwm" -> openIntent(Intent.ACTION_VIEW, qqSkRwmUrl) "qqSkRwm" -> openIntent(Intent.ACTION_VIEW, qqSkRwmUrl)
"zfbSk" -> aliDonate(requireContext()) "zfbSk" -> aliDonate(requireContext())
"zfbHbSsm" -> getZfbHb(requireContext()) "zfbHbSsm" -> getZfbHb(requireContext())
"gzGzh" -> sendToClip("开源阅读软件") "gzGzh" -> requireContext().sendToClip("开源阅读软件")
} }
return super.onPreferenceTreeClick(preference) 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) { private fun getZfbHb(context: Context) {
sendToClip("537954522") requireContext().sendToClip("537954522")
context.longToast("高级功能已开启\n红包码已复制\n支付宝首页搜索“537954522” 立即领红包") context.longToast("高级功能已开启\n红包码已复制\n支付宝首页搜索“537954522” 立即领红包")
try { try {
val packageManager = context.applicationContext.packageManager val packageManager = context.applicationContext.packageManager

@ -7,6 +7,7 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.view.* import android.view.*
import androidx.appcompat.view.menu.MenuItemImpl
import androidx.core.view.get import androidx.core.view.get
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.size import androidx.core.view.size
@ -384,6 +385,15 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
} }
override fun getSelectedText(): String {
return page_view.selectedText
}
override fun onMenuItemSelected(item: MenuItemImpl): Boolean {
return false
}
/** /**
* 音量键翻页 * 音量键翻页
*/ */

@ -13,12 +13,13 @@ import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter 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.item_fillet_text.view.*
import kotlinx.android.synthetic.main.popup_action_menu.view.* import kotlinx.android.synthetic.main.popup_action_menu.view.*
import org.jetbrains.anko.sdk27.listeners.onClick 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) { PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
init { init {
@ -55,17 +56,25 @@ class TextActionMenu(context: Context, callBack: CallBack) :
} }
override fun registerListener(holder: ItemViewHolder) { override fun registerListener(holder: ItemViewHolder) {
holder.itemView.apply { holder.itemView.onClick {
onClick {
getItem(holder.layoutPosition)?.let { 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
} }
} }

@ -1,9 +1,7 @@
package io.legado.app.utils package io.legado.app.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.*
import android.content.Intent
import android.content.IntentFilter
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Bitmap 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 { fun Context.sysIsDarkMode(): Boolean {
val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return mode == Configuration.UI_MODE_NIGHT_YES return mode == Configuration.UI_MODE_NIGHT_YES

Loading…
Cancel
Save