diff --git a/Sources/SearchFeature/Controllers/SearchLeftController.swift b/Sources/SearchFeature/Controllers/SearchLeftController.swift index d4bb0869e0a8906b7ff77a29381fc4864cfd268f..747705915b1aae3f2f05e1d1a8ebad13b4ff2bd6 100644 --- a/Sources/SearchFeature/Controllers/SearchLeftController.swift +++ b/Sources/SearchFeature/Controllers/SearchLeftController.swift @@ -86,6 +86,13 @@ final class SearchLeftController: UIViewController { .sink { [unowned self] in screenView.updateUIForItem(item: $0) } .store(in: &cancellables) + viewModel.statePublisher + .map(\.country) + .removeDuplicates() + .receive(on: DispatchQueue.main) + .sink { [unowned self] in screenView.countryButton.setFlag($0.flag, prefix: $0.prefix) } + .store(in: &cancellables) + viewModel.statePublisher .compactMap(\.snapshot) .receive(on: DispatchQueue.main) @@ -102,6 +109,16 @@ final class SearchLeftController: UIViewController { .sink { [unowned self] in presentSearchDisclaimer() } .store(in: &cancellables) + screenView.countryButton + .publisher(for: .touchUpInside) + .receive(on: DispatchQueue.main) + .sink { [unowned self] in + coordinator.toCountries(from: self) { [weak self] country in + guard let self = self else { return } + self.viewModel.didPick(country: country) + } + }.store(in: &cancellables) + screenView.inputField .textPublisher .receive(on: DispatchQueue.main) diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift index 94840637e434632621ae947fd7726bba324f4927..94b87690831b52db2e093acd2d441a88eb827995 100644 --- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift +++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift @@ -2,6 +2,7 @@ import HUD import UIKit import Combine import XXModels +import Countries import Integration import DependencyInjection @@ -10,6 +11,7 @@ typealias SearchSnapshot = NSDiffableDataSourceSnapshot<SearchSection, SearchIte struct SearchLeftViewState { var input = "" var snapshot: SearchSnapshot? + var country: Country = .fromMyPhone() var item: SearchSegmentedControl.Item = .username } @@ -36,6 +38,10 @@ final class SearchLeftViewModel { stateSubject.value.input = string } + func didPick(country: Country) { + stateSubject.value.country = country + } + func didSelectItem(_ item: SearchSegmentedControl.Item) { stateSubject.value.item = item } @@ -45,10 +51,15 @@ final class SearchLeftViewModel { hudSubject.send(.on(nil)) + var content = stateSubject.value.input let prefix = stateSubject.value.item.written.first!.uppercased() + if stateSubject.value.item == .phone { + content += stateSubject.value.country.code + } + do { - try session.search(fact: "\(prefix)\(stateSubject.value.input)") { [weak self] in + try session.search(fact: "\(prefix)\(content)") { [weak self] in guard let self = self else { return } switch $0 { diff --git a/Sources/SearchFeature/Views/SearchLeftView.swift b/Sources/SearchFeature/Views/SearchLeftView.swift index 01000b1fe0e658a008d140d89846470ad4f5e58d..bbdda3f926818439e4c28d5beb020239a623048a 100644 --- a/Sources/SearchFeature/Views/SearchLeftView.swift +++ b/Sources/SearchFeature/Views/SearchLeftView.swift @@ -17,7 +17,6 @@ final class SearchLeftView: UIView { tableView.backgroundColor = Asset.neutralWhite.color inputStackView.spacing = 5 - countryButton.isHidden = true inputStackView.addArrangedSubview(countryButton) inputStackView.addArrangedSubview(inputField) diff --git a/Sources/Shared/Views/SearchCountryComponent.swift b/Sources/Shared/Views/SearchCountryComponent.swift index bb5f55968eeccafe6fe2cd3fad5abdd9f691cc1e..186c58b95981ce45a799c93e27c8a53530b16cf4 100644 --- a/Sources/Shared/Views/SearchCountryComponent.swift +++ b/Sources/Shared/Views/SearchCountryComponent.swift @@ -2,7 +2,7 @@ import UIKit public final class SearchCountryComponent: UIControl { let flagLabel = UILabel() - let codeLabel = UILabel() + let prefixLabel = UILabel() let containerView = UIView() public init() { @@ -12,21 +12,28 @@ public final class SearchCountryComponent: UIControl { containerView.backgroundColor = Asset.neutralSecondary.color flagLabel.text = "🇺🇸" - codeLabel.text = "+1" - codeLabel.textColor = Asset.neutralDisabled.color - codeLabel.font = Fonts.Mulish.semiBold.font(size: 14.0) + prefixLabel.text = "+1" + prefixLabel.textColor = Asset.neutralDisabled.color + prefixLabel.font = Fonts.Mulish.semiBold.font(size: 14.0) addSubview(containerView) containerView.addSubview(flagLabel) - containerView.addSubview(codeLabel) + containerView.addSubview(prefixLabel) + + containerView.isUserInteractionEnabled = false setupConstraints() flagLabel.setContentCompressionResistancePriority(.required, for: .horizontal) - codeLabel.setContentCompressionResistancePriority(.required, for: .horizontal) + prefixLabel.setContentCompressionResistancePriority(.required, for: .horizontal) } required init?(coder: NSCoder) { nil } + public func setFlag(_ flag: String, prefix: String) { + flagLabel.text = flag + prefixLabel.text = prefix + } + private func setupConstraints() { containerView.snp.makeConstraints { $0.top.equalToSuperview() @@ -41,7 +48,7 @@ public final class SearchCountryComponent: UIControl { $0.centerY.equalToSuperview() } - codeLabel.snp.makeConstraints { + prefixLabel.snp.makeConstraints { $0.left.equalTo(flagLabel.snp.right).offset(10) $0.right.equalToSuperview().offset(-13) $0.centerY.equalToSuperview()