@@ -7,7 +7,9 @@ import android.view.View
77import android.view.ViewGroup
88import androidx.appcompat.widget.SearchView
99import androidx.core.view.ViewCompat
10+ import androidx.core.view.isVisible
1011import androidx.fragment.app.Fragment
12+ import androidx.lifecycle.lifecycleScope
1113import androidx.recyclerview.widget.DividerItemDecoration
1214import androidx.recyclerview.widget.LinearLayoutManager
1315import com.google.android.material.snackbar.Snackbar
@@ -18,9 +20,12 @@ import com.parseus.codecinfo.data.codecinfo.getSimpleCodecInfoList
1820import com.parseus.codecinfo.data.drm.DrmSimpleInfo
1921import com.parseus.codecinfo.data.drm.getSimpleDrmInfoList
2022import com.parseus.codecinfo.databinding.TabContentLayoutBinding
23+ import com.parseus.codecinfo.ui.MainActivity
2124import com.parseus.codecinfo.ui.adapters.CodecAdapter
2225import com.parseus.codecinfo.ui.adapters.DrmAdapter
2326import com.parseus.codecinfo.ui.adapters.SearchListenerDestroyedListener
27+ import kotlinx.coroutines.Dispatchers
28+ import kotlinx.coroutines.withContext
2429
2530internal var emptyListInformed = false
2631
@@ -42,6 +47,12 @@ class ItemFragment : Fragment(), SearchView.OnQueryTextListener {
4247 override fun onDestroyView () {
4348 searchListenerDestroyedListener?.onSearchListenerDestroyed(this )
4449 searchListenerDestroyedListener = null
50+
51+ if (activity as ? MainActivity != null ) {
52+ val searchListenerList = (activity as MainActivity ).searchListeners
53+ searchListenerList.remove(this )
54+ }
55+
4556 _binding = null
4657
4758 super .onDestroyView()
@@ -51,37 +62,45 @@ class ItemFragment : Fragment(), SearchView.OnQueryTextListener {
5162 override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
5263 super .onViewCreated(view, savedInstanceState)
5364
54- val infoType = InfoType .fromInt(requireArguments().getInt(" infoType" ))
55- val itemAdapter = if (infoType != InfoType .DRM ) {
56- val codecSimpleInfoList = getSimpleCodecInfoList(requireContext(),
57- infoType == InfoType .Audio )
58- if (codecSimpleInfoList.isEmpty()) emptyList = true
59- CodecAdapter ().also {
60- if (! emptyList) {
61- it.add(codecSimpleInfoList)
65+ viewLifecycleOwner.lifecycleScope.launchWhenStarted {
66+ binding.loadingProgress.isVisible = true
67+
68+ val infoType = InfoType .fromInt(requireArguments().getInt(" infoType" ))
69+ val itemAdapter = withContext(Dispatchers .IO ) {
70+ if (infoType != InfoType .DRM ) {
71+ val codecSimpleInfoList = getSimpleCodecInfoList(requireContext(),
72+ infoType == InfoType .Audio )
73+ if (codecSimpleInfoList.isEmpty()) emptyList = true
74+ CodecAdapter ().also {
75+ if (! emptyList) {
76+ it.add(codecSimpleInfoList)
77+ }
78+ }
79+ } else {
80+ val drmSimpleInfoList = getSimpleDrmInfoList(requireContext())
81+ if (drmSimpleInfoList.isEmpty()) emptyList = true
82+ DrmAdapter (drmSimpleInfoList)
6283 }
6384 }
64- } else {
65- val drmSimpleInfoList = getSimpleDrmInfoList(requireContext())
66- if (drmSimpleInfoList.isEmpty()) emptyList = true
67- DrmAdapter (drmSimpleInfoList)
68- }
6985
70- if (! emptyList) {
71- binding.simpleCodecListView.apply {
72- layoutManager = LinearLayoutManager (context)
73- adapter = itemAdapter
74- ViewCompat .setNestedScrollingEnabled(this , false )
75- addItemDecoration(DividerItemDecoration (context, DividerItemDecoration .VERTICAL ))
76- }
77- } else if (! emptyListInformed) {
78- // Do not spam the user with multiple snackbars.
79- emptyListInformed = true
80- val errorId = if (InfoType .currentInfoType != InfoType .DRM )
81- R .string.unable_to_get_codec_info_error
86+ binding.loadingProgress.isVisible = false
87+
88+ if (! emptyList) {
89+ binding.simpleCodecListView.apply {
90+ layoutManager = LinearLayoutManager (context)
91+ adapter = itemAdapter
92+ ViewCompat .setNestedScrollingEnabled(this , false )
93+ addItemDecoration(DividerItemDecoration (context, DividerItemDecoration .VERTICAL ))
94+ }
95+ } else if (! emptyListInformed) {
96+ // Do not spam the user with multiple snackbars.
97+ emptyListInformed = true
98+ val errorId = if (InfoType .currentInfoType != InfoType .DRM )
99+ R .string.unable_to_get_codec_info_error
82100 else R .string.unable_to_get_drm_info_error
83- Snackbar .make(requireActivity().findViewById(android.R .id.content),
101+ Snackbar .make(requireActivity().findViewById(android.R .id.content),
84102 errorId, Snackbar .LENGTH_LONG ).show()
103+ }
85104 }
86105 }
87106
0 commit comments