From e84eae74ac984b21f5970662b417836a7649583f Mon Sep 17 00:00:00 2001 From: LagradOst Date: Sat, 24 Jul 2021 17:35:53 +0200 Subject: [PATCH] download managment --- .../ui/download/DownloadChildFragment.kt | 111 ++++++++++-------- .../ui/download/DownloadViewModel.kt | 9 +- .../utils/VideoDownloadManager.kt | 5 +- 3 files changed, 70 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt index a406fc41..e37d6c76 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt @@ -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 diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt index 8dc4b910..bf1c72ff 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt @@ -18,7 +18,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class DownloadViewModel : ViewModel() { - private val _noDownloadsText = MutableLiveData().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(it) } + headers.mapNotNull { context.getKey(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 { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt index 9c3881be..25d140af 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt @@ -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(KEY_DOWNLOAD_INFO, id.toString()) ?: return false if (isScopedStorage()) {