Skip to content

Commit ccc69dc

Browse files
authored
Merge pull request libre-tube#7181 from Bnyro/master
fix: various bugs when deleting single or all downloads
2 parents 95b5b50 + f1e52a8 commit ccc69dc

2 files changed

Lines changed: 32 additions & 25 deletions

File tree

app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.recyclerview.widget.ListAdapter
1313
import com.github.libretube.R
1414
import com.github.libretube.constants.IntentData
1515
import com.github.libretube.databinding.VideoRowBinding
16+
import com.github.libretube.db.DatabaseHelper
1617
import com.github.libretube.db.DatabaseHolder
1718
import com.github.libretube.db.obj.DownloadWithItems
1819
import com.github.libretube.extensions.formatAsFileSize
@@ -40,6 +41,8 @@ class DownloadsAdapter(
4041
private val downloadTab: DownloadTab,
4142
private val toggleDownload: (DownloadWithItems) -> Boolean
4243
) : ListAdapter<DownloadWithItems, DownloadsViewHolder>(DiffUtilItemCallback()) {
44+
val items get() = (0 until itemCount).map { getItem(it) }
45+
4346
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder {
4447
val binding = VideoRowBinding.inflate(
4548
LayoutInflater.from(parent.context),
@@ -156,9 +159,9 @@ class DownloadsAdapter(
156159
.show()
157160
}
158161

159-
fun deleteDownload(position: Int) {
160-
val download = getItem(position).download
161-
val items = getItem(position).downloadItems
162+
private fun deleteDownloadContent(downloadWithItems: DownloadWithItems) {
163+
val download = downloadWithItems.download
164+
val items = downloadWithItems.downloadItems
162165

163166
items.forEach {
164167
it.path.deleteIfExists()
@@ -170,11 +173,30 @@ class DownloadsAdapter(
170173
runBlocking(Dispatchers.IO) {
171174
DatabaseHolder.Database.downloadDao().deleteDownload(download)
172175
}
176+
}
177+
178+
private fun deleteDownload(position: Int) {
179+
deleteDownloadContent(getItem(position))
180+
173181
submitList(currentList.toMutableList().also {
174182
it.removeAt(position)
175183
})
176184
}
177185

186+
fun deleteAllDownloads(onlyDeleteWatched: Boolean) {
187+
val (toDelete, toKeep) = items.partition {
188+
!onlyDeleteWatched || runBlocking(Dispatchers.IO) {
189+
DatabaseHelper.isVideoWatched(it.download.videoId, it.download.duration ?: 0)
190+
}
191+
}
192+
193+
for (item in toDelete) {
194+
deleteDownloadContent(item)
195+
}
196+
197+
submitList(toKeep)
198+
}
199+
178200
fun restoreItem(position: Int) {
179201
// moves the item back to its initial horizontal position
180202
notifyItemRemoved(position)

app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import com.github.libretube.constants.IntentData
2222
import com.github.libretube.constants.PreferenceKeys
2323
import com.github.libretube.databinding.FragmentDownloadContentBinding
2424
import com.github.libretube.databinding.FragmentDownloadsBinding
25-
import com.github.libretube.db.DatabaseHelper
2625
import com.github.libretube.db.DatabaseHolder.Database
27-
import com.github.libretube.db.obj.DownloadWithItems
2826
import com.github.libretube.db.obj.filterByTab
2927
import com.github.libretube.extensions.ceilHalf
3028
import com.github.libretube.extensions.formatAsFileSize
@@ -108,7 +106,6 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
108106
private val binding get() = _binding!!
109107

110108
private var binder: DownloadService.LocalBinder? = null
111-
private val downloads = mutableListOf<DownloadWithItems>()
112109
private val downloadReceiver = DownloadReceiver()
113110
private lateinit var downloadTab: DownloadTab
114111

@@ -172,7 +169,6 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
172169
return@DownloadsAdapter isDownloading.not()
173170
}
174171
binding.downloadsRecView.adapter = adapter
175-
adapter.submitList(downloads)
176172

177173
var selectedSortType =
178174
PreferenceHelper.getInt(PreferenceKeys.SELECTED_DOWNLOAD_SORT_TYPE, 0)
@@ -197,14 +193,10 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
197193
Database.downloadDao().getAll()
198194
}
199195

200-
downloads.clear()
201-
downloads.addAll(dbDownloads.filterByTab(downloadTab))
202-
203-
if (downloads.isEmpty()) return@launch
204-
196+
val downloads = dbDownloads.filterByTab(downloadTab)
197+
adapter.submitList(downloads)
205198
sortDownloadList(selectedSortType)
206199

207-
208200
binding.downloadsRecView.setOnDismissListener { position ->
209201
adapter.showDeleteDialog(requireContext(), position)
210202
// put the item back to the center, as it's currently out of the screen
@@ -242,7 +234,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
242234
private fun toggleVisibilities() {
243235
val binding = _binding ?: return
244236

245-
val isEmpty = downloads.isEmpty()
237+
val isEmpty = adapter.itemCount == 0
246238
binding.downloadsEmpty.isVisible = isEmpty
247239
binding.downloadsContainer.isGone = isEmpty
248240
binding.deleteAll.isGone = isEmpty
@@ -251,10 +243,10 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
251243

252244
private fun sortDownloadList(sortType: Int, previousSortType: Int? = null) {
253245
if (previousSortType == null && sortType == 1) {
254-
adapter.submitList(downloads.reversed())
246+
adapter.submitList(adapter.items.reversed())
255247
}
256248
if (previousSortType != null && sortType != previousSortType) {
257-
adapter.submitList(downloads.reversed())
249+
adapter.submitList(adapter.items.reversed())
258250
}
259251
}
260252

@@ -267,14 +259,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
267259
onlyDeleteWatchedVideos = selected
268260
}
269261
.setPositiveButton(R.string.okay) { _, _ ->
270-
lifecycleScope.launch {
271-
for (downloadIndex in downloads.size - 1 downTo 0) {
272-
val download = adapter.currentList[downloadIndex].download
273-
if (!onlyDeleteWatchedVideos || DatabaseHelper.isVideoWatched(download.videoId, download.duration ?: 0)) {
274-
adapter.deleteDownload(downloadIndex)
275-
}
276-
}
277-
}
262+
adapter.deleteAllDownloads(onlyDeleteWatchedVideos)
278263
}
279264
.setNegativeButton(R.string.cancel, null)
280265
.show()
@@ -312,7 +297,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
312297
}
313298

314299
fun updateProgress(id: Int, status: DownloadStatus) {
315-
val index = downloads.indexOfFirst {
300+
val index = adapter.items.indexOfFirst {
316301
it.downloadItems.any { item -> item.id == id }
317302
}
318303
val view =

0 commit comments

Comments
 (0)