From 79db6634b7bba13d189bdaaa22c7cbaa85341e51 Mon Sep 17 00:00:00 2001 From: Thomas Horta Date: Thu, 8 Feb 2024 17:42:01 -0300 Subject: [PATCH 1/3] Only pass a single category to the Filters Sheet The current implementation only ever expects to receive a single category but to keep most of the old code we are still using a List of categories for the sub filter logic. That is not needed on the roo SubfilterBottomSheetFragment though and the presence of the list was causing a crash since the Parcelable implementation of the SubfilterCategory was not working as expected. Removing the list from that Fragment and using a single category lets us remove the `Parcelable` requirement from the SubfilterCategory enum since we can now deal with a simple Serializable class, therefore fixing the crash. --- .../ui/reader/ReaderPostListFragment.java | 2 +- .../ui/reader/SubfilterBottomSheetFragment.kt | 15 +++++------- .../ui/reader/subfilter/BottomSheetUiState.kt | 2 +- .../ui/reader/subfilter/SubFilterViewModel.kt | 2 +- .../reader/subfilter/SubfilterPageFragment.kt | 23 +------------------ 5 files changed, 10 insertions(+), 34 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index d7bbc9b13c4b..2da410a95ab6 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -640,7 +640,7 @@ private void initSubFilterViewModel(@Nullable Bundle savedInstanceState) { BottomSheetVisible visibleState = (BottomSheetVisible) uiState; bottomSheet = SubfilterBottomSheetFragment.newInstance( SubFilterViewModel.getViewModelKeyForTag(mTagFragmentStartedWith), - visibleState.getCategories(), + visibleState.getCategory(), mUiHelpers.getTextOfUiString(requireContext(), visibleState.getTitle()) ); bottomSheet.show(getChildFragmentManager(), SUBFILTER_BOTTOM_SHEET_TAG); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt index 01de1e11aa52..287858802a62 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt @@ -24,7 +24,7 @@ import org.wordpress.android.ui.reader.subfilter.SubfilterCategory.SITES import org.wordpress.android.ui.reader.subfilter.SubfilterCategory.TAGS import org.wordpress.android.ui.reader.subfilter.SubfilterListItem.Tag import org.wordpress.android.ui.reader.subfilter.SubfilterPagerAdapter -import org.wordpress.android.util.extensions.getParcelableArrayListCompat +import org.wordpress.android.util.extensions.getSerializableCompat import javax.inject.Inject import com.google.android.material.R as MaterialR @@ -36,19 +36,19 @@ class SubfilterBottomSheetFragment : BottomSheetDialogFragment() { companion object { const val SUBFILTER_VIEW_MODEL_KEY = "subfilter_view_model_key" const val SUBFILTER_TITLE_KEY = "subfilter_title_key" - const val SUBFILTER_CATEGORIES_KEY = "subfilter_categories_key" + const val SUBFILTER_CATEGORY_KEY = "subfilter_category_key" @JvmStatic fun newInstance( subfilterViewModelKey: String, - categories: List, + category: SubfilterCategory, title: CharSequence ): SubfilterBottomSheetFragment { val fragment = SubfilterBottomSheetFragment() val bundle = Bundle() bundle.putString(SUBFILTER_VIEW_MODEL_KEY, subfilterViewModelKey) bundle.putCharSequence(SUBFILTER_TITLE_KEY, title) - bundle.putParcelableArrayList(SUBFILTER_CATEGORIES_KEY, ArrayList(categories)) + bundle.putSerializable(SUBFILTER_CATEGORY_KEY, category) fragment.arguments = bundle return fragment @@ -68,9 +68,7 @@ class SubfilterBottomSheetFragment : BottomSheetDialogFragment() { val subfilterVmKey = requireArguments().getString(SUBFILTER_VIEW_MODEL_KEY)!! val bottomSheetTitle = requireArguments().getCharSequence(SUBFILTER_TITLE_KEY)!! - val categories = requireNotNull( - requireArguments().getParcelableArrayListCompat(SUBFILTER_CATEGORIES_KEY) - ) + val category = requireArguments().getSerializableCompat(SUBFILTER_CATEGORY_KEY)!! viewModel = ViewModelProvider( parentFragment as ViewModelStoreOwner, @@ -87,7 +85,7 @@ class SubfilterBottomSheetFragment : BottomSheetDialogFragment() { requireActivity(), childFragmentManager, subfilterVmKey, - categories.toList() + listOf(category) ) pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { @@ -110,7 +108,6 @@ class SubfilterBottomSheetFragment : BottomSheetDialogFragment() { } editSubscriptions.setOnClickListener { - val category = categories.firstOrNull() ?: return@setOnClickListener val subsPageIndex = when (category) { SITES -> ReaderSubsActivity.TAB_IDX_FOLLOWED_BLOGS TAGS -> ReaderSubsActivity.TAB_IDX_FOLLOWED_TAGS diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/BottomSheetUiState.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/BottomSheetUiState.kt index 7107a7c4395a..2f163dd1ecfc 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/BottomSheetUiState.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/BottomSheetUiState.kt @@ -5,7 +5,7 @@ import org.wordpress.android.ui.utils.UiString sealed class BottomSheetUiState(val isVisible: Boolean) { data class BottomSheetVisible( val title: UiString, - val categories: List + val category: SubfilterCategory ) : BottomSheetUiState(true) object BottomSheetHidden : BottomSheetUiState(false) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubFilterViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubFilterViewModel.kt index 44d50e65e150..e331ecaeb231 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubFilterViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubFilterViewModel.kt @@ -234,7 +234,7 @@ class SubFilterViewModel @Inject constructor( _bottomSheetUiState.value = Event( BottomSheetVisible( UiStringRes(category.titleRes), - listOf(category) // TODO thomashortadev this should accept only a single category + category ) ) val source = when(category) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt index 7e7d1bd07c77..7f6f071b5232 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt @@ -4,9 +4,6 @@ package org.wordpress.android.ui.reader.subfilter import android.content.Context import android.os.Bundle -import android.os.Parcel -import android.os.Parcelable -import android.os.Parcelable.Creator import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -215,25 +212,7 @@ class SubfilterPagerAdapter( } } -enum class SubfilterCategory(@StringRes val titleRes: Int, val type: ItemType) : Parcelable { +enum class SubfilterCategory(@StringRes val titleRes: Int, val type: ItemType) { SITES(R.string.reader_filter_by_blog_title, SITE), TAGS(R.string.reader_filter_by_tag_title, TAG); - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeInt(type.ordinal) - } - - override fun describeContents(): Int { - return 0 - } - - companion object CREATOR : Creator { - override fun createFromParcel(parcel: Parcel): SubfilterCategory { - return values()[parcel.readInt()] - } - - override fun newArray(size: Int): Array { - return arrayOfNulls(size) - } - } } From 51ae6a7f94f04de9196793377a38baee661ffc5e Mon Sep 17 00:00:00 2001 From: Thomas Horta Date: Thu, 8 Feb 2024 17:45:20 -0300 Subject: [PATCH 2/3] Fix minor warnings in SubfilterPageFragment --- .../android/ui/reader/subfilter/SubfilterPageFragment.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt index 7f6f071b5232..4820df8c4599 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubfilterPageFragment.kt @@ -104,7 +104,7 @@ class SubfilterPageFragment : Fragment() { subFilterViewModel = ViewModelProvider( requireParentFragment().parentFragment as ViewModelStoreOwner, viewModelFactory - ).get(subfilterVmKey, SubFilterViewModel::class.java) + )[subfilterVmKey, SubFilterViewModel::class.java] subFilterViewModel.subFilters.observe(viewLifecycleOwner) { (recyclerView.adapter as? SubfilterListAdapter)?.let { adapter -> @@ -184,7 +184,7 @@ class SubfilterPageFragment : Fragment() { class SubfilterPagerAdapter( val context: Context, val fm: FragmentManager, - val subfilterViewModelKey: String, + private val subfilterViewModelKey: String, categories: List ) : FragmentPagerAdapter(fm) { private val filterCategory = categories @@ -198,7 +198,7 @@ class SubfilterPagerAdapter( return fragment } - override fun getPageTitle(position: Int): CharSequence? { + override fun getPageTitle(position: Int): CharSequence { return context.getString(filterCategory[position].titleRes) } From ceb4c52b070aefa95048066a20d28708dc7be930 Mon Sep 17 00:00:00 2001 From: Thomas Horta Date: Thu, 8 Feb 2024 18:07:43 -0300 Subject: [PATCH 3/3] Dismiss the Subfilter sheet if any required argument is null --- .../android/ui/reader/SubfilterBottomSheetFragment.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt index 287858802a62..ead55fbc4c12 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/SubfilterBottomSheetFragment.kt @@ -66,9 +66,14 @@ class SubfilterBottomSheetFragment : BottomSheetDialogFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val subfilterVmKey = requireArguments().getString(SUBFILTER_VIEW_MODEL_KEY)!! - val bottomSheetTitle = requireArguments().getCharSequence(SUBFILTER_TITLE_KEY)!! - val category = requireArguments().getSerializableCompat(SUBFILTER_CATEGORY_KEY)!! + val subfilterVmKey = requireArguments().getString(SUBFILTER_VIEW_MODEL_KEY) + val bottomSheetTitle = requireArguments().getCharSequence(SUBFILTER_TITLE_KEY) + val category = requireArguments().getSerializableCompat(SUBFILTER_CATEGORY_KEY) + + if (subfilterVmKey == null || category == null || bottomSheetTitle == null) { + dismiss() + return + } viewModel = ViewModelProvider( parentFragment as ViewModelStoreOwner,