<commit_before>package com.battlelancer.seriesguide.lists

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import android.widget.TextView
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.SgApp
import com.battlelancer.seriesguide.lists.ListsDistillationSettings.ListsSortOrderChangedEvent
import com.battlelancer.seriesguide.provider.SeriesGuideContract.ListItemTypes
import com.battlelancer.seriesguide.lists.database.SgListItemWithDetails
import com.battlelancer.seriesguide.ui.AutoGridLayoutManager
import com.battlelancer.seriesguide.ui.OverviewActivity
import com.battlelancer.seriesguide.util.Utils
import com.battlelancer.seriesguide.util.ViewTools
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Displays one user created list of shows.
 */
class SgListFragment : Fragment() {

    private var emptyView: TextView? = null

    private val model by viewModels<SgListItemViewModel> {
        SgListItemViewModelFactory(
            requireArguments().getString(ARG_LIST_ID)!!,
            requireActivity().application
        )
    }
    private val listsActivityViewModel by viewModels<ListsActivityViewModel>(ownerProducer = { requireActivity() })

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_list, container, false)
        val emptyView: TextView = view.findViewById(R.id.emptyViewList)
        ViewTools.setVectorDrawableTop(emptyView, R.drawable.ic_list_white_24dp)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val adapter = SgListItemAdapter(requireContext(), onItemClickListener)

        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerViewListItems).also {
            it.setHasFixedSize(true)
            it.layoutManager =
                AutoGridLayoutManager(
                    requireContext(), R.dimen.showgrid_columnWidth, 1, 1
                )
            it.adapter = adapter
        }

        listsActivityViewModel.scrollTabToTopLiveData
            .observe(viewLifecycleOwner, { tabPosition: Int? ->
                if (tabPosition != null
                    && tabPosition == requireArguments().getInt(ARG_LIST_POSITION)) {
                    recyclerView.smoothScrollToPosition(0)
                }
            })

        model.sgListItemLiveData.observe(viewLifecycleOwner, {
            recyclerView.isGone = it.isEmpty()
            emptyView?.isGone = it.isNotEmpty()
            adapter.submitList(it)
        })
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        emptyView = null
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(@Suppress("UNUSED_PARAMETER") event: ListsSortOrderChangedEvent?) {
        // sort order has changed, reload lists
        model.updateQuery()
    }

    private val onItemClickListener: SgListItemAdapter.OnItemClickListener =
        object : SgListItemAdapter.OnItemClickListener {
            override fun onItemClick(anchor: View, item: SgListItemWithDetails) {
                Utils.startActivityWithAnimation(
                    requireActivity(),
                    OverviewActivity.intentShow(requireActivity(), item.showId),
                    anchor<commit_msg>View binding: use in SgListFragment.<commit_after>package com.battlelancer.seriesguide.lists

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.SgApp
import com.battlelancer.seriesguide.databinding.FragmentListBinding
import com.battlelancer.seriesguide.lists.ListsDistillationSettings.ListsSortOrderChangedEvent
import com.battlelancer.seriesguide.lists.database.SgListItemWithDetails
import com.battlelancer.seriesguide.provider.SeriesGuideContract.ListItemTypes
import com.battlelancer.seriesguide.ui.AutoGridLayoutManager
import com.battlelancer.seriesguide.ui.OverviewActivity
import com.battlelancer.seriesguide.util.Utils
import com.battlelancer.seriesguide.util.ViewTools
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Displays one user created list of shows.
 */
class SgListFragment : Fragment() {

    private var binding: FragmentListBinding? = null

    private val model by viewModels<SgListItemViewModel> {
        SgListItemViewModelFactory(
            requireArguments().getString(ARG_LIST_ID)!!,
            requireActivity().application
        )
    }
    private val listsActivityViewModel by viewModels<ListsActivityViewModel>(ownerProducer = { requireActivity() })

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        return FragmentListBinding.inflate(layoutInflater, container, false)
            .also { binding = it }
            .root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val binding = binding!!

        ViewTools.setVectorDrawableTop(binding.emptyViewList, R.drawable.ic_list_white_24dp)

        val adapter = SgListItemAdapter(requireContext(), onItemClickListener)

        val recyclerView = binding.recyclerViewListItems.also {
            it.setHasFixedSize(true)
            it.layoutManager =
                AutoGridLayoutManager(
                    requireContext(), R.dimen.showgrid_columnWidth, 1, 1
                )
            it.adapter = adapter
        }

        listsActivityViewModel.scrollTabToTopLiveData
            .observe(viewLifecycleOwner) { tabPosition: Int? ->
                if (tabPosition != null
                    && tabPosition == requireArguments().getInt(ARG_LIST_POSITION)) {
                    recyclerView.smoothScrollToPosition(0)
                }
            }

        model.sgListItemLiveData.observe(viewLifecycleOwner) {
            val bindingOnDemand = this.binding ?: return@observe
            bindingOnDemand.recyclerViewListItems.isGone = it.isEmpty()
            bindingOnDemand.emptyViewList.isGone = it.isNotEmpty()
            adapter.submitList(it)
        }
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(@Suppress("UNUSED_PARAMETER") event: ListsSortOrderChangedEvent?) {
        // sort order has changed, reload lists
        model.updateQuery()
    }

    private val onItemClickListener: SgListItemAdapter.OnItemClickListener =
        object : SgListItemAdapter.OnItemClickListener {
            override fun onItemClick(anchor: View, item: SgListItemWithDetails) {
                Utils.startActivityWithAnimation(
                    requireActivity(),
                    OverviewActivity.intentShow(requireActivity(), item.showId),
                    anchor
{'fraction_non_alphanumeric': 0.08058830679470037, 'fraction_numerical': 0.0012155099064057372, 'mean_word_length': 3.1037406483790524, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016287', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2156, 'n_words': 385}