mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Use view model for single deletions as well
This commit is contained in:
parent
bc8c63ce5c
commit
b42337c605
3 changed files with 36 additions and 60 deletions
app/src/main/java/com/lagradost/cloudstream3/ui/download
|
@ -23,7 +23,6 @@ import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPres
|
|||
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback
|
||||
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
|
||||
import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV
|
||||
import com.lagradost.cloudstream3.utils.VideoDownloadManager
|
||||
|
||||
class DownloadChildFragment : Fragment() {
|
||||
private lateinit var downloadsViewModel: DownloadViewModel
|
||||
|
@ -38,7 +37,6 @@ class DownloadChildFragment : Fragment() {
|
|||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
unsetDownloadDeleteListener()
|
||||
detachBackPressedCallback()
|
||||
binding = null
|
||||
super.onDestroyView()
|
||||
|
@ -57,8 +55,6 @@ class DownloadChildFragment : Fragment() {
|
|||
return localBinding.root
|
||||
}
|
||||
|
||||
private var downloadDeleteEventListener: ((Int) -> Unit)? = null
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
|
@ -134,11 +130,14 @@ class DownloadChildFragment : Fragment() {
|
|||
|
||||
val adapter = DownloadAdapter(
|
||||
{},
|
||||
{ downloadClickEvent ->
|
||||
handleDownloadClick(downloadClickEvent)
|
||||
if (downloadClickEvent.action == DOWNLOAD_ACTION_DELETE_FILE) {
|
||||
setUpDownloadDeleteListener(folder)
|
||||
}
|
||||
{ click ->
|
||||
if (click.action == DOWNLOAD_ACTION_DELETE_FILE) {
|
||||
context?.let { ctx ->
|
||||
downloadsViewModel.handleSingleDelete(ctx, click.data.id) {
|
||||
downloadsViewModel.updateChildList(ctx, folder)
|
||||
}
|
||||
}
|
||||
} else handleDownloadClick(click)
|
||||
},
|
||||
{ itemId, isChecked ->
|
||||
if (isChecked) {
|
||||
|
@ -170,10 +169,6 @@ class DownloadChildFragment : Fragment() {
|
|||
}
|
||||
|
||||
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 ->
|
||||
downloadsViewModel.handleMultiDelete(ctx) {
|
||||
arguments?.getString("folder")
|
||||
|
@ -207,21 +202,4 @@ class DownloadChildFragment : Fragment() {
|
|||
binding?.btnDelete?.text =
|
||||
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
|
||||
}
|
||||
}
|
|
@ -50,7 +50,6 @@ import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
|
|||
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
|
||||
import com.lagradost.cloudstream3.utils.UIHelper.navigate
|
||||
import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV
|
||||
import com.lagradost.cloudstream3.utils.VideoDownloadManager
|
||||
import java.net.URI
|
||||
|
||||
const val DOWNLOAD_NAVIGATE_TO = "downloadpage"
|
||||
|
@ -68,7 +67,6 @@ class DownloadFragment : Fragment() {
|
|||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
unsetDownloadDeleteListener()
|
||||
detachBackPressedCallback()
|
||||
binding = null
|
||||
super.onDestroyView()
|
||||
|
@ -87,8 +85,6 @@ class DownloadFragment : Fragment() {
|
|||
return localBinding.root
|
||||
}
|
||||
|
||||
private var downloadDeleteEventListener: ((Int) -> Unit)? = null
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
hideKeyboard()
|
||||
|
@ -181,11 +177,14 @@ class DownloadFragment : Fragment() {
|
|||
|
||||
val adapter = DownloadAdapter(
|
||||
{ click -> handleItemClick(click) },
|
||||
{ downloadClickEvent ->
|
||||
handleDownloadClick(downloadClickEvent)
|
||||
if (downloadClickEvent.action == DOWNLOAD_ACTION_DELETE_FILE) {
|
||||
setUpDownloadDeleteListener()
|
||||
}
|
||||
{ click ->
|
||||
if (click.action == DOWNLOAD_ACTION_DELETE_FILE) {
|
||||
context?.let { ctx ->
|
||||
downloadsViewModel.handleSingleDelete(ctx, click.data.id) {
|
||||
downloadsViewModel.updateList(ctx)
|
||||
}
|
||||
}
|
||||
} else handleDownloadClick(click)
|
||||
},
|
||||
{ itemId, isChecked ->
|
||||
if (isChecked) {
|
||||
|
@ -253,10 +252,6 @@ class DownloadFragment : Fragment() {
|
|||
}
|
||||
|
||||
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 ->
|
||||
downloadsViewModel.handleMultiDelete(ctx) {
|
||||
downloadsViewModel.updateList(ctx)
|
||||
|
@ -290,23 +285,6 @@ class DownloadFragment : Fragment() {
|
|||
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(
|
||||
context: Context,
|
||||
bytes: Long,
|
||||
|
|
|
@ -277,6 +277,17 @@ class DownloadViewModel : ViewModel() {
|
|||
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>? {
|
||||
val selectedIds = selectedItemIds.value ?: return null
|
||||
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(
|
||||
context: Context,
|
||||
selectedItemsList: List<VisualDownloadCached>
|
||||
|
|
Loading…
Reference in a new issue