Skip to content

Commit

Permalink
Merge pull request #20157 from wordpress-mobile/issue/20148-reader-cr…
Browse files Browse the repository at this point in the history
…ash-subfilter-sheet

[Reader][Crash] Fix Subfilter sheet crash when restoring instance
  • Loading branch information
Thomas Horta authored Feb 9, 2024
2 parents 15782b1 + ceb4c52 commit b52d837
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<SubfilterCategory>,
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
Expand All @@ -66,11 +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 categories = requireNotNull(
requireArguments().getParcelableArrayListCompat<SubfilterCategory>(SUBFILTER_CATEGORIES_KEY)
)
val subfilterVmKey = requireArguments().getString(SUBFILTER_VIEW_MODEL_KEY)
val bottomSheetTitle = requireArguments().getCharSequence(SUBFILTER_TITLE_KEY)
val category = requireArguments().getSerializableCompat<SubfilterCategory>(SUBFILTER_CATEGORY_KEY)

if (subfilterVmKey == null || category == null || bottomSheetTitle == null) {
dismiss()
return
}

viewModel = ViewModelProvider(
parentFragment as ViewModelStoreOwner,
Expand All @@ -87,7 +90,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) {
Expand All @@ -110,7 +113,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubfilterCategory>
val category: SubfilterCategory
) : BottomSheetUiState(true)

object BottomSheetHidden : BottomSheetUiState(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,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 ->
Expand Down Expand Up @@ -187,7 +184,7 @@ class SubfilterPageFragment : Fragment() {
class SubfilterPagerAdapter(
val context: Context,
val fm: FragmentManager,
val subfilterViewModelKey: String,
private val subfilterViewModelKey: String,
categories: List<SubfilterCategory>
) : FragmentPagerAdapter(fm) {
private val filterCategory = categories
Expand All @@ -201,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)
}

Expand All @@ -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<SubfilterCategory> {
override fun createFromParcel(parcel: Parcel): SubfilterCategory {
return values()[parcel.readInt()]
}

override fun newArray(size: Int): Array<SubfilterCategory?> {
return arrayOfNulls(size)
}
}
}

0 comments on commit b52d837

Please sign in to comment.