From 588ef352966139ee36f09d52a911bdc0d19074ba Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Tue, 23 Jan 2024 16:46:35 +0530 Subject: [PATCH] refactor: Add parcelableArrayList extension --- .../com/github/libretube/extensions/Bundle.kt | 4 ++++ .../ui/fragments/SubscriptionsFragment.kt | 6 ++---- .../ui/sheets/FilterSortBottomSheet.kt | 18 ++++++------------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/extensions/Bundle.kt b/app/src/main/java/com/github/libretube/extensions/Bundle.kt index e5da960549..5c011fdbfc 100644 --- a/app/src/main/java/com/github/libretube/extensions/Bundle.kt +++ b/app/src/main/java/com/github/libretube/extensions/Bundle.kt @@ -11,6 +11,10 @@ inline fun Bundle.parcelable(key: String?): T? { return BundleCompat.getParcelable(this, key, T::class.java) } +inline fun Bundle.parcelableArrayList(key: String?): ArrayList? { + return BundleCompat.getParcelableArrayList(this, key, T::class.java) +} + inline fun Bundle.serializable(key: String?): T? { return getSerializable(this, key, T::class.java) } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt index 3de60156e2..06e82b6bb9 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt @@ -190,13 +190,11 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() { } } - private fun fetchSortOptions(): Array { - return resources - .getStringArray(R.array.sortOptions) + private fun fetchSortOptions(): List { + return resources.getStringArray(R.array.sortOptions) .mapIndexed { index, option -> SelectableOption(isSelected = index == selectedSortOrder, name = option) } - .toTypedArray() } override fun onDestroyView() { diff --git a/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt index 621f021ace..4711e9d79e 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt @@ -1,6 +1,5 @@ package com.github.libretube.ui.sheets -import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -11,26 +10,22 @@ import androidx.fragment.app.setFragmentResult import com.github.libretube.constants.IntentData import com.github.libretube.databinding.FilterSortSheetBinding import com.github.libretube.enums.ContentFilter +import com.github.libretube.extensions.parcelableArrayList import com.github.libretube.obj.SelectableOption class FilterSortBottomSheet: ExpandedBottomSheet() { - private var _binding: FilterSortSheetBinding? = null private val binding get() = _binding!! - private lateinit var sortOptions: Array + private lateinit var sortOptions: List private var selectedIndex = 0 private var hideWatched = false override fun onCreate(savedInstanceState: Bundle?) { - sortOptions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - requireArguments().getParcelableArray(IntentData.sortOptions, SelectableOption::class.java)!! - } else { - @Suppress("DEPRECATION") - requireArguments().getParcelableArray(IntentData.sortOptions) as Array - } - hideWatched = requireArguments().getBoolean(IntentData.hideWatched) + val arguments = requireArguments() + sortOptions = arguments.parcelableArrayList(IntentData.sortOptions)!! + hideWatched = arguments.getBoolean(IntentData.hideWatched) super.onCreate(savedInstanceState) } @@ -52,8 +47,7 @@ class FilterSortBottomSheet: ExpandedBottomSheet() { } private fun addSortOptions() { - for (i in sortOptions.indices) { - val option = sortOptions.elementAt(i) + sortOptions.forEachIndexed { i, option -> val rb = createRadioButton(i, option.name) binding.sortRadioGroup.addView(rb)