download managment

This commit is contained in:
LagradOst 2021-07-24 17:35:53 +02:00
parent 57828527aa
commit e84eae74ac
3 changed files with 70 additions and 55 deletions

View file

@ -1,11 +1,15 @@
package com.lagradost.cloudstream3.ui.download package com.lagradost.cloudstream3.ui.download
import android.app.Activity
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
@ -31,6 +35,63 @@ class DownloadChildFragment : Fragment() {
putString("name", headerName) putString("name", headerName)
} }
} }
fun handleDownloadClick(activity: Activity?, headerName: String?, click: DownloadClickEvent) {
val id = click.data.id
when (click.action) {
DOWNLOAD_ACTION_DELETE_FILE -> {
activity?.let { ctx ->
VideoDownloadManager.deleteFileAndUpdateSettings(ctx, id)
}
}
DOWNLOAD_ACTION_PAUSE_DOWNLOAD -> {
VideoDownloadManager.downloadEvent.invoke(
Pair(click.data.id, VideoDownloadManager.DownloadActionType.Pause)
)
}
DOWNLOAD_ACTION_RESUME_DOWNLOAD -> {
activity?.let { ctx ->
val pkg = VideoDownloadManager.getDownloadResumePackage(ctx, id)
if (pkg != null) {
VideoDownloadManager.downloadFromResume(ctx, pkg)
} else {
VideoDownloadManager.downloadEvent.invoke(
Pair(click.data.id, VideoDownloadManager.DownloadActionType.Resume)
)
}
}
}
DOWNLOAD_ACTION_PLAY_FILE -> {
activity?.let { act ->
val info =
VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(act, click.data.id)
?: return
(act as FragmentActivity).supportFragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.enter_anim,
R.anim.exit_anim,
R.anim.pop_enter,
R.anim.pop_exit
)
.add(
R.id.homeRoot,
PlayerFragment.newInstance(
UriData(
info.path.toString(),
click.data.id,
headerName ?: "null",
click.data.episode,
click.data.season
),
act.getViewPos(click.data.id)?.position ?: 0
)
)
.commit()
}
}
}
}
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -82,59 +143,11 @@ class DownloadChildFragment : Fragment() {
DownloadChildAdapter( DownloadChildAdapter(
ArrayList(), ArrayList(),
) { click -> ) { click ->
val id = click.data.id handleDownloadClick(activity, name, click)
when (click.action) { when (click.action) {
DOWNLOAD_ACTION_DELETE_FILE -> { DOWNLOAD_ACTION_DELETE_FILE -> {
context?.let { ctx ->
VideoDownloadManager.deleteFileAndUpdateSettings(ctx, id)
}
updateList(folder) updateList(folder)
} }
DOWNLOAD_ACTION_PAUSE_DOWNLOAD -> {
VideoDownloadManager.downloadEvent.invoke(
Pair(click.data.id, VideoDownloadManager.DownloadActionType.Pause)
)
updateList(folder)
}
DOWNLOAD_ACTION_RESUME_DOWNLOAD -> {
context?.let { ctx ->
val pkg = VideoDownloadManager.getDownloadResumePackage(ctx, id)
if(pkg != null) {
VideoDownloadManager.downloadFromResume(ctx, pkg)
} else {
VideoDownloadManager.downloadEvent.invoke(
Pair(click.data.id, VideoDownloadManager.DownloadActionType.Resume)
)
}
}
}
DOWNLOAD_ACTION_PLAY_FILE -> {
val info =
VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(requireContext(), click.data.id)
?: return@DownloadChildAdapter
(requireActivity() as AppCompatActivity).supportFragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.enter_anim,
R.anim.exit_anim,
R.anim.pop_enter,
R.anim.pop_exit
)
.add(
R.id.homeRoot,
PlayerFragment.newInstance(
UriData(
info.path.toString(),
click.data.id,
name ?: "null",
click.data.episode,
click.data.season
),
context?.getViewPos(click.data.id)?.position ?: 0
)
)
.commit()
}
} }
} }
download_child_list.adapter = adapter download_child_list.adapter = adapter

View file

@ -18,7 +18,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class DownloadViewModel : ViewModel() { class DownloadViewModel : ViewModel() {
private val _noDownloadsText = MutableLiveData<String>().apply { private val _noDownloadsText = MutableLiveData<String>().apply {
value = "" value = ""
} }
@ -39,7 +38,7 @@ class DownloadViewModel : ViewModel() {
fun updateList(context: Context) = viewModelScope.launch { fun updateList(context: Context) = viewModelScope.launch {
val children = withContext(Dispatchers.IO) { val children = withContext(Dispatchers.IO) {
val headers = context.getKeys(DOWNLOAD_EPISODE_CACHE) val headers = context.getKeys(DOWNLOAD_EPISODE_CACHE)
headers.mapNotNull { context.getKey<VideoDownloadHelper.DownloadEpisodeCached>(it) } headers.mapNotNull { context.getKey<VideoDownloadHelper.DownloadEpisodeCached>(it) }.distinctBy { it.id } // Remove duplicates
} }
// parentId : bytes // parentId : bytes
@ -50,8 +49,10 @@ class DownloadViewModel : ViewModel() {
// Gets all children downloads // Gets all children downloads
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
for (c in children) { for (c in children) {
val childFile = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, c.id) val childFile = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, c.id) ?: continue
val len = childFile?.totalBytes ?: continue
if(childFile.fileLength <= 1) continue
val len = childFile.totalBytes
if (bytesUsedByChild.containsKey(c.parentId)) { if (bytesUsedByChild.containsKey(c.parentId)) {
bytesUsedByChild[c.parentId] = bytesUsedByChild[c.parentId]?.plus(len) ?: len bytesUsedByChild[c.parentId] = bytesUsedByChild[c.parentId]?.plus(len) ?: len
} else { } else {

View file

@ -714,6 +714,7 @@ object VideoDownloadManager {
} }
private fun deleteFile(context: Context, id: Int): Boolean { private fun deleteFile(context: Context, id: Int): Boolean {
downloadEvent.invoke(Pair(id, DownloadActionType.Stop))
val info = context.getKey<DownloadedFileInfo>(KEY_DOWNLOAD_INFO, id.toString()) ?: return false val info = context.getKey<DownloadedFileInfo>(KEY_DOWNLOAD_INFO, id.toString()) ?: return false
if (isScopedStorage()) { if (isScopedStorage()) {