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

174 lines
6.6 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.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-31 18:28:35 +00:00
import androidx.appcompat.app.AppCompatActivity
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
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-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
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
2021-07-18 13:02:30 +00:00
import kotlinx.android.synthetic.main.fragment_downloads.*
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>) {
2021-11-04 16:07:29 +00:00
main {
(download_list?.adapter as DownloadHeaderAdapter?)?.cardList = list
download_list?.adapter?.notifyDataSetChanged()
}
2021-07-28 19:14:45 +00:00
}
override fun onDestroyView() {
(download_list?.adapter as DownloadHeaderAdapter?)?.killAdapter()
super.onDestroyView()
}
override fun onDestroy() {
2022-05-15 18:38:32 +00:00
if (downloadDeleteEventListener != null) {
2021-11-10 22:37:44 +00:00
VideoDownloadManager.downloadDeleteEvent -= downloadDeleteEventListener!!
downloadDeleteEventListener = null
}
2021-07-28 19:14:45 +00:00
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 =
2022-05-15 18:38:32 +00:00
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 =
2022-05-15 18:38:32 +00:00
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 =
2022-05-15 18:38:32 +00:00
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 ->
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)
)
}
}
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
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
}
}