Skip to content

Commit

Permalink
Merge pull request #20414 from wordpress-mobile/issue/19572-read-stre…
Browse files Browse the repository at this point in the history
…ams-discover-endpoint-integration

[Reader] `read/streams/discover` endpoint integration
  • Loading branch information
RenanLukas authored Mar 7, 2024
2 parents e93b549 + 14115d7 commit 380c65a
Show file tree
Hide file tree
Showing 4 changed files with 3,906 additions and 11 deletions.
1 change: 1 addition & 0 deletions WordPress/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ android {
buildConfigField "boolean", "BLOGANUARY_DASHBOARD_NUDGE", "false"
buildConfigField "boolean", "DYNAMIC_DASHBOARD_CARDS", "false"
buildConfigField "boolean", "STATS_TRAFFIC_TAB", "false"
buildConfigField "boolean", "READER_DISCOVER_NEW_ENDPOINT", "false"

// Override these constants in jetpack product flavor to enable/ disable features
buildConfigField "boolean", "ENABLE_SITE_CREATION", "true"
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.config.ReaderDiscoverNewEndpointFeatureConfig
import javax.inject.Inject

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

Expand Down Expand Up @@ -200,10 +207,19 @@ class ReaderDiscoverLogic @Inject constructor(
@Suppress("NestedBlockDepth")
private fun createSimplifiedJson(cardsJsonArray: JSONArray, discoverTasks: DiscoverTasks): JSONArray {
val simplifiedJsonList = mutableListOf<JSONObject>()
var firstYouMayLikeCard: JSONObject? = null
var firstRecommendationCard: JSONObject? = null
val isFirstPage = discoverTasks == REQUEST_FIRST_PAGE
for (i in 0 until cardsJsonArray.length()) {
val cardJson = cardsJsonArray.getJSONObject(i)
when (cardJson.getString(JSON_CARD_TYPE)) {
// We should not have a recommended blogs or interests/tags card as the first element on Discover feed.
val cardType = cardJson.optString(JSON_CARD_TYPE, "")
val isCardTypeRecommendation =
cardType == JSON_CARD_RECOMMENDED_BLOGS || cardType == JSON_CARD_INTERESTS_YOU_MAY_LIKE
if (i == 0 && isFirstPage && isCardTypeRecommendation) {
firstRecommendationCard = cardJson
continue
}
when (cardType) {
JSON_CARD_RECOMMENDED_BLOGS -> {
cardJson.optJSONArray(JSON_CARD_DATA)?.let { recommendedBlogsCardJson ->
if (recommendedBlogsCardJson.length() > 0) {
Expand All @@ -212,21 +228,17 @@ class ReaderDiscoverLogic @Inject constructor(
}
}
JSON_CARD_INTERESTS_YOU_MAY_LIKE -> {
// We should not have an interests/tags card as the first element on Discover feed.
if (i == 0 && discoverTasks == REQUEST_FIRST_PAGE) {
firstYouMayLikeCard = cardJson
continue
}
simplifiedJsonList.add(cardJson)
}
JSON_CARD_POST -> {
simplifiedJsonList.add(createSimplifiedPostJson(cardJson))
}
}
}
// If we've received an interests/tags card as the first element, it should be displayed as the third card.
if (firstYouMayLikeCard != null) {
simplifiedJsonList.add(2, firstYouMayLikeCard)
// 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)
}

return JSONArray(simplifiedJsonList)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.wordpress.android.util.config

import org.wordpress.android.BuildConfig
import org.wordpress.android.annotation.Feature
import javax.inject.Inject

private const val READER_DISCOVER_NEW_ENDPOINT_REMOTE_FIELD = "reader_discover_new_endpoint"

@Feature(
remoteField = READER_DISCOVER_NEW_ENDPOINT_REMOTE_FIELD,
defaultValue = true,
)
class ReaderDiscoverNewEndpointFeatureConfig @Inject constructor(
appConfig: AppConfig
) : FeatureConfig(
appConfig,
BuildConfig.READER_DISCOVER_NEW_ENDPOINT,
READER_DISCOVER_NEW_ENDPOINT_REMOTE_FIELD
) {
override fun isEnabled(): Boolean {
return super.isEnabled() && BuildConfig.IS_JETPACK_APP
}
}
Loading

0 comments on commit 380c65a

Please sign in to comment.