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

94 lines
3.7 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-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 13:02:30 +00:00
import com.lagradost.cloudstream3.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-18 23:57:04 +00:00
import com.lagradost.cloudstream3.ui.result.ResultFragment
import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE
import com.lagradost.cloudstream3.utils.DataStore.getFolderName
2021-07-18 13:02:30 +00:00
import kotlinx.android.synthetic.main.fragment_downloads.*
import kotlinx.android.synthetic.main.fragment_result.*
2021-04-30 17:20:15 +00:00
2021-07-15 16:45:25 +00:00
class DownloadFragment : Fragment() {
2021-04-30 17:20:15 +00:00
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-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) {
(download_list?.adapter as DownloadHeaderAdapter? ?: return@observe).cardList = it
(download_list?.adapter as DownloadHeaderAdapter? ?: return@observe).notifyDataSetChanged()
}
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)
}
return inflater.inflate(R.layout.fragment_downloads, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder> =
DownloadHeaderAdapter(
ArrayList(),
) { click ->
2021-07-18 23:57:04 +00:00
if(click.data.type.isMovieType()) {
//TODO MOVIE
}
else {
val folder = getFolderName(DOWNLOAD_EPISODE_CACHE, click.data.id.toString())
activity?.supportFragmentManager?.beginTransaction()
?.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim, R.anim.pop_enter, R.anim.pop_exit)
?.add(R.id.homeRoot, DownloadChildFragment.newInstance(click.data.name, folder))
?.commit()
}
2021-07-18 13:02:30 +00:00
}
download_list.adapter = adapter
download_list.layoutManager = GridLayoutManager(context, 1)
downloadsViewModel.updateList(requireContext())
activity?.fixPaddingStatusbar(download_root)
2021-04-30 17:20:15 +00:00
}
}