3
3
Fork 1
mirror of https://github.com/recloudstream/cloudstream.git synced 2024-08-15 01:53:11 +00:00

Move DOWNLOAD_ACTION_DELETE_MULTIPLE_FILES to view model

This commit is contained in:
Luna712 2024-07-05 15:38:32 -06:00 committed by GitHub
parent 7b1f59fdb4
commit 4179292f10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 81 additions and 53 deletions

View file

@ -155,36 +155,6 @@ object DownloadButtonSetup {
)
}
}
DOWNLOAD_ACTION_DELETE_MULTIPLE_FILES -> {
activity?.let { ctx ->
if (click !is DownloadDeleteEvent) return
val ids: List<Int> = click.items.mapNotNull { it?.id }
.takeIf { it.isNotEmpty() } ?: return@let
val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
VideoDownloadManager.deleteFilesAndUpdateSettings(ctx, ids, MainScope())
}
DialogInterface.BUTTON_NEGATIVE -> {
}
}
}
try {
builder.setTitle(R.string.delete_files)
.setMessage(
ctx.getString(R.string.delete_multiple_message)
)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
} catch (e: Exception) {
logError(e)
}
}
}
}
}
}

View file

@ -1,10 +1,12 @@
package com.lagradost.cloudstream3.ui.download
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.FragmentChildDownloadsBinding
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick
@ -24,6 +26,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class DownloadChildFragment : Fragment() {
private lateinit var downloadsViewModel: DownloadViewModel
companion object {
fun newInstance(headerName: String, folder: String): Bundle {
return Bundle().apply {
@ -47,6 +51,7 @@ class DownloadChildFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
downloadsViewModel = ViewModelProvider(this)[DownloadViewModel::class.java]
val localBinding = FragmentChildDownloadsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root
@ -102,16 +107,7 @@ class DownloadChildFragment : Fragment() {
}
val adapter = DownloadAdapter(
{ actionEvent ->
if (actionEvent.action == DOWNLOAD_ACTION_DELETE_FILE) {
val downloadDeleteEvent = DownloadDeleteEvent(
action = DOWNLOAD_ACTION_DELETE_FILE,
items = listOf(actionEvent.data)
)
handleDownloadClick(downloadDeleteEvent)
setUpDownloadDeleteListener(folder)
} else handleDownloadClick(actionEvent)
},
{ actionEvent -> handleActionEvent(folder, actionEvent, context) },
{}
)
@ -129,6 +125,25 @@ class DownloadChildFragment : Fragment() {
updateList(folder)
}
private fun handleActionEvent(folder: String, actionEvent: DownloadActionEventBase, context: Context?) {
when (actionEvent.action) {
DOWNLOAD_ACTION_DELETE_MULTIPLE_FILES -> {
if (actionEvent is DownloadDeleteEvent) {
context?.let { downloadsViewModel.handleMultiDelete(it, actionEvent) }
}
}
DOWNLOAD_ACTION_DELETE_FILE -> {
val downloadDeleteEvent = DownloadDeleteEvent(
action = DOWNLOAD_ACTION_DELETE_FILE,
items = listOf(actionEvent.data)
)
handleDownloadClick(downloadDeleteEvent)
setUpDownloadDeleteListener(folder)
}
else -> handleDownloadClick(actionEvent)
}
}
private fun setUpDownloadDeleteListener(folder: String) {
downloadDeleteEventListener = { id: Int ->
val list = (binding?.downloadChildList?.adapter as? DownloadAdapter)?.currentList

View file

@ -108,19 +108,8 @@ class DownloadFragment : Fragment() {
}
val adapter = DownloadAdapter(
{ actionEvent ->
if (actionEvent.action == DOWNLOAD_ACTION_DELETE_FILE) {
val downloadDeleteEvent = DownloadDeleteEvent(
action = DOWNLOAD_ACTION_DELETE_FILE,
items = listOf(actionEvent.data)
)
handleDownloadClick(downloadDeleteEvent)
setUpDownloadDeleteListener()
} else handleDownloadClick(actionEvent)
},
{ click ->
handleItemClick(click)
}
{ actionEvent -> handleActionEvent(actionEvent, context) },
{ click -> handleItemClick(click) }
)
binding?.downloadList?.apply {
@ -155,6 +144,25 @@ class DownloadFragment : Fragment() {
fixPaddingStatusbar(binding?.downloadRoot)
}
private fun handleActionEvent(actionEvent: DownloadActionEventBase, context: Context?) {
when (actionEvent.action) {
DOWNLOAD_ACTION_DELETE_MULTIPLE_FILES -> {
if (actionEvent is DownloadDeleteEvent) {
context?.let { downloadsViewModel.handleMultiDelete(it, actionEvent) }
}
}
DOWNLOAD_ACTION_DELETE_FILE -> {
val downloadDeleteEvent = DownloadDeleteEvent(
action = DOWNLOAD_ACTION_DELETE_FILE,
items = listOf(actionEvent.data)
)
handleDownloadClick(downloadDeleteEvent)
setUpDownloadDeleteListener()
}
else -> handleDownloadClick(actionEvent)
}
}
private fun handleItemClick(click: DownloadHeaderClickEvent) {
when (click.action) {
DOWNLOAD_ACTION_GO_TO_CHILD -> {

View file

@ -1,21 +1,26 @@
package com.lagradost.cloudstream3.ui.download
import android.content.Context
import android.content.DialogInterface
import android.os.Environment
import android.os.StatFs
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.isMovieType
import com.lagradost.cloudstream3.mvvm.launchSafe
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.AppContextUtils.setDefaultFocus
import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE
import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE
import com.lagradost.cloudstream3.utils.DataStore.getFolderName
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.getKeys
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import com.lagradost.cloudstream3.utils.VideoDownloadManager.deleteFilesAndUpdateSettings
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getDownloadFileInfoAndUpdateSettings
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@ -119,4 +124,34 @@ class DownloadViewModel : ViewModel() {
_headerCards.postValue(visual)
}
}
fun handleMultiDelete(context: Context, event: DownloadDeleteEvent) = viewModelScope.launchSafe {
context.let { ctx ->
val ids: List<Int> = event.items.mapNotNull { it?.id }
.takeIf { it.isNotEmpty() } ?: return@let
val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
deleteFilesAndUpdateSettings(ctx, ids, this@launchSafe)
}
DialogInterface.BUTTON_NEGATIVE -> {
}
}
}
try {
builder.setTitle(R.string.delete_files)
.setMessage(
ctx.getString(R.string.delete_multiple_message)
)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
} catch (e: Exception) {
logError(e)
}
}
}
}