LastPlayedFragment.kt 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package io.r_a_d.radio2.ui.songs.queuelp
  2. import android.os.Bundle
  3. import android.util.Log
  4. import androidx.fragment.app.Fragment
  5. import android.view.LayoutInflater
  6. import android.view.View
  7. import android.view.ViewGroup
  8. import androidx.lifecycle.Observer
  9. import androidx.recyclerview.widget.LinearLayoutManager
  10. import androidx.recyclerview.widget.RecyclerView
  11. import io.r_a_d.radio2.R
  12. import io.r_a_d.radio2.playerstore.PlayerStore
  13. /**
  14. * A simple [Fragment] subclass.
  15. * Activities that contain this fragment must implement the
  16. * [LastPlayedFragment.OnFragmentInteractionListener] interface
  17. * to handle interaction events.
  18. * Use the [LastPlayedFragment.newInstance] factory method to
  19. * create an instance of this fragment.
  20. */
  21. class LastPlayedFragment : Fragment() {
  22. private val lastPlayedFragmentTag = this::class.java.name
  23. private lateinit var recyclerView: RecyclerView
  24. private lateinit var viewAdapter: RecyclerView.Adapter<*>
  25. private lateinit var viewManager: RecyclerView.LayoutManager
  26. private val queueObserver = Observer<Boolean> {
  27. Log.d(tag, lastPlayedFragmentTag + "queue changed")
  28. viewAdapter.notifyDataSetChanged()
  29. }
  30. override fun onCreateView(
  31. inflater: LayoutInflater, container: ViewGroup?,
  32. savedInstanceState: Bundle?
  33. ): View? {
  34. // Inflate the layout for this fragment
  35. val root = inflater.inflate(R.layout.fragment_last_played, container, false)
  36. viewManager = LinearLayoutManager(context)
  37. viewAdapter = SongAdaptater(PlayerStore.instance.lp)
  38. recyclerView = root.findViewById<RecyclerView>(R.id.queue_lp_recycler).apply {
  39. // use this setting to improve performance if you know that changes
  40. // in content do not change the layout size of the RecyclerView
  41. setHasFixedSize(true)
  42. // use a linear layout manager
  43. layoutManager = viewManager
  44. // specify an viewAdapter (see also next example)
  45. adapter = viewAdapter
  46. }
  47. PlayerStore.instance.isLpUpdated.observeForever(queueObserver)
  48. return root
  49. }
  50. override fun onDestroyView() {
  51. PlayerStore.instance.isLpUpdated.removeObserver(queueObserver)
  52. super.onDestroyView()
  53. }
  54. /**
  55. * This interface must be implemented by activities that contain this
  56. * fragment to allow an interaction in this fragment to be communicated
  57. * to the activity and potentially other fragments contained in that
  58. * activity.
  59. *
  60. *
  61. * See the Android Training lesson [Communicating with Other Fragments]
  62. * (http://developer.android.com/training/basics/fragments/communicating.html)
  63. * for more information.
  64. */
  65. interface OnFragmentInteractionListener
  66. companion object {
  67. @JvmStatic
  68. fun newInstance() = LastPlayedFragment()
  69. }
  70. }