Merge pull request #5543 from Isira-Seneviratne/parcelableArray

refactor: Add parcelableArrayList extension
This commit is contained in:
Isira Seneviratne 2024-01-24 21:28:53 +05:30 committed by GitHub
commit 75cdfae3d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 16 deletions

View File

@ -11,6 +11,10 @@ inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java) return BundleCompat.getParcelable(this, key, T::class.java)
} }
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String?): ArrayList<T>? {
return BundleCompat.getParcelableArrayList(this, key, T::class.java)
}
inline fun <reified T : Serializable> Bundle.serializable(key: String?): T? { inline fun <reified T : Serializable> Bundle.serializable(key: String?): T? {
return getSerializable(this, key, T::class.java) return getSerializable(this, key, T::class.java)
} }

View File

@ -190,13 +190,11 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
} }
} }
private fun fetchSortOptions(): Array<SelectableOption> { private fun fetchSortOptions(): List<SelectableOption> {
return resources return resources.getStringArray(R.array.sortOptions)
.getStringArray(R.array.sortOptions)
.mapIndexed { index, option -> .mapIndexed { index, option ->
SelectableOption(isSelected = index == selectedSortOrder, name = option) SelectableOption(isSelected = index == selectedSortOrder, name = option)
} }
.toTypedArray()
} }
override fun onDestroyView() { override fun onDestroyView() {

View File

@ -1,6 +1,5 @@
package com.github.libretube.ui.sheets package com.github.libretube.ui.sheets
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -11,26 +10,22 @@ import androidx.fragment.app.setFragmentResult
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.FilterSortSheetBinding import com.github.libretube.databinding.FilterSortSheetBinding
import com.github.libretube.enums.ContentFilter import com.github.libretube.enums.ContentFilter
import com.github.libretube.extensions.parcelableArrayList
import com.github.libretube.obj.SelectableOption import com.github.libretube.obj.SelectableOption
class FilterSortBottomSheet: ExpandedBottomSheet() { class FilterSortBottomSheet: ExpandedBottomSheet() {
private var _binding: FilterSortSheetBinding? = null private var _binding: FilterSortSheetBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
private lateinit var sortOptions: Array<SelectableOption> private lateinit var sortOptions: List<SelectableOption>
private var selectedIndex = 0 private var selectedIndex = 0
private var hideWatched = false private var hideWatched = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
sortOptions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { val arguments = requireArguments()
requireArguments().getParcelableArray(IntentData.sortOptions, SelectableOption::class.java)!! sortOptions = arguments.parcelableArrayList(IntentData.sortOptions)!!
} else { hideWatched = arguments.getBoolean(IntentData.hideWatched)
@Suppress("DEPRECATION")
requireArguments().getParcelableArray(IntentData.sortOptions) as Array<SelectableOption>
}
hideWatched = requireArguments().getBoolean(IntentData.hideWatched)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
@ -52,8 +47,7 @@ class FilterSortBottomSheet: ExpandedBottomSheet() {
} }
private fun addSortOptions() { private fun addSortOptions() {
for (i in sortOptions.indices) { sortOptions.forEachIndexed { i, option ->
val option = sortOptions.elementAt(i)
val rb = createRadioButton(i, option.name) val rb = createRadioButton(i, option.name)
binding.sortRadioGroup.addView(rb) binding.sortRadioGroup.addView(rb)