Browse Source

use fragment as scope for MediaFragmentViewModel

develop
Jannik 3 months ago
parent
commit
3a71bdd2c7
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 10
      app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/MediaFragment.kt
  2. 4
      app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/MediaFragmentEpisodes.kt
  3. 4
      app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/MediaFragmentSimilar.kt
  4. 4
      app/src/main/res/layout/fragment_media_episodes.xml

10
app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/MediaFragment.kt

@ -8,8 +8,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bumptech.glide.Glide
@ -37,7 +36,7 @@ class MediaFragment(private val mediaIdStr: String) : Fragment() {
private lateinit var binding: FragmentMediaBinding
private lateinit var pagerAdapter: FragmentStateAdapter
private val model: MediaFragmentViewModel by activityViewModels()
private val model: MediaFragmentViewModel by viewModels()
private val fragments = arrayListOf<Fragment>()
private var watchlistJobRunning = false
@ -54,7 +53,7 @@ class MediaFragment(private val mediaIdStr: String) : Fragment() {
binding.frameLoading.visibility = View.VISIBLE
// tab layout and pager
pagerAdapter = ScreenSlidePagerAdapter(requireActivity())
pagerAdapter = ScreenSlidePagerAdapter(this)
// fix material components issue #1878, if more tabs are added increase
binding.pagerEpisodesSimilar.offscreenPageLimit = 2
binding.pagerEpisodesSimilar.adapter = pagerAdapter
@ -135,7 +134,6 @@ class MediaFragment(private val mediaIdStr: String) : Fragment() {
fragments.clear()
pagerAdapter.notifyItemRangeRemoved(0, fragmentsSize)
// add the episodes fragment (as tab). Note: Movies are tv shows!
MediaFragmentEpisodes().also {
fragments.add(it)
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
@ -224,7 +222,7 @@ class MediaFragment(private val mediaIdStr: String) : Fragment() {
/**
* A simple pager adapter
*/
private inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
private inner class ScreenSlidePagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]

4
app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/MediaFragmentEpisodes.kt

@ -8,7 +8,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.mosad.teapod.R
@ -22,7 +22,7 @@ class MediaFragmentEpisodes : Fragment() {
private lateinit var binding: FragmentMediaEpisodesBinding
private lateinit var adapterRecEpisodes: EpisodeItemAdapter
private val model: MediaFragmentViewModel by activityViewModels()
private val model: MediaFragmentViewModel by viewModels({requireParentFragment()})
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentMediaEpisodesBinding.inflate(inflater, container, false)

4
app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/MediaFragmentSimilar.kt

@ -5,7 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import org.mosad.teapod.databinding.FragmentMediaSimilarBinding
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
import org.mosad.teapod.util.adapter.MediaItemAdapter
@ -16,7 +16,7 @@ import org.mosad.teapod.util.toItemMediaList
class MediaFragmentSimilar : Fragment() {
private lateinit var binding: FragmentMediaSimilarBinding
private val model: MediaFragmentViewModel by activityViewModels()
private val model: MediaFragmentViewModel by viewModels({requireParentFragment()})
private lateinit var adapterSimilar: MediaItemAdapter

4
app/src/main/res/layout/fragment_media_episodes.xml

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_episodes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close