@@ -23,6 +23,7 @@ import com.github.libretube.constants.PreferenceKeys
2323import com.github.libretube.databinding.FragmentDownloadContentBinding
2424import com.github.libretube.databinding.FragmentDownloadsBinding
2525import com.github.libretube.db.DatabaseHolder.Database
26+ import com.github.libretube.db.obj.DownloadWithItems
2627import com.github.libretube.db.obj.filterByTab
2728import com.github.libretube.extensions.ceilHalf
2829import com.github.libretube.extensions.formatAsFileSize
@@ -109,6 +110,10 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
109110 private val downloadReceiver = DownloadReceiver ()
110111 private lateinit var downloadTab: DownloadTab
111112
113+ private var selectedSortType
114+ get() = PreferenceHelper .getInt(PreferenceKeys .SELECTED_DOWNLOAD_SORT_TYPE , 0 )
115+ set(value) {PreferenceHelper .putInt(PreferenceKeys .SELECTED_DOWNLOAD_SORT_TYPE , value) }
116+
112117 private val serviceConnection = object : ServiceConnection {
113118 var isBound = false
114119 var job: Job ? = null
@@ -170,32 +175,25 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
170175 }
171176 binding.downloadsRecView.adapter = adapter
172177
173- var selectedSortType =
174- PreferenceHelper .getInt(PreferenceKeys .SELECTED_DOWNLOAD_SORT_TYPE , 0 )
175178 val filterOptions = resources.getStringArray(R .array.downloadSortOptions)
176179 binding.sortType.text = filterOptions[selectedSortType]
177- binding.sortType.setOnClickListener {
178- BaseBottomSheet ().setSimpleItems(filterOptions.toList()) { index ->
179- binding.sortType.text = filterOptions[index]
180- if (::adapter.isInitialized) {
181- sortDownloadList(index, selectedSortType)
182- }
183- selectedSortType = index
184- PreferenceHelper .putInt(
185- PreferenceKeys .SELECTED_DOWNLOAD_SORT_TYPE ,
186- index
187- )
188- }.show(childFragmentManager)
189- }
190180
191181 lifecycleScope.launch {
192- val dbDownloads = withContext(Dispatchers .IO ) {
182+ val downloads = withContext(Dispatchers .IO ) {
193183 Database .downloadDao().getAll()
194- }
184+ }.filterByTab(downloadTab)
195185
196- val downloads = dbDownloads.filterByTab(downloadTab)
197- adapter.submitList(downloads)
198- sortDownloadList(selectedSortType)
186+ submitDownloadList(downloads)
187+
188+ binding.sortType.setOnClickListener {
189+ BaseBottomSheet ().setSimpleItems(filterOptions.toList()) { index ->
190+ if (index == selectedSortType) return @setSimpleItems
191+ selectedSortType = index
192+
193+ binding.sortType.text = filterOptions[index]
194+ submitDownloadList(downloads)
195+ }.show(childFragmentManager)
196+ }
199197
200198 binding.downloadsRecView.setOnDismissListener { position ->
201199 adapter.showDeleteDialog(requireContext(), position)
@@ -231,6 +229,15 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
231229 }
232230 }
233231
232+ private fun submitDownloadList (items : List <DownloadWithItems >) {
233+ val sortedItems = when (selectedSortType) {
234+ 0 -> items
235+ else -> items.reversed()
236+ }
237+
238+ adapter.submitList(sortedItems)
239+ }
240+
234241 private fun toggleVisibilities () {
235242 val binding = _binding ? : return
236243
@@ -241,15 +248,6 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
241248 binding.shuffleAll.isGone = isEmpty || downloadTab != DownloadTab .AUDIO
242249 }
243250
244- private fun sortDownloadList (sortType : Int , previousSortType : Int? = null) {
245- if (previousSortType == null && sortType == 1 ) {
246- adapter.submitList(adapter.items.reversed())
247- }
248- if (previousSortType != null && sortType != previousSortType) {
249- adapter.submitList(adapter.items.reversed())
250- }
251- }
252-
253251 private fun showDeleteAllDialog (context : Context , adapter : DownloadsAdapter ) {
254252 var onlyDeleteWatchedVideos = false
255253
0 commit comments