-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support multiple filter selection (#5478)
Co-authored-by: Bnyro <[email protected]>
- Loading branch information
1 parent
0e960e1
commit 444eb69
Showing
10 changed files
with
348 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/github/libretube/enums/ContentFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.libretube.enums | ||
|
||
import com.github.libretube.constants.PreferenceKeys.SELECTED_FEED_FILTERS | ||
import com.github.libretube.helpers.PreferenceHelper | ||
|
||
enum class ContentFilter { | ||
VIDEOS, | ||
SHORTS, | ||
LIVESTREAMS; | ||
|
||
fun isEnabled() = enabledFiltersSet.contains(ordinal.toString()) | ||
|
||
fun setState(enabled: Boolean) { | ||
val newFilters = enabledFiltersSet | ||
.apply {if (enabled) add(ordinal.toString()) else remove(ordinal.toString()) } | ||
.joinToString(",") | ||
|
||
PreferenceHelper.putString(SELECTED_FEED_FILTERS, newFilters) | ||
} | ||
|
||
companion object { | ||
|
||
private val enabledFiltersSet get() = PreferenceHelper | ||
.getString( | ||
key = SELECTED_FEED_FILTERS, | ||
defValue = entries.joinToString(",") { it.ordinal.toString() } | ||
) | ||
.split(',') | ||
.toMutableSet() | ||
|
||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/github/libretube/obj/SelectableOption.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.libretube.obj | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
data class SelectableOption( | ||
val isSelected: Boolean, | ||
val name: String | ||
): Parcelable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.github.libretube.ui.sheets | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.RadioButton | ||
import androidx.core.os.bundleOf | ||
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.obj.SelectableOption | ||
|
||
class FilterSortBottomSheet: ExpandedBottomSheet() { | ||
|
||
private var _binding: FilterSortSheetBinding? = null | ||
private val binding get() = _binding!! | ||
|
||
private lateinit var sortOptions: Array<SelectableOption> | ||
|
||
private var selectedIndex: Int = 0 | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
sortOptions = requireArguments().getParcelableArray(IntentData.sortOptions) as Array<SelectableOption> | ||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
_binding = FilterSortSheetBinding.inflate(layoutInflater) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
addSortOptions() | ||
observeSortChanges() | ||
setInitialFiltersState() | ||
observeFiltersChanges() | ||
} | ||
|
||
private fun addSortOptions() { | ||
for (i in sortOptions.indices) { | ||
val option = sortOptions.elementAt(i) | ||
val rb = createRadioButton(i, option.name) | ||
|
||
binding.sortRadioGroup.addView(rb) | ||
|
||
if (option.isSelected) { | ||
selectedIndex = i | ||
binding.sortRadioGroup.check(rb.id) | ||
} | ||
} | ||
} | ||
|
||
private fun createRadioButton(index: Int, name: String): RadioButton { | ||
return RadioButton(context).apply { | ||
tag = index | ||
text = name | ||
} | ||
} | ||
|
||
private fun observeSortChanges() { | ||
binding.sortRadioGroup.setOnCheckedChangeListener { group, checkedId -> | ||
val index = group.findViewById<RadioButton>(checkedId).tag as Int | ||
selectedIndex = index | ||
notifyChange() | ||
} | ||
} | ||
|
||
private fun setInitialFiltersState() { | ||
binding.filterVideos.isChecked = ContentFilter.VIDEOS.isEnabled() | ||
binding.filterShorts.isChecked = ContentFilter.SHORTS.isEnabled() | ||
binding.filterLivestreams.isChecked = ContentFilter.LIVESTREAMS.isEnabled() | ||
} | ||
|
||
private fun observeFiltersChanges() { | ||
binding.filters.setOnCheckedStateChangeListener { _, _ -> | ||
ContentFilter.VIDEOS.setState(binding.filterVideos.isChecked) | ||
ContentFilter.SHORTS.setState(binding.filterShorts.isChecked) | ||
ContentFilter.LIVESTREAMS.setState(binding.filterLivestreams.isChecked) | ||
notifyChange() | ||
} | ||
} | ||
|
||
private fun notifyChange() { | ||
setFragmentResult( | ||
requestKey = FILTER_SORT_REQUEST_KEY, | ||
result = bundleOf(SELECTED_SORT_OPTION_KEY to selectedIndex) | ||
) | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
|
||
companion object { | ||
const val FILTER_SORT_REQUEST_KEY = "filter_sort_request_key" | ||
const val SELECTED_SORT_OPTION_KEY = "selected_sort_option_key" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="?colorControlNormal" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
|
||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M4.25,5.61C6.27,8.2 10,13 10,13v6c0,0.55 0.45,1 1,1h2c0.55,0 1,-0.45 1,-1v-6c0,0 3.72,-4.8 5.74,-7.39C20.25,4.95 19.78,4 18.95,4H5.04C4.21,4 3.74,4.95 4.25,5.61z" /> | ||
|
||
</vector> |
Oops, something went wrong.