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.util.ItemMedia
|
||||
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.showFragment
|
||||
import org.mosad.teapod.util.toItemMediaList
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentLibraryBinding
|
||||
private lateinit var adapter: MediaItemAdapter
|
||||
private lateinit var adapter: MediaItemListAdapter
|
||||
|
||||
private val itemList = arrayListOf<ItemMedia>()
|
||||
private val pageSize = 30
|
||||
@ -33,26 +35,23 @@ class LibraryFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
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
|
||||
lifecycleScope.launch {
|
||||
// create and set the adapter, needs context
|
||||
context?.let {
|
||||
val initialResults = Crunchyroll.browse(n = pageSize)
|
||||
itemList.addAll(initialResults.items.map { item ->
|
||||
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
})
|
||||
itemList.addAll(Crunchyroll.browse(n = pageSize).toItemMediaList())
|
||||
nextItemIndex += pageSize
|
||||
|
||||
adapter = MediaItemAdapter(itemList)
|
||||
adapter.onItemClick = { mediaIdStr, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaIdStr))
|
||||
}
|
||||
|
||||
adapter = MediaItemListAdapter(MediaItemListAdapter.OnClickListener {
|
||||
activity?.showFragment(MediaFragment(it.id))
|
||||
})
|
||||
binding.recyclerMediaLibrary.adapter = adapter
|
||||
binding.recyclerMediaLibrary.addItemDecoration(MediaItemDecoration(9))
|
||||
// TODO replace with pagination3
|
||||
// https://medium.com/swlh/paging3-recyclerview-pagination-made-easy-333c7dfa8797
|
||||
binding.recyclerMediaLibrary.addOnScrollListener(PaginationScrollListener())
|
||||
adapter.submitList(itemList)
|
||||
}
|
||||
|
||||
}
|
||||
@ -72,10 +71,7 @@ class LibraryFragment : Fragment() {
|
||||
isLoading = true
|
||||
lifecycleScope.launch {
|
||||
val firstNewItemIndex = itemList.lastIndex + 1
|
||||
val results = Crunchyroll.browse(start = nextItemIndex, n = pageSize)
|
||||
itemList.addAll(results.items.map { item ->
|
||||
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
})
|
||||
itemList.addAll(Crunchyroll.browse(start = nextItemIndex, n = pageSize).toItemMediaList())
|
||||
nextItemIndex += pageSize
|
||||
|
||||
adapter.notifyItemRangeInserted(firstNewItemIndex, pageSize)
|
||||
|
@ -36,7 +36,7 @@ class MediaItemListAdapter(private val onClickListener: OnClickListener) : ListA
|
||||
fun bind(item: ItemMedia) {
|
||||
binding.textTitle.text = item.title
|
||||
|
||||
Glide.with(binding.imagePoster)
|
||||
Glide.with(binding.root.context)
|
||||
.load(item.posterUrl)
|
||||
.into(binding.imagePoster)
|
||||
|
||||
|
Reference in New Issue
Block a user