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
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lagradost.cloudstream3.R
@ -31,6 +35,63 @@ class DownloadChildFragment : Fragment() {
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? {
@ -82,59 +143,11 @@ class DownloadChildFragment : Fragment() {
DownloadChildAdapter(
ArrayList(),
) { click ->
val id = click.data.id
handleDownloadClick(activity, name, click)
when (click.action) {
DOWNLOAD_ACTION_DELETE_FILE -> {
context?.let { ctx ->
VideoDownloadManager.deleteFileAndUpdateSettings(ctx, id)
}
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

View File

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

View File

@ -167,10 +167,10 @@ object VideoDownloadManager {
}
/** Will return IsDone if not found or error */
fun getDownloadState(id : Int) : DownloadType {
fun getDownloadState(id: Int): DownloadType {
return try {
downloadStatus[id] ?: DownloadType.IsDone
} catch (e : Exception) {
} catch (e: Exception) {
e.printStackTrace()
DownloadType.IsDone
}
@ -714,6 +714,7 @@ object VideoDownloadManager {
}
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
if (isScopedStorage()) {