Skip to content

Commit 7f069f6

Browse files
author
Thomas Horta
committed
Rename FollowedTagsChanged to FollowedTagsFetched
Upon investigation I realized that most places using this event were actually using it as a callback for the end of the fetch task rather than something that is ONLY emitted when the tags CHANGE, which was what the name implied. That only worked because of the bug mentioned in commit 3017bab that did not properly compare the local and server tags and considered all fetches as requiring a local database write, which in turn triggered the event. The constant triggering of that FollowedTagsChanged event was also the cause of bugs #20009 and #20010 since part of the code that runs there was actually only supposed to run some times. To fix that I renamed it to FollowedTagsFetched to better represent what it does and figured out which parts of the code inside `ReaderPostListFragment` event listener were supposed to run and in which scenario. I also added a `didChange` field in the `FollowedTagsFetched` event for completeness but that part might not be needed in the future, since the only code using that is legacy code related to the old subfilter which should be removed in the future.
1 parent 4a8ba19 commit 7f069f6

File tree

13 files changed

+69
-53
lines changed

13 files changed

+69
-53
lines changed

WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderEvents.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@ private ReaderEvents() {
2626
throw new AssertionError();
2727
}
2828

29-
public static class FollowedTagsChanged {
29+
public static class FollowedTagsFetched {
3030
private final boolean mDidSucceed;
31+
private final boolean mDidChange;
3132

32-
public FollowedTagsChanged(boolean didSucceed) {
33+
public FollowedTagsFetched(boolean didSucceed) {
3334
mDidSucceed = didSucceed;
35+
mDidChange = true;
36+
}
37+
38+
public FollowedTagsFetched(boolean didSucceed, boolean didChange) {
39+
mDidSucceed = didSucceed;
40+
mDidChange = didChange;
3441
}
3542

3643
public boolean didSucceed() {
3744
return mDidSucceed;
3845
}
46+
47+
public boolean didChange() {
48+
return mDidChange;
49+
}
3950
}
4051

4152
public static class TagAdded {

WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import org.wordpress.android.ui.mysite.jetpackbadge.JetpackPoweredBottomSheetFragment;
8585
import org.wordpress.android.ui.pages.SnackbarMessageHolder;
8686
import org.wordpress.android.ui.prefs.AppPrefs;
87+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched;
8788
import org.wordpress.android.ui.reader.ReaderEvents.TagAdded;
8889
import org.wordpress.android.ui.reader.ReaderTypes.ReaderPostListType;
8990
import org.wordpress.android.ui.reader.actions.ReaderActions;
@@ -934,14 +935,16 @@ private void resetPostAdapter(ReaderPostListType postListType) {
934935

935936
@SuppressWarnings("unused")
936937
@Subscribe(threadMode = ThreadMode.MAIN)
937-
public void onEventMainThread(ReaderEvents.FollowedTagsChanged event) {
938+
public void onEventMainThread(FollowedTagsFetched event) {
938939
if (getPostListType() == ReaderPostListType.TAG_FOLLOWED) {
939-
// reload the tag filter since tags have changed
940-
reloadTags();
940+
if (event.didChange()) {
941+
// reload the tag filter since tags have changed or we just opened the fragment
942+
reloadTags();
943+
}
941944

942945
// update the current tag if the list fragment is empty - this will happen if
943946
// the tag table was previously empty (ie: first run)
944-
if (isPostAdapterEmpty()) {
947+
if (isPostAdapterEmpty() && (ReaderBlogTable.hasFollowedBlogs() || !mHasUpdatedPosts)) {
945948
updateCurrentTag();
946949
}
947950
}
@@ -1945,12 +1948,7 @@ private void showBookmarksSavedLocallyDialog(ShowBookmarkedSavedOnlyLocallyDialo
19451948
private final ReaderInterfaces.DataLoadedListener mDataLoadedListener = new ReaderInterfaces.DataLoadedListener() {
19461949
@Override
19471950
public void onDataLoaded(boolean isEmpty) {
1948-
if (!isAdded()) return;
1949-
1950-
if (!mHasUpdatedPosts) {
1951-
fetchInitialData();
1952-
return;
1953-
}
1951+
if (!isAdded() || !mHasUpdatedPosts) return;
19541952

19551953
if (isEmpty) {
19561954
if (getPostListType() != ReaderPostListType.SEARCH_RESULTS
@@ -1976,20 +1974,20 @@ public void onDataLoaded(boolean isEmpty) {
19761974
}
19771975
};
19781976

1979-
private void fetchInitialData() {
1980-
if (getPostListType() == ReaderPostListType.TAG_FOLLOWED) {
1981-
reloadTags();
1982-
1983-
// update the current tag if the list fragment is empty - this will happen if
1984-
// the tag table was previously empty (ie: first run)
1985-
if (isPostAdapterEmpty()) {
1986-
updateCurrentTag();
1987-
}
1988-
}
1989-
1990-
if (mReaderViewModel != null) mReaderViewModel.loadTabs();
1991-
if (mSubFilterViewModel != null) mSubFilterViewModel.loadSubFilters();
1992-
}
1977+
// private void fetchInitialData() {
1978+
// if (getPostListType() == ReaderPostListType.TAG_FOLLOWED) {
1979+
// reloadTags();
1980+
//
1981+
// // update the current tag if the list fragment is empty - this will happen if
1982+
// // the tag table was previously empty (ie: first run)
1983+
// if (isPostAdapterEmpty()) {
1984+
// updateCurrentTag();
1985+
// }
1986+
// }
1987+
//
1988+
// if (mReaderViewModel != null) mReaderViewModel.loadTabs();
1989+
// if (mSubFilterViewModel != null) mSubFilterViewModel.loadSubFilters();
1990+
// }
19931991

19941992
private boolean isBookmarksList() {
19951993
return getPostListType() == ReaderPostListType.TAG_FOLLOWED

WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.wordpress.android.ui.LocaleAwareActivity;
4242
import org.wordpress.android.ui.RequestCodes;
4343
import org.wordpress.android.ui.prefs.AppPrefs;
44+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched;
4445
import org.wordpress.android.ui.reader.actions.ReaderActions;
4546
import org.wordpress.android.ui.reader.actions.ReaderBlogActions;
4647
import org.wordpress.android.ui.reader.actions.ReaderTagActions;
@@ -203,7 +204,7 @@ protected void onResume() {
203204

204205
@SuppressWarnings("unused")
205206
@Subscribe(threadMode = ThreadMode.MAIN)
206-
public void onEventMainThread(ReaderEvents.FollowedTagsChanged event) {
207+
public void onEventMainThread(FollowedTagsFetched event) {
207208
AppLog.d(AppLog.T.READER, "reader subs > followed tags changed");
208209
getPageAdapter().refreshFollowedTagFragment();
209210
}

WordPress/src/main/java/org/wordpress/android/ui/reader/actions/ReaderTagActions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.wordpress.android.models.ReaderTagList;
1414
import org.wordpress.android.models.ReaderTagType;
1515
import org.wordpress.android.ui.reader.ReaderConstants;
16-
import org.wordpress.android.ui.reader.ReaderEvents;
16+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched;
1717
import org.wordpress.android.ui.reader.actions.ReaderActions.ActionListener;
1818
import org.wordpress.android.ui.reader.utils.ReaderUtils;
1919
import org.wordpress.android.util.AppLog;
@@ -51,7 +51,7 @@ public static boolean deleteTag(final ReaderTag tag,
5151
private static boolean deleteTagsLocallyOnly(ActionListener actionListener, ReaderTag tag) {
5252
ReaderTagTable.deleteTag(tag);
5353
ReaderActions.callActionListener(actionListener, true);
54-
EventBus.getDefault().post(new ReaderEvents.FollowedTagsChanged(true));
54+
EventBus.getDefault().post(new FollowedTagsFetched(true));
5555

5656
return true;
5757
}
@@ -130,7 +130,7 @@ public static boolean addTags(@NonNull final List<ReaderTag> tags,
130130
private static boolean saveTagsLocallyOnly(ActionListener actionListener, ReaderTagList newTags) {
131131
ReaderTagTable.addOrUpdateTags(newTags);
132132
ReaderActions.callActionListener(actionListener, true);
133-
EventBus.getDefault().post(new ReaderEvents.FollowedTagsChanged(true));
133+
EventBus.getDefault().post(new FollowedTagsFetched(true));
134134

135135
return true;
136136
}
@@ -147,7 +147,7 @@ private static boolean saveTagsLocallyAndRemotely(ActionListener actionListener,
147147
if (actionListener != null) {
148148
ReaderActions.callActionListener(actionListener, true);
149149
}
150-
EventBus.getDefault().post(new ReaderEvents.FollowedTagsChanged(true));
150+
EventBus.getDefault().post(new FollowedTagsFetched(true));
151151
};
152152

153153
RestRequest.ErrorListener errorListener = volleyError -> {
@@ -159,7 +159,7 @@ private static boolean saveTagsLocallyAndRemotely(ActionListener actionListener,
159159
if (actionListener != null) {
160160
ReaderActions.callActionListener(actionListener, true);
161161
}
162-
EventBus.getDefault().post(new ReaderEvents.FollowedTagsChanged(true));
162+
EventBus.getDefault().post(new FollowedTagsFetched(true));
163163
return;
164164
}
165165

@@ -171,7 +171,7 @@ private static boolean saveTagsLocallyAndRemotely(ActionListener actionListener,
171171
if (actionListener != null) {
172172
ReaderActions.callActionListener(actionListener, false);
173173
}
174-
EventBus.getDefault().post(new ReaderEvents.FollowedTagsChanged(false));
174+
EventBus.getDefault().post(new FollowedTagsFetched(false));
175175
};
176176

177177
ReaderTagTable.addOrUpdateTags(newTags);

WordPress/src/main/java/org/wordpress/android/ui/reader/repository/ReaderDiscoverDataProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.wordpress.android.models.discover.ReaderDiscoverCards
1414
import org.wordpress.android.modules.IO_THREAD
1515
import org.wordpress.android.modules.UI_THREAD
1616
import org.wordpress.android.ui.reader.ReaderEvents.FetchDiscoverCardsEnded
17-
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsChanged
17+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched
1818
import org.wordpress.android.ui.reader.actions.ReaderActions.UpdateResult.CHANGED
1919
import org.wordpress.android.ui.reader.actions.ReaderActions.UpdateResult.FAILED
2020
import org.wordpress.android.ui.reader.actions.ReaderActions.UpdateResult.HAS_NEW
@@ -211,7 +211,7 @@ class ReaderDiscoverDataProvider @Inject constructor(
211211

212212
@Suppress("unused", "UNUSED_PARAMETER")
213213
@Subscribe(threadMode = BACKGROUND)
214-
fun onFollowedTagsChanged(event: FollowedTagsChanged) {
214+
fun onFollowedTagsFetched(event: FollowedTagsFetched) {
215215
launch {
216216
refreshCards()
217217
}

WordPress/src/main/java/org/wordpress/android/ui/reader/repository/usecases/tags/FetchFollowedTagsUseCase.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.wordpress.android.ui.reader.repository.usecases.tags
22

33
import org.greenrobot.eventbus.Subscribe
44
import org.greenrobot.eventbus.ThreadMode
5-
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsChanged
5+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched
66
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication
77
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Error.NetworkUnavailable
88
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Error.RemoteRequestFailure
@@ -48,7 +48,7 @@ class FetchFollowedTagsUseCase @Inject constructor(
4848

4949
@Suppress("unused")
5050
@Subscribe(threadMode = ThreadMode.BACKGROUND)
51-
fun onFollowedTagsChanged(event: FollowedTagsChanged) {
51+
fun onFollowedTagsFetched(event: FollowedTagsFetched) {
5252
val result = if (event.didSucceed()) {
5353
Success
5454
} else {

WordPress/src/main/java/org/wordpress/android/ui/reader/repository/usecases/tags/FollowInterestTagsUseCase.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.greenrobot.eventbus.Subscribe
44
import org.greenrobot.eventbus.ThreadMode
55
import org.wordpress.android.fluxc.store.AccountStore
66
import org.wordpress.android.models.ReaderTag
7-
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsChanged
7+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched
88
import org.wordpress.android.ui.reader.actions.ReaderTagActions
99
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication
1010
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Error.NetworkUnavailable
@@ -43,7 +43,7 @@ class FollowInterestTagsUseCase @Inject constructor(
4343

4444
@Suppress("unused")
4545
@Subscribe(threadMode = ThreadMode.BACKGROUND)
46-
fun onFollowedTagsChanged(event: FollowedTagsChanged) {
46+
fun onFollowedTagsFetched(event: FollowedTagsFetched) {
4747
val result = if (event.didSucceed()) {
4848
Success
4949
} else {

WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.wordpress.android.ui.prefs.AppPrefs;
2525
import org.wordpress.android.ui.reader.ReaderConstants;
2626
import org.wordpress.android.ui.reader.ReaderEvents;
27+
import org.wordpress.android.ui.reader.ReaderEvents.FollowedTagsFetched;
2728
import org.wordpress.android.ui.reader.ReaderEvents.InterestTagsFetchEnded;
2829
import org.wordpress.android.ui.reader.services.ServiceCompletionListener;
2930
import org.wordpress.android.util.AppLog;
@@ -178,6 +179,7 @@ public void run() {
178179
localTopics.addAll(ReaderTagTable.getCustomListTags());
179180
localTopics.addAll(ReaderTagTable.getDiscoverPostCardsTags());
180181

182+
boolean didChangeFollowedTags = false;
181183
if (!localTopics.isSameList(serverTopics)) {
182184
AppLog.d(AppLog.T.READER, "reader service > followed topics changed "
183185
+ "updatedDisplayNames [" + displayNameUpdateWasNeeded + "]");
@@ -193,9 +195,9 @@ public void run() {
193195
ReaderTagTable.replaceTags(serverTopics);
194196
}
195197
// broadcast the fact that there are changes
196-
// TODO thomashortadev this was being sent EVERY time
197-
EventBus.getDefault().post(new ReaderEvents.FollowedTagsChanged(true));
198+
didChangeFollowedTags = true;
198199
}
200+
EventBus.getDefault().post(new FollowedTagsFetched(true, didChangeFollowedTags));
199201
AppPrefs.setReaderTagsUpdatedTimestamp(new Date().getTime());
200202

201203
taskCompleted(UpdateTask.TAGS);

WordPress/src/main/java/org/wordpress/android/ui/reader/subfilter/SubFilterViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class SubFilterViewModel @Inject constructor(
393393

394394
@Suppress("unused", "UNUSED_PARAMETER")
395395
@Subscribe(threadMode = ThreadMode.MAIN)
396-
fun onEventMainThread(event: ReaderEvents.FollowedTagsChanged) {
396+
fun onEventMainThread(event: ReaderEvents.FollowedTagsFetched) {
397397
AppLog.d(T.READER, "Subfilter bottom sheet > followed tags changed")
398398
loadSubFilters()
399399
}

WordPress/src/main/java/org/wordpress/android/ui/reader/viewmodels/ReaderViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class ReaderViewModel @Inject constructor(
216216

217217
@Suppress("unused", "UNUSED_PARAMETER")
218218
@Subscribe(threadMode = MAIN)
219-
fun onTagsUpdated(event: ReaderEvents.FollowedTagsChanged) {
219+
fun onTagsUpdated(event: ReaderEvents.FollowedTagsFetched) {
220220
loadTabs()
221221
}
222222

0 commit comments

Comments
 (0)