SectionFragment.kt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.bearya.kids.section
  2. import android.os.Bundle
  3. import android.view.LayoutInflater
  4. import android.view.View
  5. import android.view.ViewGroup
  6. import android.widget.Toast
  7. import androidx.fragment.app.Fragment
  8. import androidx.fragment.app.viewModels
  9. import androidx.lifecycle.lifecycleScope
  10. import androidx.lifecycle.observe
  11. import androidx.navigation.findNavController
  12. import androidx.navigation.fragment.navArgs
  13. import androidx.recyclerview.widget.LinearLayoutManager
  14. import androidx.viewpager2.widget.ViewPager2
  15. import com.bearya.data.entity.Section
  16. import com.bearya.kids.databinding.FragmentSectionBinding
  17. import kotlinx.coroutines.*
  18. import library.ext.setData
  19. class SectionFragment : Fragment() {
  20. private lateinit var bindView: FragmentSectionBinding
  21. private val viewModel: SectionViewModel by viewModels()
  22. private val args: SectionFragmentArgs by navArgs()
  23. private val sectionQuestionAdapter: SectionQuestionAdapter by lazy { SectionQuestionAdapter(args.chapterName) }
  24. private val previewAdapter: SectionPreviewAdapter by lazy { SectionPreviewAdapter(args.chapterName) }
  25. private val pageChangeCallback by lazy {
  26. object : ViewPager2.OnPageChangeCallback() {
  27. override fun onPageSelected(position: Int) {
  28. previewAdapter.setSelectedIndex(position)
  29. }
  30. }
  31. }
  32. override fun onCreate(savedInstanceState: Bundle?) {
  33. super.onCreate(savedInstanceState)
  34. viewModel.chapterId.setData(args.chapterId)
  35. }
  36. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  37. bindView = FragmentSectionBinding.inflate(inflater, container, false)
  38. bindView.lifecycleOwner = viewLifecycleOwner
  39. return bindView.root
  40. }
  41. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  42. bindView.playPager.adapter = sectionQuestionAdapter
  43. bindView.playPager.registerOnPageChangeCallback(pageChangeCallback)
  44. sectionQuestionAdapter.onItemClickListener = { _: View, _: Section?, _: Int ->
  45. cancelHideSectionPreview()
  46. val visibility = bindView.playList.visibility.takeIf { it == View.VISIBLE }
  47. ?.let { View.GONE } ?: View.VISIBLE
  48. bindView.playList.visibility = visibility
  49. bindView.back.visibility = visibility
  50. bindView.analysis.visibility = visibility
  51. hideSectionPreview()
  52. }
  53. bindView.playList.layoutManager =
  54. LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
  55. bindView.playList.adapter = previewAdapter
  56. previewAdapter.onItemClickListener = { _: View, _: Section?, position: Int ->
  57. bindView.playPager.currentItem = position
  58. }
  59. viewModel.sections.observe(viewLifecycleOwner) {
  60. sectionQuestionAdapter.submitList(it)
  61. previewAdapter.submitList(it)
  62. }
  63. bindView.back.setOnClickListener { v -> v.findNavController().navigateUp() }
  64. bindView.analysis.setOnClickListener {
  65. val itemData = sectionQuestionAdapter.getItemData(bindView.playPager.currentItem)
  66. if (itemData?.hasAnalysis == true) {
  67. val action = SectionFragmentDirections.actionSectionFragmentToAnalysisFragment(itemData.id ?: 1 , args.chapterName)
  68. it.findNavController().navigate(action)
  69. } else {
  70. Toast.makeText(context, "暂无该答案", Toast.LENGTH_LONG).show()
  71. }
  72. }
  73. }
  74. override fun onResume() {
  75. super.onResume()
  76. hideSectionPreview()
  77. }
  78. override fun onDestroyView() {
  79. super.onDestroyView()
  80. bindView.playPager.unregisterOnPageChangeCallback(pageChangeCallback)
  81. }
  82. private var launchDelayHide: Job? = null
  83. private fun hideSectionPreview() {
  84. launchDelayHide = lifecycleScope.launch {
  85. delay(3000)
  86. withContext(Dispatchers.Main) {
  87. bindView.playList.visibility.takeIf { it == View.VISIBLE }?.also {
  88. bindView.playList.visibility = View.GONE
  89. bindView.back.visibility = View.GONE
  90. bindView.analysis.visibility = View.GONE
  91. }
  92. }
  93. }
  94. }
  95. private fun cancelHideSectionPreview() = launchDelayHide?.cancel()
  96. }