Skip to content

Commit

Permalink
TraktAddFragment: re-use ItemAddShowClickListener
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Oct 17, 2024
1 parent 9399370 commit a4567b8
Showing 1 changed file with 12 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.battlelancer.seriesguide.databinding.ItemAddshowBinding
import com.battlelancer.seriesguide.enums.NetworkResult
import com.battlelancer.seriesguide.shows.tools.AddShowTask
import com.battlelancer.seriesguide.shows.tools.ShowTools2
import com.battlelancer.seriesguide.ui.OverviewActivity
import com.battlelancer.seriesguide.ui.widgets.EmptyView
import com.battlelancer.seriesguide.util.ImageTools
import com.battlelancer.seriesguide.util.TaskManager
Expand Down Expand Up @@ -130,26 +129,9 @@ class TraktAddFragment : Fragment() {
binding = null
}

private val itemClickListener: AddAdapter.ItemClickListener =
object : AddAdapter.ItemClickListener {
override fun onItemClick(item: SearchResult) {
if (item.state != SearchResult.STATE_ADDING) {
if (item.state == SearchResult.STATE_ADDED) {
// already in library, open it
startActivity(
OverviewActivity.intentShowByTmdbId(requireContext(), item.tmdbId)
)
} else {
// display more details in a dialog
AddShowDialogFragment.show(parentFragmentManager, item)
}
}
}

override fun onAddClick(item: SearchResult) {
EventBus.getDefault().post(OnAddingShowEvent(item.tmdbId))
TaskManager.getInstance().performAddTask(requireContext(), item)
}
private val itemClickListener
get() = object :
ItemAddShowClickListener(requireContext(), lifecycle, parentFragmentManager) {

override fun onMoreOptionsClick(view: View, show: SearchResult) {
AddShowPopupMenu(requireContext(), show, view).apply {
Expand Down Expand Up @@ -322,16 +304,10 @@ class TraktAddFragment : Fragment() {
class AddAdapter(
activity: Activity,
objects: List<SearchResult>,
private val itemClickListener: ItemClickListener,
private val itemClickListener: ItemAddShowViewHolder.ClickListener,
private val showWatchlistActions: Boolean
) : ArrayAdapter<SearchResult>(activity, 0, objects) {

interface ItemClickListener {
fun onItemClick(item: SearchResult)
fun onAddClick(item: SearchResult)
fun onMoreOptionsClick(view: View, show: SearchResult)
}

private fun getItemForShowTmdbId(showTmdbId: Int): SearchResult? {
val count = count
for (i in 0 until count) {
Expand Down Expand Up @@ -382,7 +358,7 @@ class TraktAddFragment : Fragment() {

class ViewHolder(
val binding: ItemAddshowBinding,
private val itemClickListener: ItemClickListener
private val itemClickListener: ItemAddShowViewHolder.ClickListener
) {
private var item: SearchResult? = null

Expand Down Expand Up @@ -467,15 +443,13 @@ class TraktAddFragment : Fragment() {
}

companion object {
fun inflate(parent: ViewGroup, itemClickListener: ItemClickListener) =
ViewHolder(
ItemAddshowBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
),
itemClickListener
)
fun inflate(
parent: ViewGroup,
itemClickListener: ItemAddShowViewHolder.ClickListener
) = ViewHolder(
ItemAddshowBinding.inflate(LayoutInflater.from(parent.context), parent, false),
itemClickListener
)
}
}
}
Expand Down

0 comments on commit a4567b8

Please sign in to comment.