RecyclerView 전체 갱신(notifyDataSetChanged)을 DiffUtil 및 부분 갱신으로 개선
- CompletedFilesAdapter: submitList()에 DiffUtil 적용 및 updateSelection() 시 선택 상태가 바뀐 항목만 notifyItemChanged() 호출 - TorrentListAdapter: submitList()에 DiffUtil 적용하여 진행률/상태 변경 시 불필요한 전체 뷰 재바인딩 방지
This commit is contained in:
@@ -33,6 +33,7 @@ import androidx.activity.OnBackPressedCallback
|
|||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
@@ -1335,8 +1336,22 @@ class CompletedFilesAdapter(
|
|||||||
fun getAll() = fileList
|
fun getAll() = fileList
|
||||||
|
|
||||||
fun submitList(files: List<File>) {
|
fun submitList(files: List<File>) {
|
||||||
|
val oldList = fileList
|
||||||
|
val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
|
||||||
|
override fun getOldListSize() = oldList.size
|
||||||
|
override fun getNewListSize() = files.size
|
||||||
|
override fun areItemsTheSame(oldPos: Int, newPos: Int): Boolean =
|
||||||
|
oldList[oldPos].absolutePath == files[newPos].absolutePath
|
||||||
|
override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean {
|
||||||
|
val o = oldList[oldPos]
|
||||||
|
val n = files[newPos]
|
||||||
|
return o.absolutePath == n.absolutePath &&
|
||||||
|
o.lastModified() == n.lastModified() &&
|
||||||
|
o.length() == n.length()
|
||||||
|
}
|
||||||
|
})
|
||||||
fileList = files
|
fileList = files
|
||||||
notifyDataSetChanged()
|
diffResult.dispatchUpdatesTo(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setViewMode(mode: FileViewMode) {
|
fun setViewMode(mode: FileViewMode) {
|
||||||
@@ -1345,8 +1360,13 @@ class CompletedFilesAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun updateSelection(selection: Set<File>) {
|
fun updateSelection(selection: Set<File>) {
|
||||||
|
val oldSelection = selectedFiles
|
||||||
selectedFiles = selection
|
selectedFiles = selection
|
||||||
notifyDataSetChanged()
|
fileList.forEachIndexed { index, file ->
|
||||||
|
if ((file in oldSelection) != (file in selection)) {
|
||||||
|
notifyItemChanged(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemViewType(position: Int) = viewMode.ordinal
|
override fun getItemViewType(position: Int) = viewMode.ordinal
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.view.ViewGroup
|
|||||||
import android.widget.ImageButton
|
import android.widget.ImageButton
|
||||||
import android.widget.ProgressBar
|
import android.widget.ProgressBar
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import bums.lunatic.launcher.R
|
import bums.lunatic.launcher.R
|
||||||
import bums.lunatic.launcher.workers.TorrentTask
|
import bums.lunatic.launcher.workers.TorrentTask
|
||||||
@@ -20,8 +21,20 @@ class TorrentListAdapter(
|
|||||||
private var tasks = listOf<TorrentTask>()
|
private var tasks = listOf<TorrentTask>()
|
||||||
|
|
||||||
fun submitList(newTasks: List<TorrentTask>) {
|
fun submitList(newTasks: List<TorrentTask>) {
|
||||||
|
val oldTasks = tasks
|
||||||
|
val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
|
||||||
|
override fun getOldListSize() = oldTasks.size
|
||||||
|
override fun getNewListSize() = newTasks.size
|
||||||
|
override fun areItemsTheSame(oldPos: Int, newPos: Int) =
|
||||||
|
oldTasks[oldPos].name == newTasks[newPos].name
|
||||||
|
override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean {
|
||||||
|
val o = oldTasks[oldPos]
|
||||||
|
val n = newTasks[newPos]
|
||||||
|
return o.name == n.name && o.stateText == n.stateText && o.progress == n.progress
|
||||||
|
}
|
||||||
|
})
|
||||||
tasks = newTasks
|
tasks = newTasks
|
||||||
notifyDataSetChanged()
|
diffResult.dispatchUpdatesTo(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
|||||||
Reference in New Issue
Block a user