-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Migrate NewDomainsSearchRepository from FluxC to wordpress-rs
#22891
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
Open
oguzkocer
wants to merge
2
commits into
trunk
Choose a base branch
from
integrate-wordpress-rs-domains
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
113 changes: 67 additions & 46 deletions
113
...ndroid/ui/domains/management/newdomainsearch/domainsfetcher/NewDomainsSearchRepository.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 |
|---|---|---|
| @@ -1,66 +1,87 @@ | ||
| package org.wordpress.android.ui.domains.management.newdomainsearch.domainsfetcher | ||
|
|
||
| import org.wordpress.android.Constants | ||
| import org.wordpress.android.fluxc.generated.SiteActionBuilder | ||
| import org.wordpress.android.fluxc.model.products.Product | ||
| import org.wordpress.android.fluxc.store.ProductsStore | ||
| import org.wordpress.android.fluxc.store.SiteStore | ||
| import org.wordpress.android.fluxc.store.SiteStore.OnSuggestedDomains | ||
| import org.wordpress.android.fluxc.store.AccountStore | ||
| import org.wordpress.android.networking.restapi.WpComApiClientProvider | ||
| import rs.wordpress.api.kotlin.WpComApiClient | ||
| import rs.wordpress.api.kotlin.WpRequestResult | ||
| import uniffi.wp_api.DomainSuggestion | ||
| import uniffi.wp_api.DomainSuggestionsParams | ||
| import uniffi.wp_api.Product | ||
| import uniffi.wp_api.ProductTypeFilter | ||
| import uniffi.wp_api.ProductsParams | ||
| import javax.inject.Inject | ||
|
|
||
| private const val SUGGESTIONS_REQUEST_COUNT = 20 | ||
| private const val SUGGESTIONS_REQUEST_COUNT = 20u | ||
|
|
||
| class NewDomainsSearchRepository @Inject constructor( | ||
| private val productsStore: ProductsStore, | ||
| private val suggestedDomainsFetcher: SuggestedDomainsFetcher | ||
| private val wpComApiClientProvider: WpComApiClientProvider, | ||
| private val accountStore: AccountStore, | ||
| ) { | ||
| var products: List<Product>? = null | ||
| private var wpComApiClient: WpComApiClient? = null | ||
| private var products: List<Product>? = null | ||
|
|
||
| @Synchronized | ||
| private fun getOrCreateClient(): WpComApiClient { | ||
| val token = requireNotNull(accountStore.accessToken) { | ||
| "WP.com access token is required" | ||
| } | ||
| return wpComApiClient | ||
| ?: wpComApiClientProvider.getWpComApiClient(token) | ||
| .also { wpComApiClient = it } | ||
| } | ||
|
|
||
| suspend fun searchForDomains(query: String): DomainsResult { | ||
| if (products == null) fetchProducts() | ||
| return SiteActionBuilder.newSuggestDomainsAction( | ||
| SiteStore.SuggestDomainsPayload( | ||
| query = query, | ||
| onlyWordpressCom = false, | ||
| includeWordpressCom = false, | ||
| includeDotBlogSubdomain = false, | ||
| quantity = SUGGESTIONS_REQUEST_COUNT | ||
| ) | ||
| ).let { action -> | ||
| suggestedDomainsFetcher.fetch(action) | ||
| }.let { event -> | ||
| onDomainSuggestionsFetched(query, event) | ||
|
|
||
| val params = DomainSuggestionsParams( | ||
| query = query, | ||
| quantity = SUGGESTIONS_REQUEST_COUNT, | ||
| onlyWordpressdotcom = false, // checkstyle ignore | ||
| includeWordpressdotcom = false, // checkstyle ignore | ||
| includeDotblogsubdomain = false, | ||
| ) | ||
|
|
||
| return when ( | ||
| val result = getOrCreateClient() | ||
| .request { it.domains().suggestions(params).data } | ||
| ) { | ||
| is WpRequestResult.Success -> { | ||
| val suggestions = result.response | ||
| .filterIsInstance<DomainSuggestion.Paid>() | ||
| .sortedByDescending { it.v1.relevance } | ||
| .map { paid -> | ||
| val product = products?.firstOrNull { | ||
| it.productId == paid.v1.productId | ||
| } | ||
| ProposedDomain( | ||
| productId = paid.v1.productId.toInt(), | ||
| domain = paid.v1.domainName, | ||
| price = paid.v1.cost, | ||
| salePrice = product?.combinedSaleCostDisplay, | ||
| supportsPrivacy = paid.v1.supportsPrivacy, | ||
| ) | ||
| } | ||
| DomainsResult.Success(suggestions) | ||
| } | ||
| else -> DomainsResult.Error | ||
| } | ||
| } | ||
|
|
||
| private suspend fun fetchProducts() { | ||
| val result = productsStore.fetchProducts(Constants.TYPE_DOMAINS_PRODUCT) | ||
| if (!result.isError) result.products?.let { products = it } | ||
| } | ||
|
|
||
| private fun onDomainSuggestionsFetched(query: String, event: OnSuggestedDomains): DomainsResult { | ||
| return if (query == event.query && !event.isError) { | ||
| val suggestions = event.suggestions | ||
| .filter { !it.is_free } | ||
| .sortedByDescending { it.relevance } | ||
| .map { domain -> | ||
| val product = products?.firstOrNull { product -> product.productId == domain.product_id } | ||
| ProposedDomain( | ||
| productId = domain.product_id, | ||
| domain = domain.domain_name, | ||
| price = domain.cost, | ||
| salePrice = product?.combinedSaleCostDisplay, | ||
| supportsPrivacy = domain.supports_privacy | ||
| ) | ||
| } | ||
| DomainsResult.Success(suggestions) | ||
| } else { | ||
| DomainsResult.Error | ||
| val params = ProductsParams( | ||
| productType = ProductTypeFilter.Domains | ||
| ) | ||
| val result = getOrCreateClient() | ||
| .request { it.products().list(params).data } | ||
| if (result is WpRequestResult.Success) { | ||
| products = result.response.values.toList() | ||
| } | ||
| } | ||
|
|
||
| sealed interface DomainsResult { | ||
| data class Success(val proposedDomains: List<ProposedDomain>) : DomainsResult | ||
| object Error : DomainsResult | ||
| data class Success( | ||
| val proposedDomains: List<ProposedDomain> | ||
| ) : DomainsResult | ||
| data object Error : DomainsResult | ||
| } | ||
| } | ||
14 changes: 0 additions & 14 deletions
14
...s/android/ui/domains/management/newdomainsearch/domainsfetcher/SuggestedDomainsFetcher.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkmassel Would you prefer if we changed these parameter field names to comply with our checkstyle rules, or keep them as is — matching what the API returns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes a lot more sense to match the API