2021-07-15 16:45:25 +00:00
|
|
|
package com.lagradost.cloudstream3.ui.download
|
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
|
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-19 13:19:47 +00:00
|
|
|
import androidx.navigation.findNavController
|
2021-07-18 13:02:30 +00:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
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
|
2021-07-18 23:57:04 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE
|
|
|
|
import com.lagradost.cloudstream3.utils.DataStore.getFolderName
|
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
|
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
|
2021-07-18 13:02:30 +00:00
|
|
|
import kotlinx.android.synthetic.main.fragment_downloads.*
|
|
|
|
|
2021-07-30 01:14:53 +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 getBytesAsText(bytes: Long): String {
|
|
|
|
return "%.1f".format(bytes / 1000000000f)
|
|
|
|
}
|
|
|
|
|
|
|
|
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>) {
|
|
|
|
(download_list?.adapter as DownloadHeaderAdapter?)?.cardList = list
|
|
|
|
download_list?.adapter?.notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
(download_list?.adapter as DownloadHeaderAdapter?)?.killAdapter()
|
|
|
|
super.onDestroyView()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent -= it }
|
|
|
|
super.onDestroy()
|
2021-07-25 14:25:09 +00:00
|
|
|
}
|
|
|
|
|
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 =
|
2021-07-15 16:45:25 +00:00
|
|
|
ViewModelProvider(this).get(DownloadViewModel::class.java)
|
2021-07-18 13:02:30 +00:00
|
|
|
observe(downloadsViewModel.noDownloadsText) {
|
|
|
|
text_no_downloads.text = it
|
|
|
|
}
|
|
|
|
observe(downloadsViewModel.headerCards) {
|
2021-07-25 14:25:09 +00:00
|
|
|
setList(it)
|
2021-10-03 19:15:56 +00:00
|
|
|
download_loading.isVisible = false
|
2021-07-18 13:02:30 +00:00
|
|
|
}
|
|
|
|
observe(downloadsViewModel.availableBytes) {
|
2021-09-02 13:19:50 +00:00
|
|
|
download_free_txt?.text =
|
|
|
|
getString(R.string.storage_size_format).format(getString(R.string.free_storage), getBytesAsText(it))
|
2021-07-18 13:02:30 +00:00
|
|
|
download_free?.setLayoutWidth(it)
|
|
|
|
}
|
|
|
|
observe(downloadsViewModel.usedBytes) {
|
2021-09-02 13:19:50 +00:00
|
|
|
download_used_txt?.text =
|
|
|
|
getString(R.string.storage_size_format).format(getString(R.string.used_storage), getBytesAsText(it))
|
2021-07-18 13:02:30 +00:00
|
|
|
download_used?.setLayoutWidth(it)
|
|
|
|
}
|
|
|
|
observe(downloadsViewModel.downloadBytes) {
|
2021-09-02 13:19:50 +00:00
|
|
|
download_app_txt?.text =
|
|
|
|
getString(R.string.storage_size_format).format(getString(R.string.app_storage), getBytesAsText(it))
|
2021-07-18 13:02:30 +00:00
|
|
|
download_app?.setLayoutWidth(it)
|
2021-07-19 13:19:47 +00:00
|
|
|
download_storage_appbar?.visibility = View.VISIBLE
|
2021-07-18 13:02:30 +00:00
|
|
|
}
|
|
|
|
return inflater.inflate(R.layout.fragment_downloads, container, false)
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:17:06 +00:00
|
|
|
private var downloadDeleteEventListener: ((Int) -> Unit)? = null
|
2021-07-28 19:14:45 +00:00
|
|
|
|
2021-07-18 13:02:30 +00:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2021-08-04 13:30:34 +00:00
|
|
|
hideKeyboard()
|
|
|
|
|
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 ->
|
|
|
|
if (click.data.type.isMovieType()) {
|
|
|
|
//wont be called
|
|
|
|
} else {
|
|
|
|
val folder = getFolderName(DOWNLOAD_EPISODE_CACHE, click.data.id.toString())
|
|
|
|
val navController = activity?.findNavController(R.id.nav_host_fragment)
|
2021-10-03 00:09:13 +00:00
|
|
|
navController?.navigate(R.id.navigation_download_child, DownloadChildFragment.newInstance(click.data.name,folder))
|
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
|
2021-07-25 14:25:09 +00:00
|
|
|
handleDownloadClick(activity, downloadClickEvent.data.name, 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 ->
|
2021-07-25 14:25:09 +00:00
|
|
|
val list = (download_list?.adapter as DownloadHeaderAdapter?)?.cardList
|
|
|
|
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 }
|
|
|
|
|
2021-07-18 13:02:30 +00:00
|
|
|
download_list.adapter = adapter
|
|
|
|
download_list.layoutManager = GridLayoutManager(context, 1)
|
|
|
|
downloadsViewModel.updateList(requireContext())
|
|
|
|
|
2021-07-29 00:54:27 +00:00
|
|
|
context?.fixPaddingStatusbar(download_root)
|
2021-04-30 17:20:15 +00:00
|
|
|
}
|
|
|
|
}
|