From 540d290be2db1228283e20bb5847874a235a2155 Mon Sep 17 00:00:00 2001 From: tarsin Date: Mon, 2 Jan 2023 11:32:55 +0800 Subject: [PATCH] Convert HistoryScene to databinding --- .../hippo/ehviewer/ui/scene/HistoryScene.kt | 53 ++++++++----------- app/src/main/res/layout/scene_history.xml | 4 +- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt index b993027b0..774c43a15 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt @@ -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() { 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( 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( 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, diff --git a/app/src/main/res/layout/scene_history.xml b/app/src/main/res/layout/scene_history.xml index bd60e7a3d..405d8335e 100644 --- a/app/src/main/res/layout/scene_history.xml +++ b/app/src/main/res/layout/scene_history.xml @@ -25,10 +25,12 @@ android:layout_width="match_parent" android:layout_height="match_parent"> -