|
| 1 | +import Foundation |
| 2 | +import Yosemite |
| 3 | + |
| 4 | +/// Provides domain suggestions data of a generic type. |
| 5 | +/// The generic type allows different domain suggestion schemas, like free and paid domains. |
| 6 | +protocol DomainSelectorDataProvider { |
| 7 | + associatedtype DomainSuggestion |
| 8 | + |
| 9 | + /// Loads domain suggestions async from the remote. |
| 10 | + /// - Parameter query: Search query for the domain suggestions. |
| 11 | + /// - Returns: A list of domain suggestions. |
| 12 | + func loadDomainSuggestions(query: String) async throws -> [DomainSuggestion] |
| 13 | +} |
| 14 | + |
| 15 | +/// View model for free domain suggestion UI that shows the domain name. |
| 16 | +struct FreeDomainSuggestionViewModel: DomainSuggestionViewProperties, Equatable { |
| 17 | + let name: String |
| 18 | + let attributedDetail: AttributedString? = nil |
| 19 | + |
| 20 | + init(domainSuggestion: FreeDomainSuggestion) { |
| 21 | + self.name = domainSuggestion.name |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Provides domain suggestions that are free. |
| 26 | +final class FreeDomainSelectorDataProvider: DomainSelectorDataProvider { |
| 27 | + private let stores: StoresManager |
| 28 | + |
| 29 | + init(stores: StoresManager = ServiceLocator.stores) { |
| 30 | + self.stores = stores |
| 31 | + } |
| 32 | + |
| 33 | + @MainActor |
| 34 | + func loadDomainSuggestions(query: String) async throws -> [FreeDomainSuggestionViewModel] { |
| 35 | + try await withCheckedThrowingContinuation { continuation in |
| 36 | + stores.dispatch(DomainAction.loadFreeDomainSuggestions(query: query) { result in |
| 37 | + continuation.resume(with: result.map { $0 |
| 38 | + .filter { $0.isFree } |
| 39 | + .map { FreeDomainSuggestionViewModel(domainSuggestion: $0) } |
| 40 | + }) |
| 41 | + }) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// View model for paid domain suggestion UI that shows the domain name and attributed price info. |
| 47 | +/// The product ID is for creating a cart after a domain is selected. |
| 48 | +struct PaidDomainSuggestionViewModel: DomainSuggestionViewProperties, Equatable { |
| 49 | + let name: String |
| 50 | + let attributedDetail: AttributedString? |
| 51 | + let productID: Int64 |
| 52 | + |
| 53 | + init(domainSuggestion: PaidDomainSuggestion) { |
| 54 | + self.name = domainSuggestion.name |
| 55 | + // TODO: 8558 - attributed price info |
| 56 | + self.attributedDetail = .init("\(domainSuggestion.saleCost ?? "no sale") / \(domainSuggestion.cost) / \(domainSuggestion.term)") |
| 57 | + self.productID = domainSuggestion.productID |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Provides domain suggestions that are paid. |
| 62 | +final class PaidDomainSelectorDataProvider: DomainSelectorDataProvider { |
| 63 | + private let stores: StoresManager |
| 64 | + |
| 65 | + init(stores: StoresManager = ServiceLocator.stores) { |
| 66 | + self.stores = stores |
| 67 | + } |
| 68 | + |
| 69 | + @MainActor |
| 70 | + func loadDomainSuggestions(query: String) async throws -> [PaidDomainSuggestionViewModel] { |
| 71 | + try await withCheckedThrowingContinuation { continuation in |
| 72 | + stores.dispatch(DomainAction.loadPaidDomainSuggestions(query: query) { result in |
| 73 | + continuation.resume(with: result.map { $0.map { PaidDomainSuggestionViewModel(domainSuggestion: $0) } }) |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments