-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Site Design Revamp] Recommends designs based on chosen vertical #16510
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
Merged
ovitrif
merged 21 commits into
feature/site-design-revamp
from
task/16503-recommend-vertical-designs
May 13, 2022
Merged
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f502a2a
Updates FluxC reference
antonis a6f506f
Pass selected vertical to the design picker
antonis 85d9074
Recommend designs based on the selected vertical
antonis 7142471
Merge branch 'feature/site-design-revamp' into task/16503-recommend-v…
antonis 9677bb9
Extracts the recommendation logic to a separate file
antonis fe387f4
Tests the recommended designs logic
antonis 0052838
Adds a mechanism for randomising the order of layouts in specific cat…
antonis 29060b9
Randomises the order of site designs except for the recommended category
antonis 69d9798
Also save the ordered layouts per category for efficiency
antonis 9daa723
Unit tests for which categories should be randomised
antonis c349fca
Merge branch 'feature/site-design-revamp' into task/16503-recommend-v…
antonis 11ff1a1
Merge branch 'task/16503-recommend-vertical-designs' into task/16506-…
antonis e0980a7
Unselect the layout after the choose action if fired
antonis 2fa545a
Merge branch 'task/16503-recommend-vertical-designs' into task/16506-…
antonis 3ddba5e
Adds recommended property in the enhanced_site_creation_site_design_s…
antonis e80ad38
Merge branch 'feature/site-design-revamp' into task/16503-recommend-v…
antonis 319e4c5
Merge branch 'task/16503-recommend-vertical-designs' into task/16506-…
antonis 62812e7
Merge branch 'task/16503-recommend-vertical-designs' into task/16528-…
antonis 91c1cab
Hides keyboard on back
antonis 1020c4d
Merge pull request #16531 from wordpress-mobile/task/16506-randomise-…
ovitrif 8a16b16
Merge pull request #16538 from wordpress-mobile/task/16528-track-reco…
ovitrif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...main/java/org/wordpress/android/ui/sitecreation/theme/SiteDesignRecommendationProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package org.wordpress.android.ui.sitecreation.theme | ||
|
|
||
| import org.wordpress.android.R | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.theme.StarterDesign | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.theme.StarterDesignCategory | ||
| import org.wordpress.android.ui.layoutpicker.LayoutCategoryModel | ||
| import org.wordpress.android.ui.layoutpicker.LayoutModel | ||
| import org.wordpress.android.ui.layoutpicker.toLayoutCategories | ||
| import org.wordpress.android.ui.layoutpicker.toLayoutModels | ||
| import org.wordpress.android.viewmodel.ResourceProvider | ||
| import javax.inject.Inject | ||
|
|
||
| class SiteDesignRecommendationProvider @Inject constructor(private val resourceProvider: ResourceProvider) { | ||
| fun handleResponse( | ||
| vertical: String, | ||
| designs: List<StarterDesign>, | ||
| categories: List<StarterDesignCategory>, | ||
| responseHandler: (layouts: List<LayoutModel>, categories: List<LayoutCategoryModel>) -> Unit | ||
| ) { | ||
| val verticalSlug: String? = if (vertical.isNullOrEmpty()) null else getVerticalSlug(vertical) | ||
| val hasRecommendations = !verticalSlug.isNullOrEmpty() && | ||
| designs.any { it.group != null && it.group.contains(verticalSlug) } | ||
|
|
||
| if (hasRecommendations) { | ||
| val recommendedTitle = resourceProvider.getString(R.string.hpp_recommended_title, vertical) | ||
| // Create a new category for the recommendations | ||
| val recommendedCategory = StarterDesignCategory( | ||
| slug = "recommended_$verticalSlug", // The slug is not used but should not already exist | ||
| title = recommendedTitle, | ||
| description = recommendedTitle, | ||
| emoji = "" | ||
| ) | ||
| val designsWithRecommendations = designs.map { | ||
| // Add the new category to the recommended designs so that they are filtered correctly | ||
| // in the `LayoutPickerViewModel.loadLayouts()` method | ||
| if (it.group.contains(verticalSlug)) { | ||
| it.copy(categories = it.categories + recommendedCategory) | ||
| } else { | ||
| it | ||
| } | ||
| }.toLayoutModels() | ||
| val categoriesWithRecommendations = | ||
| listOf(recommendedCategory).toLayoutCategories(true) + | ||
| categories.toLayoutCategories() | ||
| responseHandler(designsWithRecommendations, categoriesWithRecommendations) | ||
| } else { | ||
| // If no designs are recommended for the selected vertical recommend the blog category | ||
| val recommendedTitle = resourceProvider.getString( | ||
| R.string.hpp_recommended_title, | ||
| resourceProvider.getString(R.string.hpp_recommended_default_vertical) | ||
| ) | ||
| val recommendedCategory = categories.firstOrNull { it.slug == "blog" }?.copy( | ||
| title = recommendedTitle, | ||
| description = recommendedTitle | ||
| ) | ||
| if (recommendedCategory == null) { | ||
| // If there is no blog category do not show a recommendation | ||
| responseHandler(designs.toLayoutModels(), categories.toLayoutCategories()) | ||
| } else { | ||
| val categoriesWithRecommendations = | ||
| listOf(recommendedCategory).toLayoutCategories(true) + | ||
| categories.toLayoutCategories() | ||
| responseHandler(designs.toLayoutModels(), categoriesWithRecommendations) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun getVerticalSlug(vertical: String): String? { | ||
| val slugsArray = resourceProvider.getStringArray(R.array.site_creation_intents_slugs) | ||
| val verticalArray = resourceProvider.getStringArray(R.array.site_creation_intents_strings) | ||
| if (slugsArray.size != verticalArray.size) { | ||
| throw IllegalStateException("Intents arrays size mismatch") | ||
| } | ||
| return slugsArray.getOrNull(verticalArray.indexOf(vertical)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
.../java/org/wordpress/android/ui/sitecreation/theme/SiteDesignRecommendationProviderTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package org.wordpress.android.ui.sitecreation.theme | ||
|
|
||
| import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
| import com.nhaarman.mockitokotlin2.any | ||
| import com.nhaarman.mockitokotlin2.whenever | ||
| import kotlinx.coroutines.InternalCoroutinesApi | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.Mock | ||
| import org.mockito.junit.MockitoJUnitRunner | ||
| import org.wordpress.android.R | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.theme.StarterDesign | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.theme.StarterDesignCategory | ||
| import org.wordpress.android.ui.layoutpicker.LayoutCategoryModel | ||
| import org.wordpress.android.ui.layoutpicker.LayoutModel | ||
| import org.wordpress.android.viewmodel.ResourceProvider | ||
|
|
||
| @RunWith(MockitoJUnitRunner::class) | ||
| @InternalCoroutinesApi | ||
| class SiteDesignRecommendationProviderTest { | ||
| @Rule | ||
| @JvmField val rule = InstantTaskExecutorRule() | ||
|
|
||
| @Mock lateinit var resourceProvider: ResourceProvider | ||
|
|
||
| private lateinit var recommendationProvider: SiteDesignRecommendationProvider | ||
|
|
||
| private val mockedVerticalSlug = "mockedVerticalSlug" | ||
| private val mockedVerticalTitle = "mockedVerticalTitle" | ||
| private val recommendedDesignSlug = "recommendedDesignSlug" | ||
| private val blogCategory = StarterDesignCategory( | ||
| slug = "blog", | ||
| title = "Blog", | ||
| description = "Blogging designs", | ||
| emoji = "" | ||
| ) | ||
| private val anotherCategory = StarterDesignCategory( | ||
| slug = "another", | ||
| title = "Another", | ||
| description = "Random designs", | ||
| emoji = "" | ||
| ) | ||
| private val blogDesign = StarterDesign( | ||
| "blog", | ||
| "title", | ||
| 1L, | ||
| listOf(blogCategory), | ||
| "url", | ||
| "theme", | ||
| listOf("any"), | ||
| "desktopThumbnail", | ||
| "tabletThumbnail", | ||
| "mobileThumbnail" | ||
| ) | ||
| private val anotherDesign = StarterDesign( | ||
| recommendedDesignSlug, | ||
| "title", | ||
| 1L, | ||
| listOf(anotherCategory), | ||
| "url", | ||
| "theme", | ||
| listOf("any", mockedVerticalSlug), | ||
| "desktopThumbnail", | ||
| "tabletThumbnail", | ||
| "mobileThumbnail" | ||
| ) | ||
| private val allDesigns = listOf(blogDesign, anotherDesign) | ||
| private val allCategories = listOf(blogCategory, anotherCategory) | ||
|
|
||
| private val slugsArray = arrayOf("art", "automotive", "beauty", mockedVerticalSlug) | ||
| private val verticalArray = arrayOf("Art", "Automotive", "Beauty", mockedVerticalTitle) | ||
|
|
||
| private class ResponseHandler { | ||
| var layouts: List<LayoutModel>? = null | ||
| var categories: List<LayoutCategoryModel>? = null | ||
| fun handle(layouts: List<LayoutModel>, categories: List<LayoutCategoryModel>) { | ||
| this.layouts = layouts | ||
| this.categories = categories | ||
| } | ||
| } | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| recommendationProvider = SiteDesignRecommendationProvider(resourceProvider) | ||
| whenever(resourceProvider.getString(any())).thenReturn("Blogging") | ||
| whenever(resourceProvider.getString(any(), any())).thenReturn("Best for Blogging") | ||
| whenever(resourceProvider.getStringArray(R.array.site_creation_intents_slugs)).thenReturn(slugsArray) | ||
| whenever(resourceProvider.getStringArray(R.array.site_creation_intents_strings)).thenReturn(verticalArray) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when no vertical is selected the blog category is recommended`() { | ||
| val handler = ResponseHandler() | ||
| recommendationProvider.handleResponse("", allDesigns, allCategories, handler::handle) | ||
| assertThat(requireNotNull(handler.categories?.filter { it.isRecommended }?.size)).isEqualTo(1) | ||
| assertThat(requireNotNull(handler.categories?.first()?.isRecommended)).isEqualTo(true) | ||
| assertThat(requireNotNull(handler.categories?.first()?.slug)).isEqualTo(blogCategory.slug) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when a vertical is selected and there are no recommendations, the blog category is recommended`() { | ||
| val handler = ResponseHandler() | ||
| recommendationProvider.handleResponse("art", allDesigns, allCategories, handler::handle) | ||
| assertThat(requireNotNull(handler.categories?.filter { it.isRecommended }?.size)).isEqualTo(1) | ||
| assertThat(requireNotNull(handler.categories?.first()?.isRecommended)).isEqualTo(true) | ||
| assertThat(requireNotNull(handler.categories?.first()?.slug)).isEqualTo(blogCategory.slug) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when a vertical is selected and there are recommendations, a recommended category is created`() { | ||
| val handler = ResponseHandler() | ||
| recommendationProvider.handleResponse(mockedVerticalTitle, allDesigns, allCategories, handler::handle) | ||
| assertThat(requireNotNull(handler.categories?.filter { it.isRecommended }?.size)).isEqualTo(1) | ||
| assertThat(requireNotNull(handler.categories?.first()?.isRecommended)).isEqualTo(true) | ||
| assertThat(requireNotNull(handler.categories?.first()?.slug)).isEqualTo("recommended_$mockedVerticalSlug") | ||
| } | ||
|
|
||
| @Test | ||
| fun `when a vertical is selected the associated design is recommended`() { | ||
| val handler = ResponseHandler() | ||
| recommendationProvider.handleResponse(mockedVerticalTitle, allDesigns, allCategories, handler::handle) | ||
| assertThat(requireNotNull(handler.layouts?.filter { it.slug == recommendedDesignSlug }?.size)).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when no vertical is selected and there is no blog category skip the recommendation`() { | ||
| val handler = ResponseHandler() | ||
| val noBlogDesigns = listOf(anotherDesign) | ||
| val noBlogCategories = listOf(anotherCategory) | ||
| recommendationProvider.handleResponse("", noBlogDesigns, noBlogCategories, handler::handle) | ||
| assertThat(requireNotNull(handler.categories?.filter { it.isRecommended }?.size)).isEqualTo(0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when a vertical is selected and there are no recommendations or blog category skip the recommendation`() { | ||
| val handler = ResponseHandler() | ||
| val noBlogDesigns = listOf(anotherDesign) | ||
| val noBlogCategories = listOf(anotherCategory) | ||
| recommendationProvider.handleResponse("art", noBlogDesigns, noBlogCategories, handler::handle) | ||
| assertThat(requireNotNull(handler.categories?.filter { it.isRecommended }?.size)).isEqualTo(0) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.