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,8 +26,25 @@ object DownloadButtonSetup {
when (click.action) { when (click.action) {
DOWNLOAD_ACTION_DELETE_FILE -> { DOWNLOAD_ACTION_DELETE_FILE -> {
activity?.let { ctx -> activity?.let { ctx ->
val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
VideoDownloadManager.deleteFileAndUpdateSettings(ctx, id) 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 -> {
VideoDownloadManager.downloadEvent.invoke( VideoDownloadManager.downloadEvent.invoke(
@ -91,7 +110,6 @@ 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) {
@ -101,11 +119,9 @@ object DownloadButtonSetup {
} 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
@ -145,7 +161,6 @@ object DownloadButtonSetup {
} }
} }
} }
}
fixDownloadedBytes(currentBytes, totalBytes, false) fixDownloadedBytes(currentBytes, totalBytes, false)
changeDownloadImage(VideoDownloadManager.getDownloadState(data.id)) changeDownloadImage(VideoDownloadManager.getDownloadState(data.id))
@ -153,24 +168,29 @@ 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
Coroutines.runOnMainThread {
fixDownloadedBytes(downloadData.second, downloadData.third, true) fixDownloadedBytes(downloadData.second, downloadData.third, true)
} }
} }
} }
}
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
Coroutines.runOnMainThread {
changeDownloadImage(downloadData.second) changeDownloadImage(downloadData.second)
} }
} }
} }
}
downloadImage.setOnClickListener { downloadImage.setOnClickListener {
if (currentBytes <= 0) { if (currentBytes <= 0) {
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,9 +145,14 @@ class EpisodeAdapter(
return@setOnLongClickListener true return@setOnLongClickListener true
} }
episodeDownloadImage.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
episodeDownloadBar.visibility = if (hasDownloadSupport) View.VISIBLE else View.GONE
if (hasDownloadSupport) { 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
) )
@ -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 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
activity?.runOnUiThread {
(result_episodes.adapter as EpisodeAdapter).cardList = episodes (result_episodes.adapter as EpisodeAdapter).cardList = episodes
(result_episodes.adapter as EpisodeAdapter).updateLayout() (result_episodes.adapter as EpisodeAdapter).updateLayout()
(result_episodes.adapter as EpisodeAdapter).notifyDataSetChanged() (result_episodes.adapter as EpisodeAdapter).notifyDataSetChanged()
} }
}
observe(viewModel.id) { observe(viewModel.id) {
currentId = it currentId = it

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>