update MediaItem to suport dynamic size
* this is needed for dynamic span count to correctly work * this also fixes issues with poster image cropping when the MediaItem size was < 195dp
This commit is contained in:
		@ -13,13 +13,15 @@ import org.mosad.teapod.databinding.FragmentLibraryBinding
 | 
				
			|||||||
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
 | 
					import org.mosad.teapod.parser.crunchyroll.Crunchyroll
 | 
				
			||||||
import org.mosad.teapod.util.ItemMedia
 | 
					import org.mosad.teapod.util.ItemMedia
 | 
				
			||||||
import org.mosad.teapod.util.adapter.MediaItemAdapter
 | 
					import org.mosad.teapod.util.adapter.MediaItemAdapter
 | 
				
			||||||
 | 
					import org.mosad.teapod.util.adapter.MediaItemListAdapter
 | 
				
			||||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
 | 
					import org.mosad.teapod.util.decoration.MediaItemDecoration
 | 
				
			||||||
import org.mosad.teapod.util.showFragment
 | 
					import org.mosad.teapod.util.showFragment
 | 
				
			||||||
 | 
					import org.mosad.teapod.util.toItemMediaList
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class LibraryFragment : Fragment() {
 | 
					class LibraryFragment : Fragment() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private lateinit var binding: FragmentLibraryBinding
 | 
					    private lateinit var binding: FragmentLibraryBinding
 | 
				
			||||||
    private lateinit var adapter: MediaItemAdapter
 | 
					    private lateinit var adapter: MediaItemListAdapter
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private val itemList = arrayListOf<ItemMedia>()
 | 
					    private val itemList = arrayListOf<ItemMedia>()
 | 
				
			||||||
    private val pageSize = 30
 | 
					    private val pageSize = 30
 | 
				
			||||||
@ -33,26 +35,23 @@ class LibraryFragment : Fragment() {
 | 
				
			|||||||
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 | 
					    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 | 
				
			||||||
        super.onViewCreated(view, savedInstanceState)
 | 
					        super.onViewCreated(view, savedInstanceState)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        binding.recyclerMediaLibrary.addItemDecoration(MediaItemDecoration(9))
 | 
				
			||||||
 | 
					        // TODO replace with pagination3
 | 
				
			||||||
 | 
					        // https://medium.com/swlh/paging3-recyclerview-pagination-made-easy-333c7dfa8797
 | 
				
			||||||
 | 
					        binding.recyclerMediaLibrary.addOnScrollListener(PaginationScrollListener())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // init async
 | 
					        // init async
 | 
				
			||||||
        lifecycleScope.launch {
 | 
					        lifecycleScope.launch {
 | 
				
			||||||
            // create and set the adapter, needs context
 | 
					            // create and set the adapter, needs context
 | 
				
			||||||
            context?.let {
 | 
					            context?.let {
 | 
				
			||||||
                val initialResults = Crunchyroll.browse(n = pageSize)
 | 
					                itemList.addAll(Crunchyroll.browse(n = pageSize).toItemMediaList())
 | 
				
			||||||
                itemList.addAll(initialResults.items.map { item ->
 | 
					 | 
				
			||||||
                    ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
                nextItemIndex += pageSize
 | 
					                nextItemIndex += pageSize
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                adapter = MediaItemAdapter(itemList)
 | 
					                adapter = MediaItemListAdapter(MediaItemListAdapter.OnClickListener {
 | 
				
			||||||
                adapter.onItemClick = { mediaIdStr, _ ->
 | 
					                    activity?.showFragment(MediaFragment(it.id))
 | 
				
			||||||
                    activity?.showFragment(MediaFragment(mediaIdStr))
 | 
					                })
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                binding.recyclerMediaLibrary.adapter = adapter
 | 
					                binding.recyclerMediaLibrary.adapter = adapter
 | 
				
			||||||
                binding.recyclerMediaLibrary.addItemDecoration(MediaItemDecoration(9))
 | 
					                adapter.submitList(itemList)
 | 
				
			||||||
                // TODO replace with pagination3
 | 
					 | 
				
			||||||
                // https://medium.com/swlh/paging3-recyclerview-pagination-made-easy-333c7dfa8797
 | 
					 | 
				
			||||||
                binding.recyclerMediaLibrary.addOnScrollListener(PaginationScrollListener())
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -72,10 +71,7 @@ class LibraryFragment : Fragment() {
 | 
				
			|||||||
                    isLoading = true
 | 
					                    isLoading = true
 | 
				
			||||||
                    lifecycleScope.launch {
 | 
					                    lifecycleScope.launch {
 | 
				
			||||||
                        val firstNewItemIndex = itemList.lastIndex + 1
 | 
					                        val firstNewItemIndex = itemList.lastIndex + 1
 | 
				
			||||||
                        val results = Crunchyroll.browse(start = nextItemIndex, n = pageSize)
 | 
					                        itemList.addAll(Crunchyroll.browse(start = nextItemIndex, n = pageSize).toItemMediaList())
 | 
				
			||||||
                        itemList.addAll(results.items.map { item ->
 | 
					 | 
				
			||||||
                            ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
 | 
					 | 
				
			||||||
                        })
 | 
					 | 
				
			||||||
                        nextItemIndex += pageSize
 | 
					                        nextItemIndex += pageSize
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        adapter.notifyItemRangeInserted(firstNewItemIndex, pageSize)
 | 
					                        adapter.notifyItemRangeInserted(firstNewItemIndex, pageSize)
 | 
				
			||||||
 | 
				
			|||||||
@ -36,7 +36,7 @@ class MediaItemListAdapter(private val onClickListener: OnClickListener) : ListA
 | 
				
			|||||||
        fun bind(item: ItemMedia) {
 | 
					        fun bind(item: ItemMedia) {
 | 
				
			||||||
            binding.textTitle.text = item.title
 | 
					            binding.textTitle.text = item.title
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Glide.with(binding.imagePoster)
 | 
					            Glide.with(binding.root.context)
 | 
				
			||||||
                .load(item.posterUrl)
 | 
					                .load(item.posterUrl)
 | 
				
			||||||
                .into(binding.imagePoster)
 | 
					                .into(binding.imagePoster)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -120,7 +120,7 @@
 | 
				
			|||||||
            <LinearLayout
 | 
					            <LinearLayout
 | 
				
			||||||
                android:id="@+id/linear_up_next"
 | 
					                android:id="@+id/linear_up_next"
 | 
				
			||||||
                android:layout_width="match_parent"
 | 
					                android:layout_width="match_parent"
 | 
				
			||||||
                android:layout_height="match_parent"
 | 
					                android:layout_height="wrap_content"
 | 
				
			||||||
                android:orientation="vertical"
 | 
					                android:orientation="vertical"
 | 
				
			||||||
                android:paddingBottom="7dp">
 | 
					                android:paddingBottom="7dp">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -139,7 +139,7 @@
 | 
				
			|||||||
                <com.facebook.shimmer.ShimmerFrameLayout
 | 
					                <com.facebook.shimmer.ShimmerFrameLayout
 | 
				
			||||||
                    android:id="@+id/shimmer_layout_up_next"
 | 
					                    android:id="@+id/shimmer_layout_up_next"
 | 
				
			||||||
                    android:layout_width="match_parent"
 | 
					                    android:layout_width="match_parent"
 | 
				
			||||||
                    android:layout_height="match_parent"
 | 
					                    android:layout_height="wrap_content"
 | 
				
			||||||
                    tools:visibility="gone">
 | 
					                    tools:visibility="gone">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <LinearLayout
 | 
					                    <LinearLayout
 | 
				
			||||||
@ -159,7 +159,7 @@
 | 
				
			|||||||
                <androidx.recyclerview.widget.RecyclerView
 | 
					                <androidx.recyclerview.widget.RecyclerView
 | 
				
			||||||
                    android:id="@+id/recycler_up_next"
 | 
					                    android:id="@+id/recycler_up_next"
 | 
				
			||||||
                    android:layout_width="match_parent"
 | 
					                    android:layout_width="match_parent"
 | 
				
			||||||
                    android:layout_height="match_parent"
 | 
					                    android:layout_height="wrap_content"
 | 
				
			||||||
                    android:orientation="horizontal"
 | 
					                    android:orientation="horizontal"
 | 
				
			||||||
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 | 
					                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 | 
				
			||||||
                    tools:listitem="@layout/item_media" />
 | 
					                    tools:listitem="@layout/item_media" />
 | 
				
			||||||
@ -207,7 +207,7 @@
 | 
				
			|||||||
                <androidx.recyclerview.widget.RecyclerView
 | 
					                <androidx.recyclerview.widget.RecyclerView
 | 
				
			||||||
                    android:id="@+id/recycler_watchlist"
 | 
					                    android:id="@+id/recycler_watchlist"
 | 
				
			||||||
                    android:layout_width="match_parent"
 | 
					                    android:layout_width="match_parent"
 | 
				
			||||||
                    android:layout_height="match_parent"
 | 
					                    android:layout_height="wrap_content"
 | 
				
			||||||
                    android:orientation="horizontal"
 | 
					                    android:orientation="horizontal"
 | 
				
			||||||
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 | 
					                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 | 
				
			||||||
                    tools:listitem="@layout/item_media" />
 | 
					                    tools:listitem="@layout/item_media" />
 | 
				
			||||||
 | 
				
			|||||||
@ -10,27 +10,27 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    <androidx.constraintlayout.widget.ConstraintLayout
 | 
					    <androidx.constraintlayout.widget.ConstraintLayout
 | 
				
			||||||
        android:layout_width="wrap_content"
 | 
					        android:layout_width="wrap_content"
 | 
				
			||||||
        android:layout_height="wrap_content"
 | 
					        android:layout_height="wrap_content">
 | 
				
			||||||
        app:layout_constraintWidth_max="195dp">
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <FrameLayout
 | 
					        <FrameLayout
 | 
				
			||||||
            android:id="@+id/frame_image_progress"
 | 
					            android:id="@+id/frame_image_progress"
 | 
				
			||||||
            android:layout_width="0dp"
 | 
					            android:layout_width="wrap_content"
 | 
				
			||||||
            android:layout_height="0dp"
 | 
					            android:layout_height="0dp"
 | 
				
			||||||
            app:layout_constraintBottom_toTopOf="@+id/text_title"
 | 
					 | 
				
			||||||
            app:layout_constraintDimensionRatio="H,16:9"
 | 
					            app:layout_constraintDimensionRatio="H,16:9"
 | 
				
			||||||
 | 
					            app:layout_constraintBottom_toTopOf="@+id/text_title"
 | 
				
			||||||
            app:layout_constraintEnd_toEndOf="parent"
 | 
					            app:layout_constraintEnd_toEndOf="parent"
 | 
				
			||||||
            app:layout_constraintStart_toStartOf="parent"
 | 
					            app:layout_constraintStart_toStartOf="parent"
 | 
				
			||||||
            app:layout_constraintTop_toTopOf="parent"
 | 
					            app:layout_constraintTop_toTopOf="parent"
 | 
				
			||||||
            app:layout_constraintWidth="195dp">
 | 
					            app:layout_constrainedWidth="true"
 | 
				
			||||||
 | 
					            app:layout_constraintWidth_max="195dp">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <ImageView
 | 
					            <ImageView
 | 
				
			||||||
                android:id="@+id/image_poster"
 | 
					                android:id="@+id/image_poster"
 | 
				
			||||||
                android:layout_width="match_parent"
 | 
					                android:layout_width="wrap_content"
 | 
				
			||||||
                android:layout_height="match_parent"
 | 
					                android:layout_height="wrap_content"
 | 
				
			||||||
                android:contentDescription="@string/media_poster_desc"
 | 
					                android:contentDescription="@string/media_poster_desc"
 | 
				
			||||||
                android:scaleType="centerCrop"
 | 
					                android:scaleType="fitCenter"
 | 
				
			||||||
                tools:srcCompat="@color/imagePlaceholder" />
 | 
					                tools:srcCompat="@drawable/placeholder_image"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <ImageView
 | 
					            <ImageView
 | 
				
			||||||
                android:id="@+id/image_episode_play"
 | 
					                android:id="@+id/image_episode_play"
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user