download stuff

This commit is contained in:
LagradOst 2021-07-25 01:16:30 +02:00
parent 3606d1ec05
commit 423ee144fd
5 changed files with 82 additions and 56 deletions

View file

@ -3,10 +3,12 @@ package com.lagradost.cloudstream3.ui.download
import android.animation.ObjectAnimator import android.animation.ObjectAnimator
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.DialogInterface
import android.view.View import android.view.View
import android.view.animation.DecelerateInterpolator import android.view.animation.DecelerateInterpolator
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.ContentLoadingProgressBar import androidx.core.widget.ContentLoadingProgressBar
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
@ -24,7 +26,24 @@ object DownloadButtonSetup {
when (click.action) { when (click.action) {
DOWNLOAD_ACTION_DELETE_FILE -> { DOWNLOAD_ACTION_DELETE_FILE -> {
activity?.let { ctx -> activity?.let { ctx ->
VideoDownloadManager.deleteFileAndUpdateSettings(ctx, id) val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
VideoDownloadManager.deleteFileAndUpdateSettings(ctx, id)
}
DialogInterface.BUTTON_NEGATIVE -> {
}
}
}
builder.setTitle("Delete File") //TODO FIX NAME
builder.setMessage("This will permanently delete ${click.data.name ?: "Episode ${click.data.episode}"}\nAre you sure?")
.setTitle("Delete")
.setPositiveButton("Delete", dialogClickListener)
.setNegativeButton("Cancel", dialogClickListener)
.show()
} }
} }
DOWNLOAD_ACTION_PAUSE_DOWNLOAD -> { DOWNLOAD_ACTION_PAUSE_DOWNLOAD -> {
@ -91,57 +110,53 @@ object DownloadButtonSetup {
var needImageUpdate = false var needImageUpdate = false
fun changeDownloadImage(state: VideoDownloadManager.DownloadType) { fun changeDownloadImage(state: VideoDownloadManager.DownloadType) {
Coroutines.runOnMainThread { lastState = state
lastState = state if (currentBytes <= 0) needImageUpdate = true
if (currentBytes <= 0) needImageUpdate = true val img = if (currentBytes > 0) when (state) {
val img = if (currentBytes > 0) when (state) { VideoDownloadManager.DownloadType.IsPaused -> R.drawable.ic_baseline_play_arrow_24
VideoDownloadManager.DownloadType.IsPaused -> R.drawable.ic_baseline_play_arrow_24 VideoDownloadManager.DownloadType.IsDownloading -> R.drawable.netflix_pause
VideoDownloadManager.DownloadType.IsDownloading -> R.drawable.netflix_pause else -> R.drawable.ic_baseline_delete_outline_24
else -> R.drawable.ic_baseline_delete_outline_24 } else R.drawable.netflix_download
} else R.drawable.netflix_download downloadImage?.setImageResource(img)
downloadImage?.setImageResource(img)
}
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun fixDownloadedBytes(setCurrentBytes: Long, setTotalBytes: Long, animate: Boolean) { fun fixDownloadedBytes(setCurrentBytes: Long, setTotalBytes: Long, animate: Boolean) {
Coroutines.runOnMainThread { currentBytes = setCurrentBytes
currentBytes = setCurrentBytes totalBytes = setTotalBytes
totalBytes = setTotalBytes
if (currentBytes == 0L) { if (currentBytes == 0L) {
changeDownloadImage(VideoDownloadManager.DownloadType.IsStopped) changeDownloadImage(VideoDownloadManager.DownloadType.IsStopped)
textView?.visibility = View.GONE textView?.visibility = View.GONE
progressBar?.visibility = View.GONE progressBar?.visibility = View.GONE
} else { } else {
if (lastState == VideoDownloadManager.DownloadType.IsStopped) { if (lastState == VideoDownloadManager.DownloadType.IsStopped) {
changeDownloadImage(VideoDownloadManager.getDownloadState(data.id)) changeDownloadImage(VideoDownloadManager.getDownloadState(data.id))
} }
textView?.visibility = View.VISIBLE textView?.visibility = View.VISIBLE
progressBar?.visibility = View.VISIBLE progressBar?.visibility = View.VISIBLE
val currentMbString = "%.1f".format(setCurrentBytes / 1000000f) val currentMbString = "%.1f".format(setCurrentBytes / 1000000f)
val totalMbString = "%.1f".format(setTotalBytes / 1000000f) val totalMbString = "%.1f".format(setTotalBytes / 1000000f)
textView?.text = textView?.text =
"${currentMbString}MB / ${totalMbString}MB" "${currentMbString}MB / ${totalMbString}MB"
progressBar?.let { bar -> progressBar?.let { bar ->
bar.max = (setTotalBytes / 1000).toInt() bar.max = (setTotalBytes / 1000).toInt()
if (animate) { if (animate) {
val animation: ObjectAnimator = ObjectAnimator.ofInt( val animation: ObjectAnimator = ObjectAnimator.ofInt(
bar, bar,
"progress", "progress",
bar.progress, bar.progress,
(setCurrentBytes / 1000).toInt() (setCurrentBytes / 1000).toInt()
) )
animation.duration = 500 animation.duration = 500
animation.setAutoCancel(true) animation.setAutoCancel(true)
animation.interpolator = DecelerateInterpolator() animation.interpolator = DecelerateInterpolator()
animation.start() animation.start()
} else { } else {
bar.progress = (setCurrentBytes / 1000).toInt() bar.progress = (setCurrentBytes / 1000).toInt()
}
} }
} }
} }
@ -153,7 +168,9 @@ object DownloadButtonSetup {
VideoDownloadManager.downloadProgressEvent += { downloadData -> VideoDownloadManager.downloadProgressEvent += { downloadData ->
if (data.id == downloadData.first) { if (data.id == downloadData.first) {
if (downloadData.second != currentBytes || downloadData.third != totalBytes) { // TO PREVENT WASTING UI TIME if (downloadData.second != currentBytes || downloadData.third != totalBytes) { // TO PREVENT WASTING UI TIME
fixDownloadedBytes(downloadData.second, downloadData.third, true) Coroutines.runOnMainThread {
fixDownloadedBytes(downloadData.second, downloadData.third, true)
}
} }
} }
} }
@ -161,7 +178,9 @@ object DownloadButtonSetup {
VideoDownloadManager.downloadStatusEvent += { downloadData -> VideoDownloadManager.downloadStatusEvent += { downloadData ->
if (data.id == downloadData.first) { if (data.id == downloadData.first) {
if (lastState != downloadData.second || needImageUpdate) { // TO PREVENT WASTING UI TIME if (lastState != downloadData.second || needImageUpdate) { // TO PREVENT WASTING UI TIME
changeDownloadImage(downloadData.second) Coroutines.runOnMainThread {
changeDownloadImage(downloadData.second)
}
} }
} }
} }
@ -171,6 +190,7 @@ object DownloadButtonSetup {
clickCallback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_DOWNLOAD, data)) clickCallback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_DOWNLOAD, data))
} else { } else {
val list = arrayListOf( val list = arrayListOf(
Pair(DOWNLOAD_ACTION_PLAY_FILE, R.string.popup_play_file),
Pair(DOWNLOAD_ACTION_DELETE_FILE, R.string.popup_delete_file), Pair(DOWNLOAD_ACTION_DELETE_FILE, R.string.popup_delete_file),
) )

View file

@ -17,6 +17,7 @@ import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick
import com.lagradost.cloudstream3.ui.download.DownloadClickEvent import com.lagradost.cloudstream3.ui.download.DownloadClickEvent
import com.lagradost.cloudstream3.utils.VideoDownloadHelper import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import com.lagradost.cloudstream3.utils.VideoDownloadManager
import kotlinx.android.synthetic.main.result_episode.view.episode_holder import kotlinx.android.synthetic.main.result_episode.view.episode_holder
import kotlinx.android.synthetic.main.result_episode.view.episode_text import kotlinx.android.synthetic.main.result_episode.view.episode_text
import kotlinx.android.synthetic.main.result_episode_large.view.* import kotlinx.android.synthetic.main.result_episode_large.view.*
@ -144,23 +145,25 @@ class EpisodeAdapter(
return@setOnLongClickListener true return@setOnLongClickListener true
} }
if(hasDownloadSupport) { episodeDownloadImage.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
episodeDownloadBar.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
if (hasDownloadSupport) {
val downloadInfo = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(itemView.context, card.id)
DownloadButtonSetup.setUpButton( DownloadButtonSetup.setUpButton(
null, null, episodeDownloadBar, episodeDownloadImage, null, downloadInfo?.fileLength, downloadInfo?.totalBytes, episodeDownloadBar, episodeDownloadImage, null,
VideoDownloadHelper.DownloadEpisodeCached( VideoDownloadHelper.DownloadEpisodeCached(
card.name, card.poster, card.episode, card.season, card.id, 0, card.rating, card.descript card.name, card.poster, card.episode, card.season, card.id, 0, card.rating, card.descript
) )
) { ) {
if(it.action == DOWNLOAD_ACTION_DOWNLOAD) { if (it.action == DOWNLOAD_ACTION_DOWNLOAD) {
clickCallback.invoke(EpisodeClickEvent(ACTION_DOWNLOAD_EPISODE, card)) clickCallback.invoke(EpisodeClickEvent(ACTION_DOWNLOAD_EPISODE, card))
} else { } else {
downloadClickCallback.invoke(it) downloadClickCallback.invoke(it)
} }
} }
} }
episodeDownloadImage.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
episodeDownloadBar.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
} }
} }
} }

