Skip to content

Commit

Permalink
Merge pull request #2907 from wordpress-mobile/wpcom-them-fetch-impro…
Browse files Browse the repository at this point in the history
…vements

Improvements to the WPCom themes fetching endpoint
  • Loading branch information
hichamboushaba authored Nov 27, 2023
2 parents d0f1653 + 89d2c38 commit 8076d17
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wordpress.android.fluxc.store.SiteStore.OnSiteChanged;
import org.wordpress.android.fluxc.store.SiteStore.OnSiteRemoved;
import org.wordpress.android.fluxc.store.ThemeStore;
import org.wordpress.android.fluxc.store.ThemeStore.FetchWPComThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.OnCurrentThemeFetched;
import org.wordpress.android.fluxc.store.ThemeStore.OnSiteThemesChanged;
import org.wordpress.android.fluxc.store.ThemeStore.OnThemeActivated;
Expand Down Expand Up @@ -385,7 +386,7 @@ private void signOutWPCom() throws InterruptedException {
private void fetchWpComThemes() throws InterruptedException {
mCountDownLatch = new CountDownLatch(1);
mNextEvent = TestEvents.FETCHED_WPCOM_THEMES;
mDispatcher.dispatch(ThemeActionBuilder.newFetchWpComThemesAction());
mDispatcher.dispatch(ThemeActionBuilder.newFetchWpComThemesAction(new FetchWPComThemesPayload()));
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.wordpress.android.fluxc.store.AccountStore.OnAuthenticationChanged;
import org.wordpress.android.fluxc.store.SiteStore.OnSiteChanged;
import org.wordpress.android.fluxc.store.ThemeStore;
import org.wordpress.android.fluxc.store.ThemeStore.FetchWPComThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.OnCurrentThemeFetched;
import org.wordpress.android.fluxc.store.ThemeStore.OnSiteThemesChanged;
import org.wordpress.android.fluxc.store.ThemeStore.OnStarterDesignsFetched;
Expand Down Expand Up @@ -225,7 +226,7 @@ private void fetchWpComThemes() throws InterruptedException {
// no need to sign into account, this is a test for an endpoint that does not require authentication
mCountDownLatch = new CountDownLatch(1);
mNextEvent = TestEvents.FETCHED_WPCOM_THEMES;
mDispatcher.dispatch(ThemeActionBuilder.newFetchWpComThemesAction());
mDispatcher.dispatch(ThemeActionBuilder.newFetchWpComThemesAction(new FetchWPComThemesPayload()));
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_themes.*
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.example.ui.common.showSiteSelectorDialog
import org.wordpress.android.fluxc.example.utils.showSingleLineDialog
import org.wordpress.android.fluxc.generated.ThemeActionBuilder
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.ThemeModel
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.fluxc.store.ThemeStore
import org.wordpress.android.fluxc.store.ThemeStore.FetchWPComThemesPayload
import org.wordpress.android.fluxc.store.ThemeStore.OnCurrentThemeFetched
import org.wordpress.android.fluxc.store.ThemeStore.OnSiteThemesChanged
import org.wordpress.android.fluxc.store.ThemeStore.OnThemeActivated
Expand Down Expand Up @@ -50,8 +54,12 @@ class ThemeFragment : Fragment() {
dispatcher.unregister(this)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_themes, container, false)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.fragment_themes, container, false)

@Suppress("LongMethod")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -144,22 +152,48 @@ class ThemeFragment : Fragment() {
}

fetch_wpcom_themes.setOnClickListener {
dispatcher.dispatch(ThemeActionBuilder.newFetchWpComThemesAction())
lifecycleScope.launch {
val filter = showSingleLineDialog(
activity = requireActivity(),
message = "Enter filter (Optional)",
isNumeric = false
)
val limit = showSingleLineDialog(
activity = requireActivity(),
message = "Limit results? (Optional, defaults to 500)",
isNumeric = true
)
dispatcher.dispatch(
ThemeActionBuilder.newFetchWpComThemesAction(
limit?.let {
FetchWPComThemesPayload(filter, it.toInt())
} ?: FetchWPComThemesPayload(filter)
)
)
}
}

fetch_installed_themes.setOnClickListener {
if (getJetpackConnectedSite() == null) {
prependToLog("No Jetpack connected site found, unable to test.")
} else {
dispatcher.dispatch(ThemeActionBuilder.newFetchInstalledThemesAction(getJetpackConnectedSite()))
dispatcher.dispatch(
ThemeActionBuilder.newFetchInstalledThemesAction(
getJetpackConnectedSite()
)
)
}
}

fetch_current_theme_jp.setOnClickListener {
if (getJetpackConnectedSite() == null) {
prependToLog("No Jetpack connected site found, unable to test.")
} else {
dispatcher.dispatch(ThemeActionBuilder.newFetchCurrentThemeAction(getJetpackConnectedSite()))
dispatcher.dispatch(
ThemeActionBuilder.newFetchCurrentThemeAction(
getJetpackConnectedSite()
)
)
}
}

Expand Down Expand Up @@ -240,8 +274,11 @@ class ThemeFragment : Fragment() {
} else {
prependToLog("success: WP.com themes fetched count = " + themeStore.wpComThemes.size)

listOf(ThemeStore.MOBILE_FRIENDLY_CATEGORY_BLOG, ThemeStore.MOBILE_FRIENDLY_CATEGORY_WEBSITE,
ThemeStore.MOBILE_FRIENDLY_CATEGORY_PORTFOLIO).forEach { category ->
listOf(
ThemeStore.MOBILE_FRIENDLY_CATEGORY_BLOG,
ThemeStore.MOBILE_FRIENDLY_CATEGORY_WEBSITE,
ThemeStore.MOBILE_FRIENDLY_CATEGORY_PORTFOLIO
).forEach { category ->
val mobileFriendlyThemes = themeStore.getWpComMobileFriendlyThemes(category)
prependToLog(category + " theme count = " + mobileFriendlyThemes.size)
mobileFriendlyThemes.forEach { theme ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.wordpress.android.fluxc.annotations.action.IAction;
import org.wordpress.android.fluxc.model.SiteModel;
import org.wordpress.android.fluxc.store.ThemeStore.FetchStarterDesignsPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchWPComThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedCurrentThemePayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedSiteThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedStarterDesignsPayload;
Expand All @@ -14,7 +15,7 @@
@ActionEnum
public enum ThemeAction implements IAction {
// Remote actions
@Action
@Action(payloadType = FetchWPComThemesPayload.class)
FETCH_WP_COM_THEMES,
@Action(payloadType = FetchStarterDesignsPayload.class)
FETCH_STARTER_DESIGNS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@

@Singleton
public class ThemeRestClient extends BaseWPComRestClient {
/**
* Used by {@link #fetchWpComThemes()} request all themes in a single fetch.
*/
private static final String WP_THEME_FETCH_NUMBER_PARAM = "number=500";
private static final String WPCOM_MOBILE_FRIENDLY_TAXONOMY_SLUG = "mobile-friendly";
private static final String THEME_TYPE_EXTERNAL = "managed-external";

Expand Down Expand Up @@ -82,7 +78,10 @@ public void deleteTheme(@NonNull final SiteModel site, @NonNull final ThemeModel
* [Undocumented!] Endpoint: v1.1/sites/$siteId/themes/$themeId/install
*/
public void installTheme(@NonNull final SiteModel site, @NonNull final ThemeModel theme) {
String themeId = getThemeIdWithWpComSuffix(theme);
String themeId = theme.getThemeId();
if (!site.isWPComAtomic()) {
themeId = getThemeIdWithWpComSuffix(theme);
}
String url = WPCOMREST.sites.site(site.getSiteId()).themes.theme(themeId).install.getUrlV1_1();
add(WPComGsonRequest.buildPostRequest(url, null, JetpackThemeResponse.class,
response -> {
Expand Down Expand Up @@ -127,9 +126,14 @@ public void activateTheme(@NonNull final SiteModel site, @NonNull final ThemeMod
*
* @see <a href="https://developer.wordpress.com/docs/api/1.1/get/themes/">Previous version</a>
*/
public void fetchWpComThemes() {
String url = WPCOMREST.themes.getUrlV1_2() + "?" + WP_THEME_FETCH_NUMBER_PARAM;
add(WPComGsonRequest.buildGetRequest(url, null, WPComThemeListResponse.class,
public void fetchWpComThemes(@Nullable String filter, int resultsLimit) {
String url = WPCOMREST.themes.getUrlV1_2();
Map<String, String> params = new HashMap<>();
params.put("number", String.valueOf(resultsLimit));
if (filter != null) {
params.put("filter", filter);
}
add(WPComGsonRequest.buildGetRequest(url, params, WPComThemeListResponse.class,
response -> {
AppLog.d(AppLog.T.API, "Received response to WP.com themes fetch request.");
List<ThemeModel> themes = createThemeListFromArrayResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public static List<ThemeModel> getWpComThemes() {
.endWhere().getAsModel();
}

public static List<ThemeModel> getWpComThemes(@NonNull List<String> themeIds) {
return WellSql.select(ThemeModel.class)
.where()
.equals(ThemeModelTable.IS_WP_COM_THEME, true)
.isIn(ThemeModelTable.THEME_ID, themeIds)
.endWhere().getAsModel();
}

public static List<ThemeModel> getWpComMobileFriendlyThemes(String categorySlug) {
return WellSql.select(ThemeModel.class)
.where()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,26 @@ public class ThemeStore extends Store {
public static final String MOBILE_FRIENDLY_CATEGORY_WEBSITE = "starting-website";
public static final String MOBILE_FRIENDLY_CATEGORY_PORTFOLIO = "starting-portfolio";

// A high number to ensure we get all themes in one request
private static final int DEFAULT_LIMIT_OF_THEME_RESULTS = 500;

// Payloads
public static class FetchWPComThemesPayload extends Payload<BaseNetworkError> {
@Nullable public String filter;
public int resultsLimit = DEFAULT_LIMIT_OF_THEME_RESULTS;

public FetchWPComThemesPayload() {}

public FetchWPComThemesPayload(@Nullable String filter) {
this.filter = filter;
}

public FetchWPComThemesPayload(@Nullable String filter, int resultsLimit) {
this.filter = filter;
this.resultsLimit = resultsLimit;
}
}

public static class FetchedCurrentThemePayload extends Payload<ThemesError> {
@NonNull public SiteModel site;
@Nullable public ThemeModel theme;
Expand Down Expand Up @@ -260,7 +279,7 @@ public void onAction(Action action) {
}
switch ((ThemeAction) actionType) {
case FETCH_WP_COM_THEMES:
fetchWpComThemes();
fetchWpComThemes((FetchWPComThemesPayload) action.getPayload());
break;
case FETCHED_WP_COM_THEMES:
handleWpComThemesFetched((FetchedWpComThemesPayload) action.getPayload());
Expand Down Expand Up @@ -316,6 +335,10 @@ public List<ThemeModel> getWpComThemes() {
return ThemeSqlUtils.getWpComThemes();
}

public List<ThemeModel> getWpComThemes(@NonNull List<String> themeIds) {
return ThemeSqlUtils.getWpComThemes(themeIds);
}

public List<ThemeModel> getWpComMobileFriendlyThemes(String categorySlug) {
return ThemeSqlUtils.getWpComMobileFriendlyThemes(categorySlug);
}
Expand Down Expand Up @@ -348,8 +371,8 @@ public void setActiveThemeForSite(@NonNull SiteModel site, @NonNull ThemeModel t
ThemeSqlUtils.insertOrReplaceActiveThemeForSite(site, theme);
}

private void fetchWpComThemes() {
mThemeRestClient.fetchWpComThemes();
private void fetchWpComThemes(@NonNull FetchWPComThemesPayload payload) {
mThemeRestClient.fetchWpComThemes(payload.filter, payload.resultsLimit);
}

private void fetchStarterDesigns(@NonNull FetchStarterDesignsPayload payload) {
Expand Down

0 comments on commit 8076d17

Please sign in to comment.