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

132 lines
5.1 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
2021-07-18 13:02:30 +00:00
import android.annotation.SuppressLint
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-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
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
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-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>) {
(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-07-18 13:02:30 +00:00
@SuppressLint("SetTextI18n")
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-07-18 13:02:30 +00:00
}
observe(downloadsViewModel.availableBytes) {
download_free_txt?.text = "Free • ${getBytesAsText(it)}GB"
download_free?.setLayoutWidth(it)
}
observe(downloadsViewModel.usedBytes) {
download_used_txt?.text = "Used • ${getBytesAsText(it)}GB"
download_used?.setLayoutWidth(it)
}
observe(downloadsViewModel.downloadBytes) {
download_app_txt?.text = "App • ${getBytesAsText(it)}GB"
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-07-28 19:14:45 +00:00
var downloadDeleteEventListener: ((Int) -> Unit)? = null
2021-07-18 13:02:30 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
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)
navController?.navigate(R.id.navigation_download_child, Bundle().apply {
2021-07-19 13:19:47 +00:00
putString("folder", folder)
putString("name", click.data.name)
2021-07-25 14:25:09 +00:00
})
}
},
{ downloadClickEvent ->
handleDownloadClick(activity, downloadClickEvent.data.name, downloadClickEvent)
2021-07-28 19:14:45 +00:00
if (downloadClickEvent.action == DOWNLOAD_ACTION_DELETE_FILE) {
2021-07-25 14:25:09 +00:00
downloadsViewModel.updateList(requireContext())
}
}
)
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 }) {
setList(ArrayList())
downloadsViewModel.updateList(requireContext())
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
}
}