|
@@ -1,12 +1,20 @@
|
|
|
package io.legato.kazusa.ui.book.toc
|
|
package io.legato.kazusa.ui.book.toc
|
|
|
|
|
|
|
|
|
|
+//import io.legado.app.lib.theme.bottomBackground
|
|
|
|
|
+//import io.legado.app.lib.theme.getPrimaryTextColor
|
|
|
import android.annotation.SuppressLint
|
|
import android.annotation.SuppressLint
|
|
|
import android.app.Activity.RESULT_OK
|
|
import android.app.Activity.RESULT_OK
|
|
|
import android.content.Intent
|
|
import android.content.Intent
|
|
|
|
|
+import android.graphics.Canvas
|
|
|
import android.os.Bundle
|
|
import android.os.Bundle
|
|
|
import android.view.View
|
|
import android.view.View
|
|
|
|
|
+import androidx.core.content.ContextCompat
|
|
|
|
|
+import androidx.core.graphics.drawable.toDrawable
|
|
|
import androidx.fragment.app.activityViewModels
|
|
import androidx.fragment.app.activityViewModels
|
|
|
|
|
+import androidx.interpolator.view.animation.FastOutSlowInInterpolator
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
|
+import androidx.recyclerview.widget.ItemTouchHelper
|
|
|
|
|
+import androidx.recyclerview.widget.RecyclerView
|
|
|
import io.legato.kazusa.R
|
|
import io.legato.kazusa.R
|
|
|
import io.legato.kazusa.base.VMBaseFragment
|
|
import io.legato.kazusa.base.VMBaseFragment
|
|
|
import io.legato.kazusa.constant.EventBus
|
|
import io.legato.kazusa.constant.EventBus
|
|
@@ -17,12 +25,15 @@ import io.legato.kazusa.databinding.FragmentChapterListBinding
|
|
|
import io.legato.kazusa.help.book.BookHelp
|
|
import io.legato.kazusa.help.book.BookHelp
|
|
|
import io.legato.kazusa.help.book.isLocal
|
|
import io.legato.kazusa.help.book.isLocal
|
|
|
import io.legato.kazusa.help.book.simulatedTotalChapterNum
|
|
import io.legato.kazusa.help.book.simulatedTotalChapterNum
|
|
|
-//import io.legado.app.lib.theme.bottomBackground
|
|
|
|
|
-//import io.legado.app.lib.theme.getPrimaryTextColor
|
|
|
|
|
|
|
+import io.legato.kazusa.model.CacheBook
|
|
|
import io.legato.kazusa.ui.widget.recycler.UpLinearLayoutManager
|
|
import io.legato.kazusa.ui.widget.recycler.UpLinearLayoutManager
|
|
|
import io.legato.kazusa.ui.widget.recycler.VerticalDivider
|
|
import io.legato.kazusa.ui.widget.recycler.VerticalDivider
|
|
|
|
|
+import io.legato.kazusa.utils.VibrationUtils
|
|
|
import io.legato.kazusa.utils.applyNavigationBarMargin
|
|
import io.legato.kazusa.utils.applyNavigationBarMargin
|
|
|
|
|
+import io.legato.kazusa.utils.dpToPx
|
|
|
import io.legato.kazusa.utils.observeEvent
|
|
import io.legato.kazusa.utils.observeEvent
|
|
|
|
|
+import io.legato.kazusa.utils.themeColor
|
|
|
|
|
+import io.legato.kazusa.utils.toastOnUi
|
|
|
import io.legato.kazusa.utils.viewbindingdelegate.viewBinding
|
|
import io.legato.kazusa.utils.viewbindingdelegate.viewBinding
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.Dispatchers.Default
|
|
import kotlinx.coroutines.Dispatchers.Default
|
|
@@ -30,6 +41,7 @@ import kotlinx.coroutines.Dispatchers.IO
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.launch
|
|
|
import kotlinx.coroutines.withContext
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
+import kotlin.math.min
|
|
|
|
|
|
|
|
class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list),
|
|
class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list),
|
|
|
ChapterListAdapter.Callback,
|
|
ChapterListAdapter.Callback,
|
|
@@ -42,12 +54,6 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
|
|
|
|
|
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run {
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run {
|
|
|
viewModel.chapterListCallBack = this@ChapterListFragment
|
|
viewModel.chapterListCallBack = this@ChapterListFragment
|
|
|
-// val bbg = bottomBackground
|
|
|
|
|
-// val btc = requireContext().getPrimaryTextColor(ColorUtils.isColorLight(bbg))
|
|
|
|
|
-// llChapterBaseInfo.setBackgroundColor(bbg)
|
|
|
|
|
-// tvCurrentChapterInfo.setTextColor(btc)
|
|
|
|
|
-// ivChapterTop.setColorFilter(btc, PorterDuff.Mode.SRC_IN)
|
|
|
|
|
-// ivChapterBottom.setColorFilter(btc, PorterDuff.Mode.SRC_IN)
|
|
|
|
|
initRecyclerView()
|
|
initRecyclerView()
|
|
|
initView()
|
|
initView()
|
|
|
viewModel.bookData.observe(this@ChapterListFragment) {
|
|
viewModel.bookData.observe(this@ChapterListFragment) {
|
|
@@ -56,12 +62,99 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private fun initRecyclerView() {
|
|
private fun initRecyclerView() {
|
|
|
|
|
+ val swipeCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
|
|
|
|
|
+
|
|
|
|
|
+ override fun onMove(
|
|
|
|
|
+ recyclerView: RecyclerView,
|
|
|
|
|
+ viewHolder: RecyclerView.ViewHolder,
|
|
|
|
|
+ target: RecyclerView.ViewHolder
|
|
|
|
|
+ ): Boolean = false
|
|
|
|
|
+
|
|
|
|
|
+ override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float = 0.3f
|
|
|
|
|
+
|
|
|
|
|
+ override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
|
|
|
|
+ val position = viewHolder.bindingAdapterPosition
|
|
|
|
|
+ val chapter = adapter.getItem(position) ?: return
|
|
|
|
|
+ val book = book ?: return
|
|
|
|
|
+
|
|
|
|
|
+ toastOnUi("开始下载: ${adapter.getItem(position)?.title}")
|
|
|
|
|
+ CacheBook.start(requireContext(), book, chapter.index, chapter.index)
|
|
|
|
|
+ VibrationUtils.vibrate(context!!, 100)
|
|
|
|
|
+ adapter.notifyItemChanged(position)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onChildDraw(
|
|
|
|
|
+ c: Canvas,
|
|
|
|
|
+ recyclerView: RecyclerView,
|
|
|
|
|
+ viewHolder: RecyclerView.ViewHolder,
|
|
|
|
|
+ dX: Float,
|
|
|
|
|
+ dY: Float,
|
|
|
|
|
+ actionState: Int,
|
|
|
|
|
+ isCurrentlyActive: Boolean
|
|
|
|
|
+ ) {
|
|
|
|
|
+ if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && dX > 0) {
|
|
|
|
|
+ val itemView = viewHolder.itemView
|
|
|
|
|
+
|
|
|
|
|
+ val interpolator = FastOutSlowInInterpolator()
|
|
|
|
|
+ val dampedDx =
|
|
|
|
|
+ interpolator.getInterpolation(min(1f, dX / itemView.width)) * itemView.width
|
|
|
|
|
+
|
|
|
|
|
+ // 背景
|
|
|
|
|
+ val background =
|
|
|
|
|
+ requireContext().themeColor(com.google.android.material.R.attr.colorSecondary)
|
|
|
|
|
+ .toDrawable()
|
|
|
|
|
+ background.setBounds(
|
|
|
|
|
+ itemView.left,
|
|
|
|
|
+ itemView.top,
|
|
|
|
|
+ itemView.left + dampedDx.toInt(),
|
|
|
|
|
+ itemView.bottom
|
|
|
|
|
+ )
|
|
|
|
|
+ background.draw(c)
|
|
|
|
|
+
|
|
|
|
|
+ // 图标
|
|
|
|
|
+ val icon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_download)!!
|
|
|
|
|
+ val iconMargin = (itemView.height - icon.intrinsicHeight) / 2
|
|
|
|
|
+ val iconTop = itemView.top + iconMargin
|
|
|
|
|
+ val iconBottom = iconTop + icon.intrinsicHeight
|
|
|
|
|
+ val iconLeft = 12.dpToPx()
|
|
|
|
|
+ val iconRight = iconLeft + icon.intrinsicWidth
|
|
|
|
|
+ icon.setTint(requireContext().themeColor(com.google.android.material.R.attr.colorOnSecondary))
|
|
|
|
|
+ icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
|
|
|
|
|
+ icon.draw(c)
|
|
|
|
|
+
|
|
|
|
|
+ super.onChildDraw(
|
|
|
|
|
+ c,
|
|
|
|
|
+ recyclerView,
|
|
|
|
|
+ viewHolder,
|
|
|
|
|
+ dampedDx,
|
|
|
|
|
+ dY,
|
|
|
|
|
+ actionState,
|
|
|
|
|
+ isCurrentlyActive
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ super.onChildDraw(
|
|
|
|
|
+ c,
|
|
|
|
|
+ recyclerView,
|
|
|
|
|
+ viewHolder,
|
|
|
|
|
+ dX,
|
|
|
|
|
+ dY,
|
|
|
|
|
+ actionState,
|
|
|
|
|
+ isCurrentlyActive
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ val itemTouchHelper = ItemTouchHelper(swipeCallback)
|
|
|
|
|
+ itemTouchHelper.attachToRecyclerView(binding.recyclerView)
|
|
|
|
|
+
|
|
|
binding.recyclerView.layoutManager = mLayoutManager
|
|
binding.recyclerView.layoutManager = mLayoutManager
|
|
|
binding.recyclerView.addItemDecoration(VerticalDivider(requireContext()))
|
|
binding.recyclerView.addItemDecoration(VerticalDivider(requireContext()))
|
|
|
binding.recyclerView.adapter = adapter
|
|
binding.recyclerView.adapter = adapter
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private fun initView() = binding.run {
|
|
private fun initView() = binding.run {
|
|
|
|
|
+ cdBase.applyNavigationBarMargin()
|
|
|
btnChapterTop.setOnClickListener {
|
|
btnChapterTop.setOnClickListener {
|
|
|
mLayoutManager.scrollToPositionWithOffset(0, 0)
|
|
mLayoutManager.scrollToPositionWithOffset(0, 0)
|
|
|
}
|
|
}
|
|
@@ -70,10 +163,9 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
|
|
mLayoutManager.scrollToPositionWithOffset(adapter.itemCount - 1, 0)
|
|
mLayoutManager.scrollToPositionWithOffset(adapter.itemCount - 1, 0)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- tvCurrentChapterInfo.setOnClickListener {
|
|
|
|
|
|
|
+ btnLocate.setOnClickListener {
|
|
|
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
|
|
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
|
|
|
}
|
|
}
|
|
|
- binding.llChapterBaseInfo.applyNavigationBarMargin()
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
@SuppressLint("SetTextI18n")
|