View file

@ -688,9 +688,11 @@ class ResultFragment : Fragment() {
if (result_episodes == null || result_episodes.adapter == null) return@observe if (result_episodes == null || result_episodes.adapter == null) return@observe
result_episodes_text.text = "${episodes.size} Episode${if (episodes.size == 1) "" else "s"}" result_episodes_text.text = "${episodes.size} Episode${if (episodes.size == 1) "" else "s"}"
currentEpisodes = episodes currentEpisodes = episodes
(result_episodes.adapter as EpisodeAdapter).cardList = episodes activity?.runOnUiThread {
(result_episodes.adapter as EpisodeAdapter).updateLayout() (result_episodes.adapter as EpisodeAdapter).cardList = episodes
(result_episodes.adapter as EpisodeAdapter).notifyDataSetChanged() (result_episodes.adapter as EpisodeAdapter).updateLayout()
(result_episodes.adapter as EpisodeAdapter).notifyDataSetChanged()
}
} }
observe(viewModel.id) { observe(viewModel.id) {

View file

@ -61,7 +61,7 @@
android:textColor="@color/textColor" android:layout_width="wrap_content" android:textColor="@color/textColor" android:layout_width="wrap_content"
android:layout_height="match_parent"> android:layout_height="match_parent">
</TextView> </TextView>
<FrameLayout <FrameLayout
android:layout_gravity="end" android:layout_gravity="end"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View file

@ -45,6 +45,7 @@
<string name="app_dubbed_text">Dub</string> <string name="app_dubbed_text">Dub</string>
<string name="app_subbed_text">Sub</string> <string name="app_subbed_text">Sub</string>
<string name="popup_delete_file">Delete File</string> <string name="popup_delete_file">Delete File</string>
<string name="popup_play_file">Play File</string>
<string name="popup_resume_download">Resume Download</string> <string name="popup_resume_download">Resume Download</string>
<string name="popup_pause_download">Pause Download</string> <string name="popup_pause_download">Pause Download</string>
</resources> </resources>