Skip to content

Commit

Permalink
Merge pull request #20402 from wordpress-mobile/hotfix/reader-persist…
Browse files Browse the repository at this point in the history
…-feed-selection

[Reader] Persist feed selection across sessions
  • Loading branch information
Thomas Horta authored Mar 4, 2024
2 parents 5efed33 + 0cb56bd commit d8b8dbe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ public enum DeletablePrefKey implements PrefKey {
READER_CARDS_ENDPOINT_PAGE_HANDLE,
// used to tell the server to return a different set of data so the content on discover tab doesn't look static
READER_CARDS_ENDPOINT_REFRESH_COUNTER,

// Used to delete recommended tags saved as followed tags in tbl_tags
// Need to be done just once for a logged out user
READER_RECOMMENDED_TAGS_DELETED_FOR_LOGGED_OUT_USER,
// Selected Reader feed ID for persisting user preferred feed
READER_TOP_BAR_SELECTED_FEED_ITEM_ID,
MANUAL_FEATURE_CONFIG,
SITE_JETPACK_CAPABILITIES,
REMOVED_QUICK_START_CARD_TYPE,
Expand Down Expand Up @@ -1193,6 +1194,19 @@ public static void setReaderRecommendedTagsDeletedForLoggedOutUser(boolean delet
setBoolean(DeletablePrefKey.READER_RECOMMENDED_TAGS_DELETED_FOR_LOGGED_OUT_USER, deleted);
}

@Nullable
public static String getReaderTopBarSelectedFeedItemId() {
return getString(DeletablePrefKey.READER_TOP_BAR_SELECTED_FEED_ITEM_ID, null);
}

public static void setReaderTopBarSelectedFeedItemId(@Nullable String selectedFeedItemId) {
if (selectedFeedItemId == null) {
remove(DeletablePrefKey.READER_TOP_BAR_SELECTED_FEED_ITEM_ID);
} else {
setString(DeletablePrefKey.READER_TOP_BAR_SELECTED_FEED_ITEM_ID, selectedFeedItemId);
}
}

public static void setShouldShowStoriesIntro(boolean shouldShow) {
setBoolean(UndeletablePrefKey.SHOULD_SHOW_STORIES_INTRO, shouldShow);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class AppPrefsWrapper @Inject constructor() {
get() = AppPrefs.getReaderCardsPageHandle()
set(pageHandle) = AppPrefs.setReaderCardsPageHandle(pageHandle)

var readerTopBarSelectedFeedItemId: String?
get() = AppPrefs.getReaderTopBarSelectedFeedItemId()
set(selectedFeedItemId) = AppPrefs.setReaderTopBarSelectedFeedItemId(selectedFeedItemId)

var shouldShowStoriesIntro: Boolean
get() = AppPrefs.shouldShowStoriesIntro()
set(shouldShow) = AppPrefs.setShouldShowStoriesIntro(shouldShow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ private void showBookmarksSavedLocallyDialog(ShowBookmarkedSavedOnlyLocallyDialo
private final ReaderInterfaces.DataLoadedListener mDataLoadedListener = new ReaderInterfaces.DataLoadedListener() {
@Override
public void onDataLoaded(boolean isEmpty) {
if (!isAdded() || !mHasUpdatedPosts) {
if (!isAdded() || (isEmpty && !mHasUpdatedPosts)) {
return;
}
if (isEmpty) {
Expand Down Expand Up @@ -2320,7 +2320,7 @@ public void run() {
requireActivity().runOnUiThread(() -> updateCurrentTag());
} else {
requireActivity().runOnUiThread(() -> {
if ((isBookmarksList()) && isPostAdapterEmpty() && isAdded()) {
if (isBookmarksList() && isPostAdapterEmpty() && isAdded()) {
setEmptyTitleAndDescriptionForBookmarksList();
mActionableEmptyView.image.setImageResource(
R.drawable.illustration_reader_empty);
Expand All @@ -2330,6 +2330,8 @@ public void run() {
R.drawable.illustration_reader_empty);
mActionableEmptyView.title.setText(
getString(R.string.reader_empty_blogs_posts_in_custom_list));
mActionableEmptyView.image.setVisibility(View.VISIBLE);
mActionableEmptyView.title.setVisibility(View.VISIBLE);
mActionableEmptyView.button.setVisibility(View.GONE);
mActionableEmptyView.subtitle.setVisibility(View.GONE);
showEmptyView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class ReaderViewModel @Inject constructor(

fun updateSelectedContent(selectedTag: ReaderTag) {
getMenuItemFromReaderTag(selectedTag)?.let { newSelectedMenuItem ->
// Persist selected feed to app prefs
appPrefsWrapper.readerTopBarSelectedFeedItemId = newSelectedMenuItem.id
// Update top bar UI state so menu is updated with new selected item
_topBarUiState.value?.let {
_topBarUiState.value = it.copy(
Expand Down Expand Up @@ -310,27 +312,33 @@ class ReaderViewModel @Inject constructor(
// if menu is exactly the same as before, don't update
if (_topBarUiState.value?.menuItems == menuItems) return@withContext


// if there's already a selected item, use it, otherwise use the first item, also try to use the saved state
// choose selected item, either from current, saved state, or persisted, falling back to first item
val savedStateSelectedId = savedInstanceState?.getString(KEY_TOP_BAR_UI_STATE_SELECTED_ITEM_ID)

val persistedSelectedId = appPrefsWrapper.readerTopBarSelectedFeedItemId

val selectedItem = _topBarUiState.value?.selectedItem
?: menuItems.filterSingleItems()
.let { singleItems ->
singleItems.firstOrNull { it.id == savedStateSelectedId } ?: singleItems.first()
singleItems.firstOrNull { it.id == savedStateSelectedId }
?: singleItems.firstOrNull { it.id == persistedSelectedId }
?: singleItems.first()
}

// if there's a selected item and filter state, also use the filter state, also try to use the saved state
val savedStateFilterUiState = savedInstanceState
?.let {
BundleCompat.getParcelable(
it,
KEY_TOP_BAR_UI_STATE_FILTER_UI_STATE,
TopBarUiState.FilterUiState::class.java
)
}
?.takeIf { selectedItem.id == savedStateSelectedId }

val filterUiState = _topBarUiState.value?.filterUiState
?.takeIf { _topBarUiState.value?.selectedItem != null }
?: savedInstanceState
?.let {
BundleCompat.getParcelable(
it,
KEY_TOP_BAR_UI_STATE_FILTER_UI_STATE,
TopBarUiState.FilterUiState::class.java
)
}
?.takeIf { selectedItem.id == savedStateSelectedId }
?: savedStateFilterUiState

_topBarUiState.postValue(
TopBarUiState(
Expand Down

0 comments on commit d8b8dbe

Please sign in to comment.