diff --git a/app/src/main/kotlin/bums/lunatic/launcher/home/CompletedFilesActivity.kt b/app/src/main/kotlin/bums/lunatic/launcher/home/CompletedFilesActivity.kt index c600c526..9ee9188e 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/home/CompletedFilesActivity.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/home/CompletedFilesActivity.kt @@ -33,6 +33,7 @@ import androidx.activity.OnBackPressedCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.core.content.FileProvider +import androidx.recyclerview.widget.DiffUtil import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager @@ -1335,8 +1336,22 @@ class CompletedFilesAdapter( fun getAll() = fileList fun submitList(files: List) { + 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 - notifyDataSetChanged() + diffResult.dispatchUpdatesTo(this) } fun setViewMode(mode: FileViewMode) { @@ -1345,8 +1360,13 @@ class CompletedFilesAdapter( } fun updateSelection(selection: Set) { + val oldSelection = selectedFiles selectedFiles = selection - notifyDataSetChanged() + fileList.forEachIndexed { index, file -> + if ((file in oldSelection) != (file in selection)) { + notifyItemChanged(index) + } + } } override fun getItemViewType(position: Int) = viewMode.ordinal diff --git a/app/src/main/kotlin/bums/lunatic/launcher/home/adapters/TorrentListAdapter.kt b/app/src/main/kotlin/bums/lunatic/launcher/home/adapters/TorrentListAdapter.kt index 84e1b24c..7c246f81 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/home/adapters/TorrentListAdapter.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/home/adapters/TorrentListAdapter.kt @@ -7,6 +7,7 @@ import android.view.ViewGroup import android.widget.ImageButton import android.widget.ProgressBar import android.widget.TextView +import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import bums.lunatic.launcher.R import bums.lunatic.launcher.workers.TorrentTask @@ -20,8 +21,20 @@ class TorrentListAdapter( private var tasks = listOf() fun submitList(newTasks: List) { + 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 - notifyDataSetChanged() + diffResult.dispatchUpdatesTo(this) } class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {