Created
January 14, 2020 15:03
-
-
Save zafarivaev/b0e6a19c517ec4164b98f6f7e7e72262 to your computer and use it in GitHub Desktop.
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
import RxSwift | |
import RxCocoa | |
final class ChooseCountryViewModel { | |
private let disposeBag = DisposeBag() | |
// MARK: - Actions | |
let didClose = PublishSubject<Void>() | |
let selectedCountry = PublishSubject<String>() | |
let isLoading = BehaviorSubject<Bool>(value: false) | |
let searchText = PublishSubject<String>() | |
// MARK: - Table View Model & Data Source | |
let fetchedCountries = BehaviorSubject<[CountryViewModel]>(value: []) | |
let filteredCountries = BehaviorSubject<[CountryViewModel]>(value: []) | |
// MARK: - API Call | |
func fetchCountries(onError: @escaping (String) -> ()) { | |
self.isLoading.onNext(true) | |
CountriesService.shared.getCountries(success: { [weak self] (code, countries) in | |
guard let `self` = self else { return } | |
self.isLoading.onNext(false) | |
let countryItems = countries.countries!.compactMap { | |
CountryViewModel(country: $0) | |
} | |
self.fetchedCountries.onNext(countryItems) | |
self.filteredCountries.onNext(countryItems) | |
self.bindSearchToModel() | |
}) { [weak self] (error) in | |
guard let `self` = self else { return } | |
self.isLoading.onNext(false) | |
onError(error) | |
} | |
} | |
func bindSearchToModel() { | |
self.searchText.subscribe(onNext: { [weak self] (text) in | |
guard let `self` = self else { return } | |
switch text.isEmpty { | |
case false: | |
let countries = try! self.fetchedCountries.value().filter { | |
$0.name.range(of: text, options: .caseInsensitive) != nil | |
} | |
self.filteredCountries.onNext(countries) | |
case true: | |
self.filteredCountries.onNext(try! self.fetchedCountries.value()) | |
} | |
}) | |
.disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment