Show selected bytes in delete app bar

This commit is contained in:
Luna712 2024-07-09 16:17:39 -06:00 committed by GitHub
parent 8b7b6659b0
commit 14d16590a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 14 deletions

View file

@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.ui.download
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.text.format.Formatter.formatShortFileSize
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -19,15 +20,9 @@ import com.lagradost.cloudstream3.ui.result.setLinearListLayout
import com.lagradost.cloudstream3.ui.settings.Globals.EMULATOR import com.lagradost.cloudstream3.ui.settings.Globals.EMULATOR
import com.lagradost.cloudstream3.ui.settings.Globals.PHONE import com.lagradost.cloudstream3.ui.settings.Globals.PHONE
import com.lagradost.cloudstream3.ui.settings.Globals.isLayout import com.lagradost.cloudstream3.ui.settings.Globals.isLayout
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.getKeys
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import com.lagradost.cloudstream3.utils.VideoDownloadManager import com.lagradost.cloudstream3.utils.VideoDownloadManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class DownloadChildFragment : Fragment() { class DownloadChildFragment : Fragment() {
private lateinit var downloadsViewModel: DownloadViewModel private lateinit var downloadsViewModel: DownloadViewModel
@ -118,10 +113,12 @@ class DownloadChildFragment : Fragment() {
downloadsViewModel.clearSelectedItems() downloadsViewModel.clearSelectedItems()
} }
} }
observe(downloadsViewModel.selectedBytes) {
updateDeleteButton(downloadsViewModel.selectedItems.value?.count() ?: 0, it)
}
observe(downloadsViewModel.selectedItems) { observe(downloadsViewModel.selectedItems) {
handleSelectedChange(it) handleSelectedChange(it)
binding?.btnDelete?.text = updateDeleteButton(it.count(), downloadsViewModel.selectedBytes.value ?: 0L)
getString(R.string.delete_count).format(it.count())
binding?.btnDelete?.isVisible = it.isNotEmpty() binding?.btnDelete?.isVisible = it.isNotEmpty()
binding?.selectItemsText?.isVisible = it.isEmpty() binding?.selectItemsText?.isVisible = it.isEmpty()
@ -188,6 +185,12 @@ class DownloadChildFragment : Fragment() {
} }
} }
private fun updateDeleteButton(count: Int, selectedBytes: Long) {
val formattedSize = formatShortFileSize(context, selectedBytes)
binding?.btnDelete?.text =
getString(R.string.delete_format).format(count, formattedSize)
}
private fun setUpDownloadDeleteListener(folder: String) { private fun setUpDownloadDeleteListener(folder: String) {
downloadDeleteEventListener = { id: Int -> downloadDeleteEventListener = { id: Int ->
val list = (binding?.downloadChildList?.adapter as? DownloadAdapter)?.currentList val list = (binding?.downloadChildList?.adapter as? DownloadAdapter)?.currentList

View file

@ -125,6 +125,9 @@ class DownloadFragment : Fragment() {
observe(downloadsViewModel.downloadBytes) { observe(downloadsViewModel.downloadBytes) {
updateStorageInfo(view.context, it, R.string.app_storage, binding?.downloadAppTxt, binding?.downloadApp) updateStorageInfo(view.context, it, R.string.app_storage, binding?.downloadAppTxt, binding?.downloadApp)
} }
observe(downloadsViewModel.selectedBytes) {
updateDeleteButton(downloadsViewModel.selectedItems.value?.count() ?: 0, it)
}
observe(downloadsViewModel.isMultiDeleteState) { isMultiDeleteState -> observe(downloadsViewModel.isMultiDeleteState) { isMultiDeleteState ->
val adapter = binding?.downloadList?.adapter as? DownloadAdapter val adapter = binding?.downloadList?.adapter as? DownloadAdapter
adapter?.setIsMultiDeleteState(isMultiDeleteState) adapter?.setIsMultiDeleteState(isMultiDeleteState)
@ -140,8 +143,7 @@ class DownloadFragment : Fragment() {
} }
observe(downloadsViewModel.selectedItems) { observe(downloadsViewModel.selectedItems) {
handleSelectedChange(it) handleSelectedChange(it)
binding?.btnDelete?.text = updateDeleteButton(it.count(), downloadsViewModel.selectedBytes.value ?: 0L)
getString(R.string.delete_count).format(it.count())
binding?.btnDelete?.isVisible = it.isNotEmpty() binding?.btnDelete?.isVisible = it.isNotEmpty()
binding?.selectItemsText?.isVisible = it.isEmpty() binding?.selectItemsText?.isVisible = it.isEmpty()
@ -216,8 +218,8 @@ class DownloadFragment : Fragment() {
} }
} }
private fun handleSelectedChange(selected: MutableList<VisualDownloadCached>) { private fun handleSelectedChange(selectedItems: MutableList<VisualDownloadCached>) {
if (selected.isNotEmpty()) { if (selectedItems.isNotEmpty()) {
binding?.downloadDeleteAppbar?.isVisible = true binding?.downloadDeleteAppbar?.isVisible = true
binding?.downloadStorageAppbar?.isVisible = false binding?.downloadStorageAppbar?.isVisible = false
attachBackPressedCallback() attachBackPressedCallback()
@ -244,6 +246,12 @@ class DownloadFragment : Fragment() {
} }
} }
private fun updateDeleteButton(count: Int, selectedBytes: Long) {
val formattedSize = formatShortFileSize(context, selectedBytes)
binding?.btnDelete?.text =
getString(R.string.delete_format).format(count, formattedSize)
}
private fun setUpDownloadDeleteListener() { private fun setUpDownloadDeleteListener() {
downloadDeleteEventListener = { id -> downloadDeleteEventListener = { id ->
val list = (binding?.downloadList?.adapter as? DownloadAdapter)?.currentList val list = (binding?.downloadList?.adapter as? DownloadAdapter)?.currentList

View file

@ -303,10 +303,10 @@
<string name="season_short">S</string> <string name="season_short">S</string>
<string name="episode_short">E</string> <string name="episode_short">E</string>
<string name="no_episodes_found">No Episodes found</string> <string name="no_episodes_found">No Episodes found</string>
<string name="delete">Delete</string>
<string name="delete_file">Delete File</string> <string name="delete_file">Delete File</string>
<string name="delete_files">Delete Files</string> <string name="delete_files">Delete Files</string>
<string name="delete">Delete</string> <string name="delete_format">Delete (%1$d | %2$s)</string>
<string name="delete_count">Delete (%d)</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="pause">Pause</string> <string name="pause">Pause</string>
<string name="start">Start</string> <string name="start">Start</string>