Skip to content

Commit

Permalink
Merge pull request #20113 from wordpress-mobile/issue/20012-subscribi…
Browse files Browse the repository at this point in the history
…ng-tag-update-filter-pill

[Reader IA] Subscribing to tag doesn't update the chip count
  • Loading branch information
RenanLukas authored Feb 6, 2024
2 parents dc9d7c1 + 16cae76 commit eb28864
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,13 @@ public static void showReaderSubs(Context context) {
}

public static void showReaderSubs(Context context, int selectPosition) {
Intent intent = new Intent(context, ReaderSubsActivity.class);
context.startActivity(createIntentShowReaderSubs(context, selectPosition));
}

public static Intent createIntentShowReaderSubs(@NonNull final Context context, final int selectPosition) {
final Intent intent = new Intent(context, ReaderSubsActivity.class);
intent.putExtra(ReaderConstants.ARG_SUBS_TAB_POSITION, selectPosition);
context.startActivity(intent);
return intent;
}

public static void showReaderInterests(Activity activity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,8 @@ private ReaderBlogAdapter getBlogAdapter() {
ReaderTracker.SOURCE_SETTINGS
);
mAdapter.setBlogClickListener(this);
mAdapter.setDataLoadedListener(new ReaderInterfaces.DataLoadedListener() {
@Override
public void onDataLoaded(boolean isEmpty) {
checkEmptyView();
}
mAdapter.setDataLoadedListener(isEmpty -> {
checkEmptyView();
});
}
return mAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -214,6 +216,7 @@ public class ReaderPostListFragment extends ViewPagerFragment
private int mSearchTabsPos = NO_POSITION;
private boolean mIsFilterableScreen;
private boolean mIsFiltered = false;
private ActivityResultLauncher<Intent> mReaderSubsActivityResultLauncher;
@NonNull private HashSet<UpdateAction> mCurrentUpdateActions = new HashSet<>();
/*
* called by post adapter to load older posts when user scrolls to the last post
Expand Down Expand Up @@ -652,9 +655,11 @@ private void initSubFilterViewModel(@Nullable Bundle savedInstanceState) {
mSubFilterViewModel.getBottomSheetAction().observe(getViewLifecycleOwner(), event -> {
event.applyIfNotHandled(action -> {
if (action instanceof OpenSubsAtPage) {
ReaderActivityLauncher.showReaderSubs(
requireActivity(),
((OpenSubsAtPage) action).getTabIndex()
mReaderSubsActivityResultLauncher.launch(
ReaderActivityLauncher.createIntentShowReaderSubs(
requireActivity(),
((OpenSubsAtPage) action).getTabIndex()
)
);
} else if (action instanceof OpenLoginPage) {
wpMainActivityViewModel.onOpenLoginPage();
Expand Down Expand Up @@ -846,6 +851,25 @@ public void onAttach(@NonNull Context context) {
if (context instanceof BottomNavController) {
mBottomNavController = (BottomNavController) context;
}

initReaderSubsActivityResultLauncher();
}

private void initReaderSubsActivityResultLauncher() {
mReaderSubsActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
final Intent data = result.getData();
if (data != null) {
final boolean shouldRefreshSubscriptions =
data.getBooleanExtra(ReaderSubsActivity.RESULT_SHOULD_REFRESH_SUBSCRIPTIONS, false);
if (shouldRefreshSubscriptions) {
mSubFilterViewModel.loadSubFilters();
}
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class ReaderSubsActivity extends LocaleAwareActivity
public static final int TAB_IDX_FOLLOWED_TAGS = 0;
public static final int TAB_IDX_FOLLOWED_BLOGS = 1;

public static final String RESULT_SHOULD_REFRESH_SUBSCRIPTIONS = "should_refresh_subscriptions";

@Inject AccountStore mAccountStore;
@Inject ReaderTracker mReaderTracker;

Expand All @@ -98,10 +100,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (!TextUtils.isEmpty(mLastAddedTagName)) {
EventBus.getDefault().postSticky(new ReaderEvents.TagAdded(mLastAddedTagName));
}
mReaderTracker.track(Stat.READER_MANAGE_VIEW_DISMISSED);
setResult();
CompatExtensionsKt.onBackPressedCompat(getOnBackPressedDispatcher(), this);
}
};
Expand Down Expand Up @@ -171,6 +171,20 @@ public void onPageSelected(int position) {
mReaderTracker.track(Stat.READER_MANAGE_VIEW_DISPLAYED);
}

private void setResult() {
final Intent data = new Intent();
boolean shouldRefreshSubscriptions = false;
if (mPageAdapter != null) {
final ReaderTagFragment readerTagFragment = mPageAdapter.getReaderTagFragment();
final ReaderBlogFragment readerBlogFragment = mPageAdapter.getReaderBlogFragment();
if (readerTagFragment != null && readerBlogFragment != null) {
shouldRefreshSubscriptions = readerTagFragment.hasChangedSelectedTags();
}
}
data.putExtra(RESULT_SHOULD_REFRESH_SUBSCRIPTIONS, shouldRefreshSubscriptions);
setResult(RESULT_OK, data);
}

@Override
protected void onPause() {
EventBus.getDefault().unregister(this);
Expand Down Expand Up @@ -564,12 +578,30 @@ public Object instantiateItem(ViewGroup container, int position) {
}

private void refreshFollowedTagFragment() {
for (Fragment fragment : mFragments) {
final ReaderTagFragment fragment = getReaderTagFragment();
if (fragment != null) {
fragment.refresh();
}
}

@Nullable
private ReaderTagFragment getReaderTagFragment() {
for (final Fragment fragment : mFragments) {
if (fragment instanceof ReaderTagFragment) {
ReaderTagFragment tagFragment = (ReaderTagFragment) fragment;
tagFragment.refresh();
return (ReaderTagFragment) fragment;
}
}
return null;
}

@Nullable
private ReaderBlogFragment getReaderBlogFragment() {
for (final Fragment fragment : mFragments) {
if (fragment instanceof ReaderBlogFragment) {
return (ReaderBlogFragment) fragment;
}
}
return null;
}

private void refreshBlogFragments(ReaderBlogType blogType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@

import org.wordpress.android.R;
import org.wordpress.android.models.ReaderTag;
import org.wordpress.android.models.ReaderTagList;
import org.wordpress.android.ui.ActionableEmptyView;
import org.wordpress.android.ui.reader.adapters.ReaderTagAdapter;
import org.wordpress.android.ui.reader.views.ReaderRecyclerView;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.WPActivityUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
* fragment hosted by ReaderSubsActivity which shows followed tags
*/
public class ReaderTagFragment extends Fragment implements ReaderTagAdapter.TagDeletedListener {
private ReaderRecyclerView mRecyclerView;
private ReaderTagAdapter mTagAdapter;

private boolean mIsFirstDataLoaded;
private final ReaderTagList mInitialReaderTagList = new ReaderTagList();

static ReaderTagFragment newInstance() {
AppLog.d(AppLog.T.READER, "reader tag list > newInstance");
return new ReaderTagFragment();
Expand All @@ -36,6 +44,21 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
return view;
}

public boolean hasChangedSelectedTags() {
final Set<String> initialTagsSlugs = new HashSet<>();
for (final ReaderTag readerTag : mInitialReaderTagList) {
initialTagsSlugs.add(readerTag.getTagSlug());
}
final List<ReaderTag> currentReaderTagList = getTagAdapter().getItems();
final Set<String> currentTagsSlugs = new HashSet<>();
if (currentReaderTagList != null) {
for (final ReaderTag readerTag : currentReaderTagList) {
currentTagsSlugs.add(readerTag.getTagSlug());
}
}
return !(initialTagsSlugs.equals(currentTagsSlugs));
}

private void checkEmptyView() {
if (!isAdded() || getView() == null) {
return;
Expand All @@ -62,6 +85,12 @@ public void onActivityCreated(Bundle savedInstanceState) {
refresh();
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
mIsFirstDataLoaded = true;
}

void refresh() {
if (hasTagAdapter()) {
AppLog.d(AppLog.T.READER, "reader subs > refreshing tag fragment");
Expand All @@ -74,10 +103,14 @@ private ReaderTagAdapter getTagAdapter() {
Context context = WPActivityUtils.getThemedContext(getActivity());
mTagAdapter = new ReaderTagAdapter(context);
mTagAdapter.setTagDeletedListener(this);
mTagAdapter.setDataLoadedListener(new ReaderInterfaces.DataLoadedListener() {
@Override
public void onDataLoaded(boolean isEmpty) {
checkEmptyView();
mTagAdapter.setDataLoadedListener(isEmpty -> {
checkEmptyView();
if (mIsFirstDataLoaded) {
mIsFirstDataLoaded = false;
mInitialReaderTagList.clear();
if (mTagAdapter != null && mTagAdapter.getItems() != null) {
mInitialReaderTagList.addAll(mTagAdapter.getItems());
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import org.wordpress.android.R;
Expand Down Expand Up @@ -93,6 +94,11 @@ public TagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new TagViewHolder(view);
}

@Nullable
public ReaderTagList getItems() {
return mTags;
}

@Override
public void onBindViewHolder(TagViewHolder holder, int position) {
final ReaderTag tag = mTags.get(position);
Expand Down

0 comments on commit eb28864

Please sign in to comment.