Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reader IA] Subscribing to tag doesn't update the chip count #20113

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,5 +1,6 @@
package org.wordpress.android.ui.reader;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
Expand All @@ -17,6 +18,7 @@
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.ReaderBlog;
import org.wordpress.android.models.ReaderBlogList;
import org.wordpress.android.ui.ActionableEmptyView;
import org.wordpress.android.ui.reader.adapters.ReaderBlogAdapter;
import org.wordpress.android.ui.reader.adapters.ReaderBlogAdapter.ReaderBlogType;
Expand All @@ -39,6 +41,9 @@ public class ReaderBlogFragment extends Fragment

@Inject ReaderTracker mReaderTracker;

private boolean mIsFirstDataLoaded;
private final ReaderBlogList mInitialReaderBlogList = new ReaderBlogList();

private static final String ARG_BLOG_TYPE = "blog_type";
private static final String KEY_SEARCH_FILTER = "search_filter";

Expand Down Expand Up @@ -147,6 +152,12 @@ public void onResume() {
refresh();
}

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

/*
* note this will only be called for followed blogs
*/
Expand Down Expand Up @@ -227,10 +238,14 @@ 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();
if (mIsFirstDataLoaded) {
mIsFirstDataLoaded = false;
mInitialReaderBlogList.clear();
if (mAdapter != null && mAdapter.getItems() != null) {
mInitialReaderBlogList.addAll(mAdapter.getItems());
}
}
});
}
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 @@ -10,6 +10,7 @@
import android.widget.ImageView;
import android.widget.TextView;

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

import org.wordpress.android.R;
Expand Down Expand Up @@ -99,6 +100,11 @@ public void refresh() {
new LoadBlogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

@Nullable
public ReaderBlogList getItems() {
return mFollowedBlogs;
}

private ReaderBlogType getBlogType() {
return mBlogType;
}
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
Loading