AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt

129 lines
4.6 KiB
Kotlin
Raw Normal View History

2021-04-30 17:20:15 +00:00
package com.lagradost.cloudstream3.ui.home
2021-07-29 01:46:10 +00:00
import android.content.res.Configuration
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-29 01:46:10 +00:00
import android.widget.TextView
2021-07-29 00:54:27 +00:00
import androidx.appcompat.app.AppCompatActivity
2021-04-30 17:20:15 +00:00
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
2021-07-29 00:19:42 +00:00
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
2021-07-29 01:46:10 +00:00
import com.google.android.material.bottomsheet.BottomSheetDialog
2021-04-30 17:20:15 +00:00
import com.lagradost.cloudstream3.R
2021-07-29 00:54:27 +00:00
import com.lagradost.cloudstream3.UIHelper.fixPaddingStatusbar
2021-07-29 01:46:10 +00:00
import com.lagradost.cloudstream3.UIHelper.getGridIsCompact
2021-07-29 00:54:27 +00:00
import com.lagradost.cloudstream3.UIHelper.loadResult
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.observe
2021-07-29 01:46:10 +00:00
import com.lagradost.cloudstream3.ui.AutofitRecyclerView
import com.lagradost.cloudstream3.ui.search.SearchAdapter
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey
2021-07-29 01:46:10 +00:00
import com.lagradost.cloudstream3.utils.Event
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.utils.HOMEPAGE_API
import kotlinx.android.synthetic.main.fragment_home.*
2021-04-30 17:20:15 +00:00
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
2021-07-29 00:19:42 +00:00
return inflater.inflate(R.layout.fragment_home, container, false)
}
2021-07-29 01:46:10 +00:00
private val configEvent = Event<Int>()
private var currentSpan = 1
private fun fixGrid() {
val compactView = activity?.getGridIsCompact() ?: false
val spanCountLandscape = if (compactView) 2 else 6
val spanCountPortrait = if (compactView) 1 else 3
val orientation = resources.configuration.orientation
currentSpan = if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
spanCountLandscape
} else {
spanCountPortrait
}
configEvent.invoke(currentSpan)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
fixGrid()
}
2021-07-29 00:19:42 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2021-07-29 01:46:10 +00:00
fixGrid()
2021-07-29 00:19:42 +00:00
observe(homeViewModel.apiName) {
context?.setKey(HOMEPAGE_API, it)
}
observe(homeViewModel.page) {
when (it) {
is Resource.Success -> {
val d = it.value
2021-07-29 01:46:10 +00:00
(home_master_recycler?.adapter as ParentItemAdapter?)?.items = d.items
2021-07-29 00:19:42 +00:00
home_master_recycler?.adapter?.notifyDataSetChanged()
}
is Resource.Failure -> {
}
is Resource.Loading -> {
}
}
}
2021-07-29 00:54:27 +00:00
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder> = ParentItemAdapter(listOf(), { card ->
(activity as AppCompatActivity).loadResult(card.url, card.slug, card.apiName)
2021-07-29 01:46:10 +00:00
}, { item ->
val bottomSheetDialogBuilder = BottomSheetDialog(view.context)
bottomSheetDialogBuilder.setContentView(R.layout.home_episodes_expanded)
val title = bottomSheetDialogBuilder.findViewById<TextView>(R.id.home_expanded_text)!!
title.text = item.name
val recycle = bottomSheetDialogBuilder.findViewById<AutofitRecyclerView>(R.id.home_expanded_recycler)!!
// Span settings
recycle.spanCount = currentSpan
recycle.adapter = SearchAdapter(item.list, recycle) { card ->
bottomSheetDialogBuilder.dismiss()
(activity as AppCompatActivity).loadResult(card.url, card.slug, card.apiName)
}
val spanListener = { span: Int ->
recycle.spanCount = span
(recycle.adapter as SearchAdapter).notifyDataSetChanged()
}
configEvent += spanListener
bottomSheetDialogBuilder.setOnDismissListener {
configEvent -= spanListener
}
(recycle.adapter as SearchAdapter).notifyDataSetChanged()
2021-07-29 00:19:42 +00:00
2021-07-29 01:46:10 +00:00
bottomSheetDialogBuilder.show()
2021-07-29 00:54:27 +00:00
})
context?.fixPaddingStatusbar(home_root)
2021-07-29 00:19:42 +00:00
home_master_recycler.adapter = adapter
home_master_recycler.layoutManager = GridLayoutManager(context, 1)
homeViewModel.load(context?.getKey(HOMEPAGE_API))
2021-04-30 17:20:15 +00:00
}
}