From 483e49b2cff5b830dd1b859be054bf45bc59b453 Mon Sep 17 00:00:00 2001 From: Amandeep Singh <57549340+amandeepsinghjamwal@users.noreply.github.com> Date: Sun, 14 Jul 2024 17:01:36 +0530 Subject: [PATCH] feat: added sorting method in downloads (#6194) Co-authored-by: Bnyro --- .../libretube/constants/PreferenceKeys.kt | 1 + .../ui/fragments/DownloadsFragment.kt | 40 ++++++++++++++-- .../main/res/layout/fragment_downloads.xml | 46 +++++++++++++++---- app/src/main/res/values/array.xml | 5 ++ 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 56fce5f7c..1ff020f85 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -140,4 +140,5 @@ object PreferenceKeys { const val AUTH_PREF_FILE = "auth" const val IMAGE_PROXY_URL = "image_proxy_url" const val SELECTED_CHANNEL_GROUP = "selected_channel_group" + const val SELECTED_DOWNLOAD_SORT_TYPE = "selected_download_sort_type" } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index 7ac6bf527..cc468f1b0 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -19,6 +19,7 @@ import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R +import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.FragmentDownloadsBinding import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.obj.DownloadWithItems @@ -26,11 +27,13 @@ import com.github.libretube.extensions.ceilHalf import com.github.libretube.extensions.formatAsFileSize import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.DownloadHelper +import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.obj.DownloadStatus import com.github.libretube.receivers.DownloadReceiver import com.github.libretube.services.DownloadService import com.github.libretube.ui.adapters.DownloadsAdapter import com.github.libretube.ui.base.DynamicLayoutManagerFragment +import com.github.libretube.ui.sheets.BaseBottomSheet import com.github.libretube.ui.viewholders.DownloadsViewHolder import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers @@ -43,7 +46,7 @@ import kotlin.io.path.fileSize class DownloadsFragment : DynamicLayoutManagerFragment() { private var _binding: FragmentDownloadsBinding? = null private val binding get() = _binding!! - + private lateinit var adapter: DownloadsAdapter private var binder: DownloadService.LocalBinder? = null private val downloads = mutableListOf() private val downloadReceiver = DownloadReceiver() @@ -85,6 +88,23 @@ class DownloadsFragment : DynamicLayoutManagerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.deleteAll.isInvisible = true + var selectedSortType = + PreferenceHelper.getInt(PreferenceKeys.SELECTED_DOWNLOAD_SORT_TYPE, 0) + val filterOptions = resources.getStringArray(R.array.downloadSortOptions) + binding.sortType.text = filterOptions[selectedSortType] + binding.sortType.setOnClickListener { + BaseBottomSheet().setSimpleItems(filterOptions.toList()) { index -> + binding.sortType.text = filterOptions[index] + if (::adapter.isInitialized) { + sortDownloadList(index, selectedSortType) + } + selectedSortType = index + PreferenceHelper.putInt( + PreferenceKeys.SELECTED_DOWNLOAD_SORT_TYPE, + index + ) + }.show(childFragmentManager) + } val dbDownloads = runBlocking(Dispatchers.IO) { Database.downloadDao().getAll() @@ -92,10 +112,9 @@ class DownloadsFragment : DynamicLayoutManagerFragment() { downloads.clear() downloads.addAll(dbDownloads) - binding.downloadsEmpty.isGone = true binding.downloads.isVisible = true - val adapter = DownloadsAdapter(requireContext(), downloads) { + adapter = DownloadsAdapter(requireContext(), downloads) { var isDownloading = false val ids = it.downloadItems .filter { item -> item.path.fileSize() < item.downloadSize } @@ -120,7 +139,7 @@ class DownloadsFragment : DynamicLayoutManagerFragment() { } return@DownloadsAdapter isDownloading.not() } - + sortDownloadList(selectedSortType) binding.downloads.adapter = adapter val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) { @@ -168,6 +187,17 @@ class DownloadsFragment : DynamicLayoutManagerFragment() { } } + private fun sortDownloadList(sortType: Int, previousSortType: Int? = null) { + if (previousSortType == null && sortType == 1) { + downloads.reverse() + adapter.notifyDataSetChanged() + } + if (previousSortType != null && sortType != previousSortType) { + downloads.reverse() + adapter.notifyDataSetChanged() + } + } + private fun showDeleteAllDialog(context: Context, adapter: DownloadsAdapter) { MaterialAlertDialogBuilder(context) .setTitle(R.string.delete_all) @@ -237,7 +267,7 @@ class DownloadsFragment : DynamicLayoutManagerFragment() { if (progressBar.isIndeterminate) return progressBar.incrementProgressBy(status.progress.toInt()) val progressInfo = progressBar.progress.formatAsFileSize() + - " /\n" + progressBar.max.formatAsFileSize() + " /\n" + progressBar.max.formatAsFileSize() fileSize.text = progressInfo } diff --git a/app/src/main/res/layout/fragment_downloads.xml b/app/src/main/res/layout/fragment_downloads.xml index 7503b5b71..dc7c09e0f 100644 --- a/app/src/main/res/layout/fragment_downloads.xml +++ b/app/src/main/res/layout/fragment_downloads.xml @@ -1,16 +1,38 @@ - + + + android:orientation="vertical" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toTopOf="parent"> + android:layout_height="0dp" + android:visibility="gone" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/sort_type" /> - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index b4401bd31..686e0782c 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -312,6 +312,11 @@ @string/alphabetic_reversed + + @string/least_recent + @string/most_recent + + @string/all @string/videos