Skip to content

Commit

Permalink
Update region saving logic on sell screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Elizaveta Semenova committed Jan 15, 2024
1 parent b2f4cd7 commit 0a7bda2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Foundation
public protocol SellDataService {
associatedtype Provider: SellDataServiceProvider

/// Current region
var region: ProviderRegion? { get }

/// Availability status
var isAvailable: Bool { get }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public final class MoonpaySellDataService: SellDataService {

public var fiat: MoonpaySellDataServiceProvider.Fiat?

public var region: ProviderRegion?

public let userId: String

// MARK: - Initializer
Expand Down Expand Up @@ -63,6 +65,9 @@ public final class MoonpaySellDataService: SellDataService {
do {
if region == nil {
isAvailable = try await provider.isAvailable()
self.region = try await provider.ipRegion()
} else {
self.region = region
}
let (currency, fiat, _) = try await(
provider.currencies().filter { $0.code.uppercased() == "SOL" }.first,
Expand All @@ -79,6 +84,7 @@ public final class MoonpaySellDataService: SellDataService {
currency = nil
fiat = nil
status = .error(SellDataServiceError.unsupportedRegion(region))
self.region = region
return
} catch {
debugPrint(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class MoonpaySellDataServiceProvider: SellDataServiceProvider {
// MARK: - Properties

private let moonpayAPI: Moonpay.Provider
private var ipRegion: Moonpay.Provider.IpAddressResponse?

// MARK: - Initializer

Expand All @@ -26,7 +27,14 @@ public class MoonpaySellDataServiceProvider: SellDataServiceProvider {
// MARK: - Methods

func isAvailable() async throws -> Bool {
try await moonpayAPI.ipAddresses().isSellAllowed
try await ipRegion().isSellAllowed
}

func ipRegion() async throws -> Moonpay.Provider.IpAddressResponse {
if let ipRegion {
return ipRegion
}
return try await moonpayAPI.ipAddresses()
}

func fiat(region: ProviderRegion?) async throws -> Fiat {
Expand All @@ -43,8 +51,8 @@ public class MoonpaySellDataServiceProvider: SellDataServiceProvider {
if let region {
return try fiatByApha3(region: region)
} else {
let resp = try await moonpayAPI.ipAddresses()
return try fiatByApha3(region: resp)
let region = try await ipRegion()
return try fiatByApha3(region: region)
}
}

Expand Down
1 change: 1 addition & 0 deletions p2p_wallet/Scenes/Main/Sell/SellCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ final class SellCoordinator: Coordinator<SellCoordinatorResult> {
let selectCountryViewModel = SelectCountryViewModel(selectedCountry: selectedCountry)
let selectCountryViewController = SelectCountryView(viewModel: selectCountryViewModel)
.asViewController(withoutUIKitNavBar: false)
viewModel?.isEnteringBaseAmount = false
navigationController.pushViewController(selectCountryViewController, animated: true)

selectCountryViewModel.selectCountry
Expand Down
45 changes: 18 additions & 27 deletions p2p_wallet/Scenes/Main/Sell/SellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class SellViewModel: BaseViewModel, ObservableObject {

func countrySelected(_ country: SelectCountryViewModel.Model, isSellAllowed: Bool) {
setNewCountryInfo(model: country, isSellAllowed: isSellAllowed)
warmUp(region: country)
}

func changeTheRegionClicked() {
Expand Down Expand Up @@ -256,18 +257,11 @@ class SellViewModel: BaseViewModel, ObservableObject {
case .ready:
self.status = .ready
case let .error(SellDataServiceError.unsupportedRegion(region)):
setNewCountryInfo(
model: SelectCountryViewModel.Model(
alpha2: region.alpha2,
country: region.country,
state: region.state,
alpha3: region.alpha3
),
isSellAllowed: false
)
setNewCountryInfo(model: region.createModel(), isSellAllowed: false)
case let .error(error):
self.status = .error(.other)
}
self.region = self.dataService.region?.createModel()
})
.store(in: &subscriptions)

Expand Down Expand Up @@ -370,25 +364,16 @@ class SellViewModel: BaseViewModel, ObservableObject {
// MARK: - Helpers

private func setNewCountryInfo(model: SelectCountryViewModel.Model, isSellAllowed: Bool) {
guard !model.title.isEmpty else {
status = .ready
region = model
return
}
region = model

if isSellAllowed {
warmUp(region: model)
} else {
let model = ChangeCountryErrorView.ChangeCountryModel(
image: UIImage(resource: .connectionErrorCat),
title: L10n.sorry,
subtitle: L10n.unfortunatelyYouCanNotBuyInButYouCanStillUseOtherKeyAppFeatures(model.title),
buttonTitle: L10n.goBack,
subButtonTitle: L10n.changeTheRegionManually
)
status = .error(.region(model))
}
guard !isSellAllowed else { return }
let model = ChangeCountryErrorView.ChangeCountryModel(
image: UIImage(resource: .connectionErrorCat),
title: L10n.sorry,
subtitle: L10n.unfortunatelyYouCanNotCashoutInButYouCanStillUseOtherKeyAppFeatures(model.title),
buttonTitle: L10n.goBack,
subButtonTitle: L10n.changeTheRegionManually
)
status = .error(.region(model))
}

private func checkIfMoreBaseCurrencyNeeded() {
Expand Down Expand Up @@ -499,3 +484,9 @@ extension SellViewModel {
}

extension SelectCountryViewModel.Model: ProviderRegion {}

private extension ProviderRegion {
func createModel() -> SelectCountryViewModel.Model {
SelectCountryViewModel.Model(alpha2: alpha2, country: country, state: state, alpha3: alpha3)
}
}

0 comments on commit 0a7bda2

Please sign in to comment.