Use view model for single deletions as well

This commit is contained in:
Luna712 2024-07-17 14:52:46 -06:00 committed by GitHub
parent bc8c63ce5c
commit b42337c605
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 60 deletions

View file

@ -23,7 +23,6 @@ import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPres
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback
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.VideoDownloadManager
class DownloadChildFragment : Fragment() { class DownloadChildFragment : Fragment() {
private lateinit var downloadsViewModel: DownloadViewModel private lateinit var downloadsViewModel: DownloadViewModel
@ -38,7 +37,6 @@ class DownloadChildFragment : Fragment() {
} }
override fun onDestroyView() { override fun onDestroyView() {
unsetDownloadDeleteListener()
detachBackPressedCallback() detachBackPressedCallback()
binding = null binding = null
super.onDestroyView() super.onDestroyView()
@ -57,8 +55,6 @@ class DownloadChildFragment : Fragment() {
return localBinding.root return localBinding.root
} }
private var downloadDeleteEventListener: ((Int) -> Unit)? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -134,11 +130,14 @@ class DownloadChildFragment : Fragment() {
val adapter = DownloadAdapter( val adapter = DownloadAdapter(
{}, {},
{ downloadClickEvent -> { click ->
handleDownloadClick(downloadClickEvent) if (click.action == DOWNLOAD_ACTION_DELETE_FILE) {
if (downloadClickEvent.action == DOWNLOAD_ACTION_DELETE_FILE) { context?.let { ctx ->
setUpDownloadDeleteListener(folder) downloadsViewModel.handleSingleDelete(ctx, click.data.id) {
downloadsViewModel.updateChildList(ctx, folder)
} }
}
} else handleDownloadClick(click)
}, },
{ itemId, isChecked -> { itemId, isChecked ->
if (isChecked) { if (isChecked) {
@ -170,10 +169,6 @@ class DownloadChildFragment : Fragment() {
} }
binding?.btnDelete?.setOnClickListener { binding?.btnDelete?.setOnClickListener {
// We want to unset it here if we have it so
// that we don't have to run it for every download,
// we just do it once here.
unsetDownloadDeleteListener()
context?.let { ctx -> context?.let { ctx ->
downloadsViewModel.handleMultiDelete(ctx) { downloadsViewModel.handleMultiDelete(ctx) {
arguments?.getString("folder") arguments?.getString("folder")
@ -207,21 +202,4 @@ class DownloadChildFragment : Fragment() {
binding?.btnDelete?.text = binding?.btnDelete?.text =
getString(R.string.delete_format).format(count, formattedSize) getString(R.string.delete_format).format(count, formattedSize)
} }
private fun setUpDownloadDeleteListener(folder: String) {
downloadDeleteEventListener = { id: Int ->
val list = (binding?.downloadChildList?.adapter as? DownloadAdapter)?.currentList
if (list?.any { it.data.id == id } == true) {
context?.let { downloadsViewModel.updateChildList(it, folder) }
}
}
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it }
}
private fun unsetDownloadDeleteListener() {
downloadDeleteEventListener?.let {
VideoDownloadManager.downloadDeleteEvent -= it
}
downloadDeleteEventListener = null
}
} }

View file

@ -50,7 +50,6 @@ import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
import com.lagradost.cloudstream3.utils.UIHelper.navigate import com.lagradost.cloudstream3.utils.UIHelper.navigate
import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV
import com.lagradost.cloudstream3.utils.VideoDownloadManager
import java.net.URI import java.net.URI
const val DOWNLOAD_NAVIGATE_TO = "downloadpage" const val DOWNLOAD_NAVIGATE_TO = "downloadpage"
@ -68,7 +67,6 @@ class DownloadFragment : Fragment() {
} }
override fun onDestroyView() { override fun onDestroyView() {
unsetDownloadDeleteListener()
detachBackPressedCallback() detachBackPressedCallback()
binding = null binding = null
super.onDestroyView() super.onDestroyView()
@ -87,8 +85,6 @@ class DownloadFragment : Fragment() {
return localBinding.root return localBinding.root
} }
private var downloadDeleteEventListener: ((Int) -> Unit)? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
hideKeyboard() hideKeyboard()
@ -181,11 +177,14 @@ class DownloadFragment : Fragment() {
val adapter = DownloadAdapter( val adapter = DownloadAdapter(
{ click -> handleItemClick(click) }, { click -> handleItemClick(click) },
{ downloadClickEvent -> { click ->
handleDownloadClick(downloadClickEvent) if (click.action == DOWNLOAD_ACTION_DELETE_FILE) {
if (downloadClickEvent.action == DOWNLOAD_ACTION_DELETE_FILE) { context?.let { ctx ->
setUpDownloadDeleteListener() downloadsViewModel.handleSingleDelete(ctx, click.data.id) {
downloadsViewModel.updateList(ctx)
} }
}
} else handleDownloadClick(click)
}, },
{ itemId, isChecked -> { itemId, isChecked ->
if (isChecked) { if (isChecked) {
@ -253,10 +252,6 @@ class DownloadFragment : Fragment() {
} }
binding?.btnDelete?.setOnClickListener { binding?.btnDelete?.setOnClickListener {
// We want to unset it here if we have it so
// that we don't have to run it for every download,
// we just do it once here.
unsetDownloadDeleteListener()
context?.let { ctx -> context?.let { ctx ->
downloadsViewModel.handleMultiDelete(ctx) { downloadsViewModel.handleMultiDelete(ctx) {
downloadsViewModel.updateList(ctx) downloadsViewModel.updateList(ctx)
@ -290,23 +285,6 @@ class DownloadFragment : Fragment() {
getString(R.string.delete_format).format(count, formattedSize) getString(R.string.delete_format).format(count, formattedSize)
} }
private fun setUpDownloadDeleteListener() {
downloadDeleteEventListener = { id ->
val list = (binding?.downloadList?.adapter as? DownloadAdapter)?.currentList
if (list?.any { it.data.id == id } == true) {
context?.let { downloadsViewModel.updateList(it) }
}
}
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it }
}
private fun unsetDownloadDeleteListener() {
downloadDeleteEventListener?.let {
VideoDownloadManager.downloadDeleteEvent -= it
}
downloadDeleteEventListener = null
}
private fun updateStorageInfo( private fun updateStorageInfo(
context: Context, context: Context,
bytes: Long, bytes: Long,

View file

@ -277,6 +277,17 @@ class DownloadViewModel : ViewModel() {
showDeleteConfirmationDialog(context, message, deleteData.ids, onDeleteConfirm) showDeleteConfirmationDialog(context, message, deleteData.ids, onDeleteConfirm)
} }
fun handleSingleDelete(
context: Context,
itemId: Int,
onDeleteConfirm: () -> Unit
) = viewModelScope.launchSafe {
val itemData = getItemDataFromId(itemId)
val deleteData = processSelectedItems(context, itemData)
val message = buildDeleteMessage(context, deleteData)
showDeleteConfirmationDialog(context, message, deleteData.ids, onDeleteConfirm)
}
private fun getSelectedItemsData(): List<VisualDownloadCached>? { private fun getSelectedItemsData(): List<VisualDownloadCached>? {
val selectedIds = selectedItemIds.value ?: return null val selectedIds = selectedItemIds.value ?: return null
val headers = headerCards.value ?: emptyList() val headers = headerCards.value ?: emptyList()
@ -287,6 +298,15 @@ class DownloadViewModel : ViewModel() {
} }
} }
private fun getItemDataFromId(itemId: Int): List<VisualDownloadCached> {
val headers = headerCards.value ?: emptyList()
val children = childCards.value ?: emptyList()
return (headers + children).filter { item ->
item.data.id == itemId
}
}
private fun processSelectedItems( private fun processSelectedItems(
context: Context, context: Context,
selectedItemsList: List<VisualDownloadCached> selectedItemsList: List<VisualDownloadCached>