Convert HistoryScene to databinding

This commit is contained in:
tarsin committed 2023-01-02 11:32:55 +08:00
1 parent 53974ad88d
commit 540d290be2
2 files changed
+24 -33

No files matched your search

@@ -36,7 +36,6 @@ import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hippo.app.BaseDialogBuilder
import com.hippo.easyrecyclerview.FastScroller
import com.hippo.easyrecyclerview.HandlerDrawable
import com.hippo.ehviewer.*
import com.hippo.ehviewer.client.EhCacheKeyFactory
@@ -44,6 +43,7 @@ import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.dao.DownloadInfo
import com.hippo.ehviewer.dao.HistoryInfo
import com.hippo.ehviewer.databinding.SceneHistoryBinding
import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener
import com.hippo.ehviewer.ui.CommonOperations
import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter
@@ -61,7 +61,7 @@ import rikka.core.res.resolveColor
@SuppressLint("NotifyDataSetChanged")
class HistoryScene : ToolbarScene() {
private var mRecyclerView: RecyclerView? = null
lateinit var binding: SceneHistoryBinding
private val mAdapter: HistoryAdapter by lazy {
HistoryAdapter(object : DiffUtil.ItemCallback<HistoryInfo>() {
override fun areItemsTheSame(oldItem: HistoryInfo, newItem: HistoryInfo): Boolean {
@@ -126,38 +126,28 @@ class HistoryScene : ToolbarScene() {
inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?
): View {
val view = inflater.inflate(R.layout.scene_history, container, false)
val content = ViewUtils.`$$`(view, R.id.content)
val recyclerView = ViewUtils.`$$`(content, R.id.recycler_view) as RecyclerView
setLiftOnScrollTargetView(recyclerView)
val mFastScroller = ViewUtils.`$$`(content, R.id.fast_scroller) as FastScroller
val mTip = ViewUtils.`$$`(view, R.id.tip) as TextView
val mViewTransition = ViewTransition(content, mTip)
val resources = requireContext().resources
binding = SceneHistoryBinding.inflate(inflater, container, false)
setLiftOnScrollTargetView(binding.recyclerView)
val mViewTransition = ViewTransition(binding.content, binding.tip)
val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.big_history)
drawable!!.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
mTip.setCompoundDrawables(null, drawable, null, null)
drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
binding.tip.setCompoundDrawables(null, drawable, null, null)
val historyData = Pager(
PagingConfig(20)
) {
EhDB.getHistoryLazyList()
}.flow.cachedIn(viewLifecycleOwner.lifecycleScope)
recyclerView.adapter = mAdapter
val layoutManager = AutoStaggeredGridLayoutManager(
0, StaggeredGridLayoutManager.VERTICAL
)
binding.recyclerView.adapter = mAdapter
val layoutManager = AutoStaggeredGridLayoutManager(0, StaggeredGridLayoutManager.VERTICAL)
layoutManager.setColumnSize(resources.getDimensionPixelOffset(Settings.getDetailSizeResId()))
layoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_MIN_SIZE)
recyclerView.layoutManager = layoutManager
recyclerView.clipToPadding = false
recyclerView.clipChildren = false
binding.recyclerView.layoutManager = layoutManager
val itemTouchHelper = ItemTouchHelper(HistoryItemTouchHelperCallback())
itemTouchHelper.attachToRecyclerView(recyclerView)
mFastScroller.attachToRecyclerView(recyclerView)
mRecyclerView = recyclerView
itemTouchHelper.attachToRecyclerView(binding.recyclerView)
binding.fastScroller.attachToRecyclerView(binding.recyclerView)
val handlerDrawable = HandlerDrawable()
handlerDrawable.setColor(theme.resolveColor(com.google.android.material.R.attr.colorPrimary))
mFastScroller.setHandlerDrawable(handlerDrawable)
binding.fastScroller.setHandlerDrawable(handlerDrawable)
viewLifecycleOwner.lifecycleScope.launch {
historyData.collectLatest { value ->
mAdapter.submitData(
@@ -174,7 +164,7 @@ class HistoryScene : ToolbarScene() {
}
}
}
return view
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -185,8 +175,7 @@ class HistoryScene : ToolbarScene() {
override fun onDestroyView() {
super.onDestroyView()
mRecyclerView?.stopScroll()
mRecyclerView = null
binding.recyclerView.stopScroll()
}
override fun onNavigationClick() {
@@ -235,26 +224,26 @@ class HistoryScene : ToolbarScene() {
val context = requireContext()
val activity = mainActivity ?: return false
val downloaded = mDownloadManager.getDownloadState(gi.gid) != DownloadInfo.STATE_INVALID
val favourited = gi.favoriteSlot != -2
val favourite = gi.favoriteSlot != -2
val items = if (downloaded) arrayOf<CharSequence>(
context.getString(R.string.read),
context.getString(R.string.delete_downloads),
context.getString(if (favourited) R.string.remove_from_favourites else R.string.add_to_favourites),
context.getString(if (favourite) R.string.remove_from_favourites else R.string.add_to_favourites),
context.getString(R.string.download_move_dialog_title)
) else arrayOf<CharSequence>(
context.getString(R.string.read),
context.getString(R.string.download),
context.getString(if (favourited) R.string.remove_from_favourites else R.string.add_to_favourites),
context.getString(if (favourite) R.string.remove_from_favourites else R.string.add_to_favourites),
)
val icons = if (downloaded) intArrayOf(
R.drawable.v_book_open_x24,
R.drawable.v_delete_x24,
if (favourited) R.drawable.v_heart_broken_x24 else R.drawable.v_heart_x24,
if (favourite) R.drawable.v_heart_broken_x24 else R.drawable.v_heart_x24,
R.drawable.v_folder_move_x24
) else intArrayOf(
R.drawable.v_book_open_x24,
R.drawable.v_download_x24,
if (favourited) R.drawable.v_heart_broken_x24 else R.drawable.v_heart_x24,
if (favourite) R.drawable.v_heart_broken_x24 else R.drawable.v_heart_x24,
)
BaseDialogBuilder(context)
.setTitle(EhUtils.getSuitableTitle(gi))
@@ -292,7 +281,7 @@ class HistoryScene : ToolbarScene() {
CommonOperations.startDownload(activity, gi, false)
}
2 -> if (favourited) {
2 -> if (favourite) {
CommonOperations.removeFromFavorites(
activity,
gi,