1
0
mirror of https://github.com/libre-tube/LibreTube.git synced 2024-12-14 22:30:30 +05:30

refactor: Add parcelableArrayList extension

This commit is contained in:
Isira Seneviratne 2024-01-23 16:46:35 +05:30
parent e24b0ab517
commit 588ef35296
3 changed files with 12 additions and 16 deletions
app/src/main/java/com/github/libretube

View File

@ -11,6 +11,10 @@ inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
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? {
return getSerializable(this, key, T::class.java)
}

View File

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

View File

@ -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<SelectableOption>
private lateinit var sortOptions: List<SelectableOption>
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<SelectableOption>
}
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)