Skip to content

Commit

Permalink
refactor: clean up adapter callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jarnedemeulemeester committed Sep 25, 2023
1 parent 256371d commit 0512d89
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dev.jdtech.jellyfin.databinding.CollectionItemBinding
import dev.jdtech.jellyfin.models.FindroidCollection

class CollectionListAdapter(
private val onClickListener: OnClickListener,
private val onClickListener: (collection: FindroidCollection) -> Unit,
) : ListAdapter<FindroidCollection, CollectionListAdapter.CollectionViewHolder>(DiffCallback) {
class CollectionViewHolder(private var binding: CollectionItemBinding) :
RecyclerView.ViewHolder(binding.root) {
Expand Down Expand Up @@ -43,12 +43,8 @@ class CollectionListAdapter(
override fun onBindViewHolder(holder: CollectionViewHolder, position: Int) {
val collection = getItem(position)
holder.itemView.setOnClickListener {
onClickListener.onClick(collection)
onClickListener(collection)
}
holder.bind(collection)
}

class OnClickListener(val clickListener: (collection: FindroidCollection) -> Unit) {
fun onClick(collection: FindroidCollection) = clickListener(collection)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private const val ITEM_VIEW_TYPE_HEADER = 0
private const val ITEM_VIEW_TYPE_EPISODE = 1

class EpisodeListAdapter(
private val onClickListener: OnClickListener,
private val onClickListener: (item: FindroidEpisode) -> Unit,
) :
ListAdapter<EpisodeItem, RecyclerView.ViewHolder>(DiffCallback) {

Expand Down Expand Up @@ -109,7 +109,7 @@ class EpisodeListAdapter(
ITEM_VIEW_TYPE_EPISODE -> {
val item = getItem(position) as EpisodeItem.Episode
holder.itemView.setOnClickListener {
onClickListener.onClick(item.episode)
onClickListener(item.episode)
}
(holder as EpisodeViewHolder).bind(item.episode)
}
Expand All @@ -122,8 +122,4 @@ class EpisodeListAdapter(
is EpisodeItem.Episode -> ITEM_VIEW_TYPE_EPISODE
}
}

class OnClickListener(val clickListener: (item: FindroidEpisode) -> Unit) {
fun onClick(item: FindroidEpisode) = clickListener(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ import androidx.recyclerview.widget.RecyclerView
import dev.jdtech.jellyfin.Constants
import dev.jdtech.jellyfin.databinding.FavoriteSectionBinding
import dev.jdtech.jellyfin.models.FavoriteSection
import dev.jdtech.jellyfin.models.FindroidItem

class FavoritesListAdapter(
private val onClickListener: ViewItemListAdapter.OnClickListener,
private val onEpisodeClickListener: HomeEpisodeListAdapter.OnClickListener,
private val onItemClickListener: (item: FindroidItem) -> Unit,
) : ListAdapter<FavoriteSection, FavoritesListAdapter.SectionViewHolder>(DiffCallback) {
class SectionViewHolder(private var binding: FavoriteSectionBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(
section: FavoriteSection,
onClickListener: ViewItemListAdapter.OnClickListener,
onEpisodeClickListener: HomeEpisodeListAdapter.OnClickListener,
onItemClickListener: (item: FindroidItem) -> Unit,
) {
if (section.id == Constants.FAVORITE_TYPE_MOVIES || section.id == Constants.FAVORITE_TYPE_SHOWS) {
binding.itemsRecyclerView.adapter =
ViewItemListAdapter(onClickListener, fixedWidth = true)
ViewItemListAdapter(onItemClickListener, fixedWidth = true)
(binding.itemsRecyclerView.adapter as ViewItemListAdapter).submitList(section.items)
} else if (section.id == Constants.FAVORITE_TYPE_EPISODES) {
binding.itemsRecyclerView.adapter =
HomeEpisodeListAdapter(onEpisodeClickListener)
HomeEpisodeListAdapter(onItemClickListener)
(binding.itemsRecyclerView.adapter as HomeEpisodeListAdapter).submitList(section.items)
}
binding.sectionName.text = section.name.asString(binding.root.resources)
Expand Down Expand Up @@ -58,6 +57,6 @@ class FavoritesListAdapter(

override fun onBindViewHolder(holder: SectionViewHolder, position: Int) {
val collection = getItem(position)
holder.bind(collection, onClickListener, onEpisodeClickListener)
holder.bind(collection, onItemClickListener)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import dev.jdtech.jellyfin.models.FindroidMovie
import dev.jdtech.jellyfin.models.isDownloaded
import dev.jdtech.jellyfin.core.R as CoreR

class HomeEpisodeListAdapter(private val onClickListener: OnClickListener) : ListAdapter<FindroidItem, HomeEpisodeListAdapter.EpisodeViewHolder>(DiffCallback) {
class HomeEpisodeListAdapter(private val onClickListener: (item: FindroidItem) -> Unit) : ListAdapter<FindroidItem, HomeEpisodeListAdapter.EpisodeViewHolder>(DiffCallback) {
class EpisodeViewHolder(
private var binding: HomeEpisodeItemBinding,
private val parent: ViewGroup,
Expand Down Expand Up @@ -77,12 +77,8 @@ class HomeEpisodeListAdapter(private val onClickListener: OnClickListener) : Lis
override fun onBindViewHolder(holder: EpisodeViewHolder, position: Int) {
val item = getItem(position)
holder.itemView.setOnClickListener {
onClickListener.onClick(item)
onClickListener(item)
}
holder.bind(item)
}

class OnClickListener(val clickListener: (item: FindroidItem) -> Unit) {
fun onClick(item: FindroidItem) = clickListener(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import dev.jdtech.jellyfin.databinding.ServerItemBinding
import dev.jdtech.jellyfin.models.Server

class ServerGridAdapter(
private val onClickListener: OnClickListener,
private val onLongClickListener: OnLongClickListener,
private val onClickListener: (server: Server) -> Unit,
private val onLongClickListener: (server: Server) -> Boolean,
) : ListAdapter<Server, ServerGridAdapter.ServerViewHolder>(DiffCallback) {
class ServerViewHolder(private var binding: ServerItemBinding) :
RecyclerView.ViewHolder(binding.root) {
Expand Down Expand Up @@ -39,19 +39,11 @@ class ServerGridAdapter(
override fun onBindViewHolder(holder: ServerViewHolder, position: Int) {
val server = getItem(position)
holder.itemView.setOnClickListener {
onClickListener.onClick(server)
onClickListener(server)
}
holder.itemView.setOnLongClickListener {
onLongClickListener.onLongClick(server)
onLongClickListener(server)
}
holder.bind(server)
}

class OnClickListener(val clickListener: (server: Server) -> Unit) {
fun onClick(server: Server) = clickListener(server)
}

class OnLongClickListener(val clickListener: (server: Server) -> Boolean) {
fun onLongClick(server: Server) = clickListener(server)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dev.jdtech.jellyfin.models.isDownloaded
import dev.jdtech.jellyfin.core.R as CoreR

class ViewItemListAdapter(
private val onClickListener: OnClickListener,
private val onClickListener: (item: FindroidItem) -> Unit,
private val fixedWidth: Boolean = false,
) : ListAdapter<FindroidItem, ViewItemListAdapter.ItemViewHolder>(DiffCallback) {

Expand Down Expand Up @@ -63,12 +63,8 @@ class ViewItemListAdapter(
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = getItem(position)
holder.itemView.setOnClickListener {
onClickListener.onClick(item)
onClickListener(item)
}
holder.bind(item, fixedWidth)
}

class OnClickListener(val clickListener: (item: FindroidItem) -> Unit) {
fun onClick(item: FindroidItem) = clickListener(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dev.jdtech.jellyfin.models.isDownloaded
import dev.jdtech.jellyfin.core.R as CoreR

class ViewItemPagingAdapter(
private val onClickListener: OnClickListener,
private val onClickListener: (item: FindroidItem) -> Unit,
private val fixedWidth: Boolean = false,
) : PagingDataAdapter<FindroidItem, ViewItemPagingAdapter.ItemViewHolder>(DiffCallback) {

Expand Down Expand Up @@ -65,13 +65,9 @@ class ViewItemPagingAdapter(
val item = getItem(position)
if (item != null) {
holder.itemView.setOnClickListener {
onClickListener.onClick(item)
onClickListener(item)
}
holder.bind(item, fixedWidth)
}
}

class OnClickListener(val clickListener: (item: FindroidItem) -> Unit) {
fun onClick(item: FindroidItem) = clickListener(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.recyclerview.widget.RecyclerView
import dev.jdtech.jellyfin.databinding.CardOfflineBinding
import dev.jdtech.jellyfin.databinding.NextUpSectionBinding
import dev.jdtech.jellyfin.databinding.ViewItemBinding
import dev.jdtech.jellyfin.models.FindroidItem
import dev.jdtech.jellyfin.models.HomeItem
import dev.jdtech.jellyfin.models.View
import dev.jdtech.jellyfin.core.R as CoreR
Expand All @@ -17,43 +18,45 @@ private const val ITEM_VIEW_TYPE_VIEW = 1
private const val ITEM_VIEW_TYPE_OFFLINE_CARD = 2

class ViewListAdapter(
private val onClickListener: OnClickListener,
private val onItemClickListener: ViewItemListAdapter.OnClickListener,
private val onNextUpClickListener: HomeEpisodeListAdapter.OnClickListener,
private val onOnlineClickListener: OnClickListenerOfflineCard,
private val onClickListener: (view: View) -> Unit,
private val onItemClickListener: (item: FindroidItem) -> Unit,
private val onOnlineClickListener: () -> Unit,
) : ListAdapter<HomeItem, RecyclerView.ViewHolder>(DiffCallback) {

class ViewViewHolder(private var binding: ViewItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(
dataItem: HomeItem.ViewItem,
onClickListener: OnClickListener,
onItemClickListener: ViewItemListAdapter.OnClickListener,
onClickListener: (view: View) -> Unit,
onItemClickListener: (item: FindroidItem) -> Unit,
) {
val view = dataItem.view
binding.viewName.text = binding.viewName.context.resources.getString(CoreR.string.latest_library, view.name)
binding.itemsRecyclerView.adapter =
ViewItemListAdapter(onItemClickListener, fixedWidth = true)
(binding.itemsRecyclerView.adapter as ViewItemListAdapter).submitList(view.items)
binding.viewAll.setOnClickListener {
onClickListener.onClick(view)
onClickListener(view)
}
}
}

class NextUpViewHolder(private var binding: NextUpSectionBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(section: HomeItem.Section, onClickListener: HomeEpisodeListAdapter.OnClickListener) {
fun bind(
section: HomeItem.Section,
onClickListener: (item: FindroidItem) -> Unit,
) {
binding.sectionName.text = section.homeSection.name.asString(binding.sectionName.context.resources)
binding.itemsRecyclerView.adapter = HomeEpisodeListAdapter(onClickListener)
(binding.itemsRecyclerView.adapter as HomeEpisodeListAdapter).submitList(section.homeSection.items)
}
}

class OfflineCardViewHolder(private var binding: CardOfflineBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(onClickListener: OnClickListenerOfflineCard) {
fun bind(onClickListener: () -> Unit) {
binding.onlineButton.setOnClickListener {
onClickListener.onClick()
onClickListener()
}
}
}
Expand Down Expand Up @@ -103,7 +106,7 @@ class ViewListAdapter(
when (holder.itemViewType) {
ITEM_VIEW_TYPE_NEXT_UP -> {
val view = getItem(position) as HomeItem.Section
(holder as NextUpViewHolder).bind(view, onNextUpClickListener)
(holder as NextUpViewHolder).bind(view, onItemClickListener)
}
ITEM_VIEW_TYPE_VIEW -> {
val view = getItem(position) as HomeItem.ViewItem
Expand All @@ -123,12 +126,4 @@ class ViewListAdapter(
is HomeItem.ViewItem -> ITEM_VIEW_TYPE_VIEW
}
}

class OnClickListener(val clickListener: (view: View) -> Unit) {
fun onClick(view: View) = clickListener(view)
}

class OnClickListenerOfflineCard(val clickListener: () -> Unit) {
fun onClick() = clickListener()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import dagger.hilt.android.AndroidEntryPoint
import dev.jdtech.jellyfin.adapters.FavoritesListAdapter
import dev.jdtech.jellyfin.adapters.HomeEpisodeListAdapter
import dev.jdtech.jellyfin.adapters.ViewItemListAdapter
import dev.jdtech.jellyfin.databinding.FragmentFavoriteBinding
import dev.jdtech.jellyfin.dialogs.ErrorDialogFragment
import dev.jdtech.jellyfin.models.FindroidEpisode
Expand All @@ -42,14 +40,9 @@ class CollectionFragment : Fragment() {
): View {
binding = FragmentFavoriteBinding.inflate(inflater, container, false)

binding.favoritesRecyclerView.adapter = FavoritesListAdapter(
ViewItemListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
HomeEpisodeListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
)
binding.favoritesRecyclerView.adapter = FavoritesListAdapter { item ->
navigateToMediaItem(item)
}

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import dev.jdtech.jellyfin.AppPreferences
import dev.jdtech.jellyfin.adapters.FavoritesListAdapter
import dev.jdtech.jellyfin.adapters.HomeEpisodeListAdapter
import dev.jdtech.jellyfin.adapters.ViewItemListAdapter
import dev.jdtech.jellyfin.databinding.FragmentDownloadsBinding
import dev.jdtech.jellyfin.models.FindroidItem
import dev.jdtech.jellyfin.models.FindroidMovie
Expand All @@ -43,14 +41,9 @@ class DownloadsFragment : Fragment() {
): View {
binding = FragmentDownloadsBinding.inflate(inflater, container, false)

binding.downloadsRecyclerView.adapter = FavoritesListAdapter(
ViewItemListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
HomeEpisodeListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
)
binding.downloadsRecyclerView.adapter = FavoritesListAdapter { item ->
navigateToMediaItem(item)
}

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import dagger.hilt.android.AndroidEntryPoint
import dev.jdtech.jellyfin.adapters.FavoritesListAdapter
import dev.jdtech.jellyfin.adapters.HomeEpisodeListAdapter
import dev.jdtech.jellyfin.adapters.ViewItemListAdapter
import dev.jdtech.jellyfin.databinding.FragmentFavoriteBinding
import dev.jdtech.jellyfin.dialogs.ErrorDialogFragment
import dev.jdtech.jellyfin.models.FindroidEpisode
Expand All @@ -41,14 +39,9 @@ class FavoriteFragment : Fragment() {
): View {
binding = FragmentFavoriteBinding.inflate(inflater, container, false)

binding.favoritesRecyclerView.adapter = FavoritesListAdapter(
ViewItemListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
HomeEpisodeListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
)
binding.favoritesRecyclerView.adapter = FavoritesListAdapter { item ->
navigateToMediaItem(item)
}

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import dagger.hilt.android.AndroidEntryPoint
import dev.jdtech.jellyfin.AppPreferences
import dev.jdtech.jellyfin.adapters.HomeEpisodeListAdapter
import dev.jdtech.jellyfin.adapters.ViewItemListAdapter
import dev.jdtech.jellyfin.adapters.ViewListAdapter
import dev.jdtech.jellyfin.databinding.FragmentHomeBinding
import dev.jdtech.jellyfin.dialogs.ErrorDialogFragment
Expand Down Expand Up @@ -146,14 +144,11 @@ class HomeFragment : Fragment() {
}

binding.viewsRecyclerView.adapter = ViewListAdapter(
onClickListener = ViewListAdapter.OnClickListener { navigateToLibraryFragment(it) },
onItemClickListener = ViewItemListAdapter.OnClickListener {
onClickListener = { navigateToLibraryFragment(it) },
onItemClickListener = {
navigateToMediaItem(it)
},
onNextUpClickListener = HomeEpisodeListAdapter.OnClickListener { item ->
navigateToMediaItem(item)
},
onOnlineClickListener = ViewListAdapter.OnClickListenerOfflineCard {
onOnlineClickListener = {
appPreferences.offlineMode = false
activity?.restart()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class LibraryFragment : Fragment() {

binding.itemsRecyclerView.adapter =
ViewItemPagingAdapter(
ViewItemPagingAdapter.OnClickListener { item ->
{ item ->
navigateToItem(item)
},
)
Expand Down
Loading

0 comments on commit 0512d89

Please sign in to comment.