AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt

280 lines
11 KiB
Kotlin
Raw Normal View History

2021-07-15 16:45:25 +00:00
package com.lagradost.cloudstream3.ui.download
2021-04-30 17:20:15 +00:00
import android.app.Dialog
import android.content.ClipboardManager
import android.content.Context
import android.os.Build
2021-04-30 17:20:15 +00:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-07-18 13:02:30 +00:00
import android.widget.LinearLayout
import android.widget.Toast
2021-10-31 18:28:35 +00:00
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
2021-10-03 19:15:56 +00:00
import androidx.core.view.isVisible
2021-04-30 17:20:15 +00:00
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
2021-07-18 13:02:30 +00:00
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lagradost.cloudstream3.CommonActivity.showToast
2021-04-30 17:20:15 +00:00
import com.lagradost.cloudstream3.R
2021-07-18 23:57:04 +00:00
import com.lagradost.cloudstream3.isMovieType
2021-07-18 13:02:30 +00:00
import com.lagradost.cloudstream3.mvvm.observe
2021-07-25 14:25:09 +00:00
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick
import com.lagradost.cloudstream3.ui.player.GeneratorPlayer
import com.lagradost.cloudstream3.ui.player.LinkGenerator
2021-10-31 18:28:35 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.loadResult
2021-11-04 16:07:29 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.main
2021-07-18 23:57:04 +00:00
import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE
2021-10-31 18:28:35 +00:00
import com.lagradost.cloudstream3.utils.DataStore
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
2021-09-02 13:19:50 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
2021-08-04 13:30:34 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
2022-05-15 18:38:32 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.navigate
2021-08-21 21:06:24 +00:00
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
2021-07-25 14:25:09 +00:00
import com.lagradost.cloudstream3.utils.VideoDownloadManager
import android.text.format.Formatter.formatShortFileSize
2022-09-05 18:45:56 +00:00
import androidx.core.widget.doOnTextChanged
import com.lagradost.cloudstream3.databinding.FragmentDownloadsBinding
import com.lagradost.cloudstream3.databinding.StreamInputBinding
2022-09-05 18:45:56 +00:00
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
2023-03-17 21:15:25 +00:00
import com.lagradost.cloudstream3.ui.player.BasicLink
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
2022-09-05 18:45:56 +00:00
import java.net.URI
2021-07-18 13:02:30 +00:00
2022-06-29 01:20:23 +00:00
const val DOWNLOAD_NAVIGATE_TO = "downloadpage"
2021-07-15 16:45:25 +00:00
class DownloadFragment : Fragment() {
2021-07-18 13:02:30 +00:00
private lateinit var downloadsViewModel: DownloadViewModel
private fun View.setLayoutWidth(weight: Long) {
val param = LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
maxOf((weight / 1000000000f), 0.1f) // 100mb
)
this.layoutParams = param
}
2021-04-30 17:20:15 +00:00
2021-07-28 19:14:45 +00:00
private fun setList(list: List<VisualDownloadHeaderCached>) {
2021-11-04 16:07:29 +00:00
main {
(binding?.downloadList?.adapter as DownloadHeaderAdapter?)?.cardList = list
binding?.downloadList?.adapter?.notifyDataSetChanged()
2021-11-04 16:07:29 +00:00
}
2021-07-28 19:14:45 +00:00
}
override fun onDestroyView() {
2022-05-15 18:38:32 +00:00
if (downloadDeleteEventListener != null) {
2021-11-10 22:37:44 +00:00
VideoDownloadManager.downloadDeleteEvent -= downloadDeleteEventListener!!
downloadDeleteEventListener = null
}
binding = null
super.onDestroyView()
2021-07-25 14:25:09 +00:00
}
var binding: FragmentDownloadsBinding? = null
2021-04-30 17:20:15 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
2021-07-18 13:02:30 +00:00
downloadsViewModel =
ViewModelProvider(this)[DownloadViewModel::class.java]
val localBinding = FragmentDownloadsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root//inflater.inflate(R.layout.fragment_downloads, container, false)
}
private var downloadDeleteEventListener: ((Int) -> Unit)? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
hideKeyboard()
2021-07-18 13:02:30 +00:00
observe(downloadsViewModel.noDownloadsText) {
binding?.textNoDownloads?.text = it
2021-07-18 13:02:30 +00:00
}
observe(downloadsViewModel.headerCards) {
2021-07-25 14:25:09 +00:00
setList(it)
binding?.downloadLoading?.isVisible = false
2021-07-18 13:02:30 +00:00
}
observe(downloadsViewModel.availableBytes) {
binding?.downloadFreeTxt?.text =
2022-05-15 18:38:32 +00:00
getString(R.string.storage_size_format).format(
getString(R.string.free_storage),
formatShortFileSize(view.context, it)
2022-05-15 18:38:32 +00:00
)
binding?.downloadFree?.setLayoutWidth(it)
2021-07-18 13:02:30 +00:00
}
observe(downloadsViewModel.usedBytes) {
binding?.apply {
downloadUsedTxt.text =
getString(R.string.storage_size_format).format(
getString(R.string.used_storage),
formatShortFileSize(view.context, it)
)
downloadUsed.setLayoutWidth(it)
downloadStorageAppbar.isVisible = it > 0
}
2021-07-18 13:02:30 +00:00
}
observe(downloadsViewModel.downloadBytes) {
binding?.apply {
downloadAppTxt.text =
getString(R.string.storage_size_format).format(
getString(R.string.app_storage),
formatShortFileSize(view.context, it)
)
downloadApp.setLayoutWidth(it)
}
2021-07-18 13:02:30 +00:00
}
2021-08-04 13:30:34 +00:00
2021-07-18 13:02:30 +00:00
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder> =
DownloadHeaderAdapter(
ArrayList(),
2021-07-25 14:25:09 +00:00
{ click ->
2021-10-31 18:28:35 +00:00
when (click.action) {
0 -> {
if (click.data.type.isMovieType()) {
//wont be called
} else {
2022-05-15 18:38:32 +00:00
val folder = DataStore.getFolderName(
DOWNLOAD_EPISODE_CACHE,
click.data.id.toString()
)
activity?.navigate(
R.id.action_navigation_downloads_to_navigation_download_child,
2021-10-31 18:28:35 +00:00
DownloadChildFragment.newInstance(click.data.name, folder)
)
}
}
2021-10-31 18:28:35 +00:00
1 -> {
2022-05-15 18:38:32 +00:00
(activity as AppCompatActivity?)?.loadResult(
click.data.url,
click.data.apiName
)
2021-10-31 18:28:35 +00:00
}
2021-07-25 14:25:09 +00:00
}
2021-10-31 18:28:35 +00:00
2021-07-25 14:25:09 +00:00
},
{ downloadClickEvent ->
2021-08-21 21:06:24 +00:00
if (downloadClickEvent.data !is VideoDownloadHelper.DownloadEpisodeCached) return@DownloadHeaderAdapter
handleDownloadClick(downloadClickEvent)
2021-07-28 19:14:45 +00:00
if (downloadClickEvent.action == DOWNLOAD_ACTION_DELETE_FILE) {
2021-09-12 15:57:07 +00:00
context?.let { ctx ->
downloadsViewModel.updateList(ctx)
}
2021-07-25 14:25:09 +00:00
}
}
)
2021-07-28 19:14:45 +00:00
downloadDeleteEventListener = { id ->
val list = (binding?.downloadList?.adapter as DownloadHeaderAdapter?)?.cardList
2021-07-25 14:25:09 +00:00
if (list != null) {
if (list.any { it.data.id == id }) {
2021-09-12 15:57:07 +00:00
context?.let { ctx ->
setList(ArrayList())
downloadsViewModel.updateList(ctx)
}
2021-07-18 23:57:04 +00:00
}
2021-07-18 13:02:30 +00:00
}
2021-07-25 14:25:09 +00:00
}
2021-07-28 19:14:45 +00:00
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it }
binding?.downloadList?.apply {
this.adapter = adapter
setLinearListLayout(
isHorizontal = false,
nextRight = FOCUS_SELF,
nextUp = FOCUS_SELF,
nextDown = FOCUS_SELF
)
//layoutManager = GridLayoutManager(context, 1)
}
// Should be visible in emulator layout
binding?.downloadStreamButton?.isGone = isTrueTvSettings()
binding?.downloadStreamButton?.setOnClickListener {
val dialog =
Dialog(it.context ?: return@setOnClickListener, R.style.AlertDialogCustom)
val binding = StreamInputBinding.inflate(dialog.layoutInflater)
dialog.setContentView(binding.root)
2022-06-29 01:20:23 +00:00
dialog.show()
2022-09-05 18:45:56 +00:00
// If user has clicked the switch do not interfere
var preventAutoSwitching = false
binding.hlsSwitch.setOnClickListener {
2022-09-05 18:45:56 +00:00
preventAutoSwitching = true
}
fun activateSwitchOnHls(text: String?) {
binding.hlsSwitch.isChecked = normalSafeApiCall {
2022-09-05 18:45:56 +00:00
URI(text).path?.substringAfterLast(".")?.contains("m3u")
} == true
}
binding.streamReferer.doOnTextChanged { text, _, _, _ ->
2022-09-05 18:45:56 +00:00
if (!preventAutoSwitching)
activateSwitchOnHls(text?.toString())
}
(activity?.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager?)?.primaryClip?.getItemAt(
0
)?.text?.toString()?.let { copy ->
2022-09-05 18:45:56 +00:00
val fixedText = copy.trim()
binding.streamUrl.setText(fixedText)
2022-09-05 18:45:56 +00:00
activateSwitchOnHls(fixedText)
}
binding.applyBtt.setOnClickListener {
val url = binding.streamUrl.text?.toString()
if (url.isNullOrEmpty()) {
showToast(R.string.error_invalid_url, Toast.LENGTH_SHORT)
} else {
val referer = binding.streamReferer.text?.toString()
activity?.navigate(
R.id.global_to_navigation_player,
GeneratorPlayer.newInstance(
LinkGenerator(
2023-03-17 21:15:25 +00:00
listOf(BasicLink(url)),
extract = true,
2022-09-05 18:45:56 +00:00
referer = referer,
isM3u8 = binding.hlsSwitch.isChecked
)
)
)
dialog.dismissSafe(activity)
}
}
binding.cancelBtt.setOnClickListener {
dialog.dismissSafe(activity)
}
2022-06-29 01:20:23 +00:00
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
binding?.downloadList?.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
2022-06-29 01:20:23 +00:00
val dy = scrollY - oldScrollY
if (dy > 0) { //check for scroll down
binding?.downloadStreamButton?.shrink() // hide
2022-06-29 01:20:23 +00:00
} else if (dy < -5) {
binding?.downloadStreamButton?.extend() // show
2022-06-29 01:20:23 +00:00
}
}
}
2021-07-18 13:02:30 +00:00
downloadsViewModel.updateList(requireContext())
fixPaddingStatusbar(binding?.downloadRoot)
2021-04-30 17:20:15 +00:00
}
}