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

122 lines
4.5 KiB
Kotlin
Raw Normal View History

2021-07-18 23:57:04 +00:00
package com.lagradost.cloudstream3.ui.download
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.FragmentChildDownloadsBinding
2021-07-24 20:50:57 +00:00
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout
2021-07-18 23:57:04 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.getKeys
2021-09-03 09:13:34 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
2021-07-18 23:57:04 +00:00
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import com.lagradost.cloudstream3.utils.VideoDownloadManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class DownloadChildFragment : Fragment() {
companion object {
fun newInstance(headerName: String, folder: String): Bundle {
2021-09-20 21:11:36 +00:00
return Bundle().apply {
putString("folder", folder)
putString("name", headerName)
2021-07-18 23:57:04 +00:00
}
2021-09-20 21:11:36 +00:00
}
2021-07-18 23:57:04 +00:00
}
2021-07-28 19:14:45 +00:00
override fun onDestroyView() {
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent -= it }
binding = null
super.onDestroyView()
2021-07-28 19:14:45 +00:00
}
var binding: FragmentChildDownloadsBinding? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val localBinding = FragmentChildDownloadsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root//inflater.inflate(R.layout.fragment_child_downloads, container, false)
2021-07-18 23:57:04 +00:00
}
private fun updateList(folder: String) = main {
2021-09-12 15:57:07 +00:00
context?.let { ctx ->
val data = withContext(Dispatchers.IO) { ctx.getKeys(folder) }
val eps = withContext(Dispatchers.IO) {
data.mapNotNull { key ->
context?.getKey<VideoDownloadHelper.DownloadEpisodeCached>(key)
}.mapNotNull {
val info = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(ctx, it.id)
?: return@mapNotNull null
VisualDownloadChildCached(info.fileLength, info.totalBytes, it)
}
}.sortedBy { it.data.episode + (it.data.season ?: 0) * 100000 }
2021-09-12 15:57:07 +00:00
if (eps.isEmpty()) {
activity?.onBackPressed()
return@main
2021-07-18 23:57:04 +00:00
}
(binding?.downloadChildList?.adapter as DownloadChildAdapter? ?: return@main).cardList =
eps
binding?.downloadChildList?.adapter?.notifyDataSetChanged()
2021-09-12 15:57:07 +00:00
}
2021-07-18 23:57:04 +00:00
}
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 23:57:04 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val folder = arguments?.getString("folder")
val name = arguments?.getString("name")
if (folder == null) {
activity?.onBackPressed() // TODO FIX
return
}
fixPaddingStatusbar(binding?.downloadChildRoot)
2021-07-18 23:57:04 +00:00
binding?.downloadChildToolbar?.apply {
title = name
setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
setNavigationOnClickListener {
activity?.onBackPressed()
}
2021-07-19 13:19:47 +00:00
}
2021-07-18 23:57:04 +00:00
2021-07-18 23:57:04 +00:00
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder> =
DownloadChildAdapter(
ArrayList(),
) { click ->
handleDownloadClick(click)
2021-07-25 14:25:09 +00:00
}
2021-07-28 19:14:45 +00:00
downloadDeleteEventListener = { id: Int ->
val list = (binding?.downloadChildList?.adapter as DownloadChildAdapter?)?.cardList
2021-07-25 14:25:09 +00:00
if (list != null) {
if (list.any { it.data.id == id }) {
updateList(folder)
2021-07-18 23:57:04 +00:00
}
}
2021-07-25 14:25:09 +00:00
}
2021-07-28 19:14:45 +00:00
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it }
binding?.downloadChildList?.adapter = adapter
binding?.downloadChildList?.setLinearListLayout(
isHorizontal = false,
nextDown = FOCUS_SELF,
nextRight = FOCUS_SELF
)//layoutManager = GridLayoutManager(context, 1)
2021-07-18 23:57:04 +00:00
updateList(folder)
}
}