Skip to content

Commit

Permalink
Merge pull request #20477 from wordpress-mobile/issue/19969-locale-no…
Browse files Browse the repository at this point in the history
…t-working-reader-requests

[Reader] Fix `Reader` showing posts from a language different than the selected one
  • Loading branch information
RenanLukas authored Mar 18, 2024
2 parents 54e3d3b + 315f71d commit 0212137
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ public static ReaderPost fromJson(JSONObject json) {
// if there's no featured image, check if featured media has been set to an image
if (!post.hasFeaturedImage() && json.has("featured_media")) {
JSONObject jsonMedia = json.optJSONObject("featured_media");
String type = JSONUtils.getString(jsonMedia, "type");
if (type.equals("image")) {
post.mFeaturedImage = JSONUtils.getString(jsonMedia, "uri");
if (jsonMedia != null) {
String type = JSONUtils.getString(jsonMedia, "type");
if (type.equals("image")) {
post.mFeaturedImage = JSONUtils.getString(jsonMedia, "uri");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RecommendApiCallsProvider @Inject constructor(
cont.resume(Failure(errorMessage))
}

restClientProvider.getRestClientUtilsV2().get(
restClientProvider.getRestClientUtilsV2().getWithLocale(
endPointPath,
listener,
errorListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void getPushNotificationSettings(Context context, RestRequest.List
if (!TextUtils.isEmpty(deviceID)) {
settingsEndpoint += "?device_id=" + deviceID;
}
WordPress.getRestClientUtilsV1_1().get(settingsEndpoint, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(settingsEndpoint, listener, errorListener);
}

public static void registerDeviceForPushNotifications(final Context ctx, String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class InviteLinksApiCallsProvider @Inject constructor(
cont.resume(Failure(error))
}

WordPress.getRestClientUtilsV1_1().get(
WordPress.getRestClientUtilsV1_1().getWithLocale(
endPointPath,
listener,
errorListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void onErrorResponse(VolleyError volleyError) {
params.put("order_by", "display_name");
params.put("order", "ASC");
String path = String.format(Locale.US, "sites/%d/users", site.getSiteId());
WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, params, null, listener, errorListener);
}

public static void fetchAuthors(final SiteModel site, final int offset, final FetchUsersCallback callback) {
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void fetchAuthors(final SiteModel site, final int offset, final Fe
params.put("order", "ASC");
params.put("authors_only", "true");
String path = String.format(Locale.US, "sites/%d/users", site.getSiteId());
WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, params, null, listener, errorListener);
}

public static void fetchRevisionAuthorsDetails(final SiteModel site, List<String> authors,
Expand Down Expand Up @@ -144,7 +144,7 @@ public void onErrorResponse(VolleyError volleyError) {
site.getSiteId(), authors.get(i)));
}

WordPress.getRestClientUtilsV1_1().get("batch/", batchParams, null, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale("batch/", batchParams, null, listener, errorListener);
}

public static void fetchFollowers(final SiteModel site, final int page, final FetchFollowersCallback callback) {
Expand Down Expand Up @@ -195,7 +195,7 @@ public void onErrorResponse(VolleyError volleyError) {
params.put("page", Integer.toString(page));
params.put("type", isEmailFollower ? "email" : "wp_com");
String path = String.format(Locale.US, "sites/%d/stats/followers", site.getSiteId());
WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, params, null, listener, errorListener);
}

public static void fetchViewers(final SiteModel site, final int offset, final FetchViewersCallback callback) {
Expand Down Expand Up @@ -233,7 +233,7 @@ public void onErrorResponse(VolleyError volleyError) {
params.put("number", Integer.toString(FETCH_LIMIT));
params.put("page", Integer.toString(page));
String path = String.format(Locale.US, "sites/%d/viewers", site.getSiteId());
WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, params, null, listener, errorListener);
}

public static void updateRole(final SiteModel site, long personID, String newRole, final int localTableBlogId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void onErrorResponse(VolleyError volleyError) {
};

String path = "/me/keyring-connections";
WordPress.getRestClientUtilsV1_1().get(path, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, listener, errorListener);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PublicizeUpdateServicesV2 @Inject constructor(
}
val errorListener = RestRequest.ErrorListener { volleyError -> failure(volleyError) }
val path = "sites/$siteId/external-services?type=publicize"
restClientProvider.getRestClientUtilsV2().get(path, listener, errorListener)
restClientProvider.getRestClientUtilsV2().getWithLocale(path, listener, errorListener)
}

/*
Expand All @@ -45,6 +45,6 @@ class PublicizeUpdateServicesV2 @Inject constructor(
}
val errorListener = RestRequest.ErrorListener { volleyError -> failure(volleyError) }
val path = String.format(Locale.ROOT, "sites/%d/publicize-connections", siteId)
restClientProvider.getRestClientUtilsV1_1().get(path, listener, errorListener)
restClientProvider.getRestClientUtilsV1_1().getWithLocale(path, listener, errorListener)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,11 @@ public void onErrorResponse(VolleyError volleyError) {
};

if (hasBlogId) {
WordPress.getRestClientUtilsV1_1().get("read/sites/" + blogId, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale("read/sites/" + blogId, listener, errorListener);
} else {
WordPress.getRestClientUtilsV1_1()
.get("read/sites/" + UrlUtils.urlEncode(UrlUtils.getHost(blogUrl)), listener, errorListener);
.getWithLocale("read/sites/" + UrlUtils.urlEncode(UrlUtils.getHost(blogUrl)), listener,
errorListener);
}
}

Expand Down Expand Up @@ -438,7 +439,7 @@ public void onErrorResponse(VolleyError volleyError) {
} else {
path = "read/feed/" + UrlUtils.urlEncode(feedUrl);
}
WordPress.getRestClientUtilsV1_1().get(path, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, listener, errorListener);
}

private static void handleUpdateBlogInfoResponse(JSONObject jsonObject, UpdateBlogInfoListener infoListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void onErrorResponse(VolleyError volleyError) {
}
};
AppLog.d(T.READER, "updating post");
WordPress.getRestClientUtilsV1_2().get(path, null, null, listener, errorListener);
WordPress.getRestClientUtilsV1_2().getWithLocale(path, null, null, listener, errorListener);
}

private static void handleUpdatePostResponse(@NonNull final ReaderPost localPost,
Expand Down Expand Up @@ -320,7 +320,7 @@ public void onErrorResponse(VolleyError volleyError) {
};

AppLog.d(T.READER, "requesting post");
restClientUtils.get(path, null, null, listener, errorListener);
restClientUtils.getWithLocale(path, null, null, listener, errorListener);
}

private static String getTrackingPixelForPost(@NonNull ReaderPost post) {
Expand Down Expand Up @@ -417,7 +417,7 @@ public void onErrorResponse(VolleyError volleyError) {
+ "?size_local=" + NUM_RELATED_POSTS_TO_REQUEST
+ "&size_global=" + NUM_RELATED_POSTS_TO_REQUEST
+ "&fields=" + ReaderSimplePost.SIMPLE_POST_FIELDS;
WordPress.getRestClientUtilsV1_2().get(path, null, null, listener, errorListener);
WordPress.getRestClientUtilsV1_2().getWithLocale(path, null, null, listener, errorListener);
}

private static void handleRelatedPostsResponse(final ReaderPost sourcePost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ParseDiscoverCardsJsonUseCase @Inject constructor(
}

fun parseNextPageHandle(jsonObject: JSONObject): String =
jsonObject.getString(ReaderConstants.JSON_NEXT_PAGE_HANDLE)
jsonObject.optString(ReaderConstants.JSON_NEXT_PAGE_HANDLE)

fun convertListOfJsonArraysIntoSingleJsonArray(jsons: List<String>): JSONArray {
val arrays = jsons.map { JSONArray(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void onErrorResponse(VolleyError volleyError) {
}
};
AppLog.d(AppLog.T.READER, "updating comments");
WordPress.getRestClientUtilsV1_1().get(path, null, null, listener, errorListener);
WordPress.getRestClientUtilsV1_1().getWithLocale(path, null, null, listener, errorListener);
}

private static void handleUpdateCommentsResponse(final JSONObject jsonObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import org.wordpress.android.ui.reader.services.discover.ReaderDiscoverLogic.Dis
import org.wordpress.android.ui.reader.services.discover.ReaderDiscoverLogic.DiscoverTasks.REQUEST_MORE
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T.READER
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.util.config.ReaderDiscoverNewEndpointFeatureConfig
import javax.inject.Inject

Expand All @@ -59,6 +60,7 @@ class ReaderDiscoverLogic @Inject constructor(
private val getDiscoverCardsUseCase: GetDiscoverCardsUseCase,
private val appPrefsWrapper: AppPrefsWrapper,
private val readerDiscoverNewEndpointFeatureConfig: ReaderDiscoverNewEndpointFeatureConfig,
private val localeManagerWrapper: LocaleManagerWrapper,
) {
enum class DiscoverTasks {
REQUEST_MORE, REQUEST_FIRST_PAGE
Expand Down Expand Up @@ -118,12 +120,13 @@ class ReaderDiscoverLogic @Inject constructor(
AppLog.e(READER, volleyError)
resultListener.onUpdateResult(FAILED)
}
params["_locale"] = localeManagerWrapper.getLanguage()
val endpoint = if (readerDiscoverNewEndpointFeatureConfig.isEnabled()) {
"read/streams/discover"
} else {
"read/tags/cards"
}
WordPress.getRestClientUtilsV2()[endpoint, params, null, listener, errorListener]
WordPress.getRestClientUtilsV2().get(endpoint, params, null, listener, errorListener)
}
}

Expand All @@ -150,7 +153,9 @@ class ReaderDiscoverLogic @Inject constructor(
insertCardsJsonIntoDb(simplifiedCardsJson)

val nextPageHandle = parseDiscoverCardsJsonUseCase.parseNextPageHandle(json)
appPrefsWrapper.readerCardsPageHandle = nextPageHandle
if (nextPageHandle.isNotEmpty()) {
appPrefsWrapper.readerCardsPageHandle = nextPageHandle
}

if (cards.isEmpty()) {
readerTagTableWrapper.clearTagLastUpdated(ReaderTag.createDiscoverPostCardsTag())
Expand Down Expand Up @@ -238,7 +243,11 @@ class ReaderDiscoverLogic @Inject constructor(
// If we've received a recommended tags or blogs card as the first element,
// it should be displayed as the third card.
if (firstRecommendationCard != null) {
simplifiedJsonList.add(2, firstRecommendationCard)
if (simplifiedJsonList.size >=2) {
simplifiedJsonList.add(2, firstRecommendationCard)
} else {
simplifiedJsonList.add(firstRecommendationCard)
}
}

return JSONArray(simplifiedJsonList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import org.wordpress.android.ui.reader.ReaderEvents;
import org.wordpress.android.ui.reader.services.ServiceCompletionListener;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.LocaleManagerWrapper;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

import static org.wordpress.android.ui.reader.services.post.ReaderPostServiceStarter.ARG_ACTION;
import static org.wordpress.android.ui.reader.services.post.ReaderPostServiceStarter.ARG_BLOG_ID;
Expand All @@ -26,9 +31,12 @@
* EventBus to alert of update status
*/

@AndroidEntryPoint
public class ReaderPostJobService extends JobService implements ServiceCompletionListener {
private ReaderPostLogic mReaderPostLogic;

@Inject LocaleManagerWrapper mLocaleManagerWrapper;

@Override public boolean onStartJob(JobParameters params) {
AppLog.i(AppLog.T.READER, "reader post job service > started");
UpdateAction action;
Expand Down Expand Up @@ -66,7 +74,7 @@ public class ReaderPostJobService extends JobService implements ServiceCompletio
@Override
public void onCreate() {
super.onCreate();
mReaderPostLogic = new ReaderPostLogic(this);
mReaderPostLogic = new ReaderPostLogic(this, mLocaleManagerWrapper);
AppLog.i(AppLog.T.READER, "reader post job service > created");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
import org.wordpress.android.ui.reader.services.post.ReaderPostServiceStarter.UpdateAction;
import org.wordpress.android.ui.reader.utils.ReaderUtils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.LocaleManagerWrapper;
import org.wordpress.android.util.StringUtils;
import org.wordpress.android.util.UrlUtils;

public class ReaderPostLogic {
private ServiceCompletionListener mCompletionListener;
private final LocaleManagerWrapper mLocaleManagerWrapper;
private Object mListenerCompanion;

public ReaderPostLogic(ServiceCompletionListener listener) {
public ReaderPostLogic(@NonNull final ServiceCompletionListener listener,
@NonNull final LocaleManagerWrapper localeManagerWrapper) {
mCompletionListener = listener;
mLocaleManagerWrapper = localeManagerWrapper;
}

public void performTask(Object companion, UpdateAction action,
Expand Down Expand Up @@ -87,7 +91,7 @@ public void onUpdateResult(ReaderActions.UpdateResult result) {
requestPostsForFeed(feedId, action, listener);
}

private static void requestPostsWithTag(final ReaderTag tag,
private void requestPostsWithTag(final ReaderTag tag,
final UpdateAction updateAction,
final ReaderActions.UpdateResultListener resultListener) {
String path = getRelativeEndpointForTag(tag);
Expand Down Expand Up @@ -126,22 +130,18 @@ private static void requestPostsWithTag(final ReaderTag tag,

sb.append("&meta=site,likes");

com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
// remember when this tag was updated if newer posts were requested
if (updateAction == UpdateAction.REQUEST_NEWER || updateAction == UpdateAction.REQUEST_REFRESH) {
ReaderTagTable.setTagLastUpdated(tag);
}
handleUpdatePostsResponse(tag, jsonObject, updateAction, resultListener);
sb.append("&lang=").append(mLocaleManagerWrapper.getLanguage());

com.wordpress.rest.RestRequest.Listener listener = jsonObject -> {
// remember when this tag was updated if newer posts were requested
if (updateAction == UpdateAction.REQUEST_NEWER || updateAction == UpdateAction.REQUEST_REFRESH) {
ReaderTagTable.setTagLastUpdated(tag);
}
handleUpdatePostsResponse(tag, jsonObject, updateAction, resultListener);
};
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(AppLog.T.READER, volleyError);
resultListener.onUpdateResult(ReaderActions.UpdateResult.FAILED);
}
RestRequest.ErrorListener errorListener = volleyError -> {
AppLog.e(AppLog.T.READER, volleyError);
resultListener.onUpdateResult(ReaderActions.UpdateResult.FAILED);
};

WordPress.getRestClientUtilsV1_2().get(sb.toString(), null, null, listener, errorListener);
Expand Down Expand Up @@ -174,7 +174,7 @@ public void onErrorResponse(VolleyError volleyError) {
}
};
AppLog.d(AppLog.T.READER, "updating posts in blog " + blogId);
WordPress.getRestClientUtilsV1_2().get(path, null, null, listener, errorListener);
WordPress.getRestClientUtilsV1_2().getWithLocale(path, null, null, listener, errorListener);
}

private static void requestPostsForFeed(final long feedId,
Expand Down Expand Up @@ -203,7 +203,7 @@ public void onErrorResponse(VolleyError volleyError) {
};

AppLog.d(AppLog.T.READER, "updating posts in feed " + feedId);
WordPress.getRestClientUtilsV1_2().get(path, null, null, listener, errorListener);
WordPress.getRestClientUtilsV1_2().getWithLocale(path, null, null, listener, errorListener);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import org.wordpress.android.ui.reader.ReaderEvents;
import org.wordpress.android.ui.reader.services.ServiceCompletionListener;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.LocaleManagerWrapper;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

import static org.wordpress.android.ui.reader.services.post.ReaderPostServiceStarter.ARG_ACTION;
import static org.wordpress.android.ui.reader.services.post.ReaderPostServiceStarter.ARG_BLOG_ID;
Expand All @@ -21,9 +26,12 @@
* EventBus to alert of update status
*/

@AndroidEntryPoint
public class ReaderPostService extends Service implements ServiceCompletionListener {
private ReaderPostLogic mReaderPostLogic;

@Inject LocaleManagerWrapper mLocaleManagerWrapper;

@Override
public IBinder onBind(Intent intent) {
return null;
Expand All @@ -32,7 +40,7 @@ public IBinder onBind(Intent intent) {
@Override
public void onCreate() {
super.onCreate();
mReaderPostLogic = new ReaderPostLogic(this);
mReaderPostLogic = new ReaderPostLogic(this, mLocaleManagerWrapper);
AppLog.i(AppLog.T.READER, "reader post service > created");
}

Expand Down
Loading

0 comments on commit 0212137

Please sign in to comment.