feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 4e5f16dae5
commit 6dbd3e70f4
  1. 29
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/source/manage/DiffCallBack.kt
  3. 8
      app/src/main/java/io/legado/app/utils/ViewExtensions.kt
  4. 4
      app/src/main/res/menu/book_source_item.xml

@ -11,7 +11,6 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.utils.invisible
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.item_book_source.view.* import kotlinx.android.synthetic.main.item_book_source.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
@ -82,20 +81,31 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
iv_menu_more.onClick { iv_menu_more.onClick {
val popupMenu = PopupMenu(context, it) val popupMenu = PopupMenu(context, it)
popupMenu.inflate(R.menu.book_source_item) popupMenu.inflate(R.menu.book_source_item)
val qyMenu = popupMenu.menu.findItem(R.id.menu_enable_explore)
if (item.exploreUrl.isNullOrEmpty()) {
qyMenu.isVisible = false
} else {
if (item.enabledExplore) {
qyMenu.setTitle(R.string.disable_explore)
} else {
qyMenu.setTitle(R.string.enable_explore)
}
}
popupMenu.setOnMenuItemClickListener { menuItem -> popupMenu.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) { when (menuItem.itemId) {
R.id.menu_top -> callBack.toTop(item) R.id.menu_top -> callBack.toTop(item)
R.id.menu_del -> callBack.del(item) R.id.menu_del -> callBack.del(item)
R.id.menu_enable_explore -> {
item.enabledExplore = !item.enabledExplore
callBack.update(item)
iv_explore.visible(item.showExplore())
}
} }
true true
} }
popupMenu.show() popupMenu.show()
} }
if (item.showExplore()) { iv_explore.visible(item.showExplore())
iv_explore.visible()
} else {
iv_explore.invisible()
}
} else { } else {
payload.keySet().map { payload.keySet().map {
when (it) { when (it) {
@ -107,18 +117,13 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup) String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup)
} }
"enabled" -> swt_enabled.isChecked = payload.getBoolean(it) "enabled" -> swt_enabled.isChecked = payload.getBoolean(it)
"showExplore" -> if (payload.getBoolean(it)) { "showExplore" -> iv_explore.visible(payload.getBoolean(it))
iv_explore.visible()
} else {
iv_explore.invisible()
} }
} }
} }
} }
} }
}
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition) val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition) val targetItem = getItem(targetPosition)

@ -32,6 +32,9 @@ class DiffCallBack(
return false return false
if (oldItem.enabled != newItem.enabled) if (oldItem.enabled != newItem.enabled)
return false return false
if (oldItem.showExplore() != newItem.showExplore()) {
return false
}
return true return true
} }

@ -49,6 +49,14 @@ fun View.visible() {
visibility = VISIBLE visibility = VISIBLE
} }
fun View.visible(visible: Boolean) {
visibility = if (visible) {
VISIBLE
} else {
INVISIBLE
}
}
fun View.screenshot(): Bitmap? { fun View.screenshot(): Bitmap? {
return runCatching { return runCatching {
val screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)

@ -9,4 +9,8 @@
android:id="@+id/menu_del" android:id="@+id/menu_del"
android:title="@string/delete" /> android:title="@string/delete" />
<item
android:id="@+id/menu_enable_explore"
android:title="@string/enable_explore" />
</menu> </menu>
Loading…
Cancel
Save