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.annotation.SuppressLint
import android.app.Activity
import android.content.DialogInterface
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.ContentLoadingProgressBar
import androidx.fragment.app.FragmentActivity
import com.lagradost.cloudstream3.R
@ -24,8 +26,25 @@ object DownloadButtonSetup {
when (click.action) {
DOWNLOAD_ACTION_DELETE_FILE -> {
activity?.let { ctx ->
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 -> {
VideoDownloadManager.downloadEvent.invoke(
@ -91,7 +110,6 @@ object DownloadButtonSetup {
var needImageUpdate = false
fun changeDownloadImage(state: VideoDownloadManager.DownloadType) {
Coroutines.runOnMainThread {
lastState = state
if (currentBytes <= 0) needImageUpdate = true
val img = if (currentBytes > 0) when (state) {
@ -101,11 +119,9 @@ object DownloadButtonSetup {
} else R.drawable.netflix_download
downloadImage?.setImageResource(img)
}
}
@SuppressLint("SetTextI18n")
fun fixDownloadedBytes(setCurrentBytes: Long, setTotalBytes: Long, animate: Boolean) {
Coroutines.runOnMainThread {
currentBytes = setCurrentBytes
totalBytes = setTotalBytes
@ -145,7 +161,6 @@ object DownloadButtonSetup {
}
}
}
}
fixDownloadedBytes(currentBytes, totalBytes, false)
changeDownloadImage(VideoDownloadManager.getDownloadState(data.id))
@ -153,24 +168,29 @@ object DownloadButtonSetup {
VideoDownloadManager.downloadProgressEvent += { downloadData ->
if (data.id == downloadData.first) {
if (downloadData.second != currentBytes || downloadData.third != totalBytes) { // TO PREVENT WASTING UI TIME
Coroutines.runOnMainThread {
fixDownloadedBytes(downloadData.second, downloadData.third, true)
}
}
}
}
VideoDownloadManager.downloadStatusEvent += { downloadData ->
if (data.id == downloadData.first) {
if (lastState != downloadData.second || needImageUpdate) { // TO PREVENT WASTING UI TIME
Coroutines.runOnMainThread {
changeDownloadImage(downloadData.second)
}
}
}
}
downloadImage.setOnClickListener {
if (currentBytes <= 0) {
clickCallback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_DOWNLOAD, data))
} else {
val list = arrayListOf(
Pair(DOWNLOAD_ACTION_PLAY_FILE, R.string.popup_play_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.DownloadClickEvent
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_text
import kotlinx.android.synthetic.main.result_episode_large.view.*
@ -144,9 +145,14 @@ class EpisodeAdapter(
return@setOnLongClickListener true
}
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(
null, null, episodeDownloadBar, episodeDownloadImage, null,
downloadInfo?.fileLength, downloadInfo?.totalBytes, episodeDownloadBar, episodeDownloadImage, null,
VideoDownloadHelper.DownloadEpisodeCached(
card.name, card.poster, card.episode, card.season, card.id, 0, card.rating, card.descript
)
@ -158,9 +164,6 @@ class EpisodeAdapter(
}
}
}
episodeDownloadImage.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
episodeDownloadBar.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
}
}
}

View File

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

View File

@ -45,6 +45,7 @@
<string name="app_dubbed_text">Dub</string>
<string name="app_subbed_text">Sub</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_pause_download">Pause Download</string>
</resources>