Created
January 14, 2020 14:59
-
-
Save zafarivaev/394b617a5b44d5825b757e15c87133c9 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 | |
class HolidaysViewModel { | |
private let disposeBag = DisposeBag() | |
// MARK: - Actions | |
let isLoading = BehaviorSubject<Bool>(value: false) | |
let selectedCountry = PublishSubject<String>() | |
let selectedHoliday = PublishSubject<HolidayViewModel>() | |
let chooseCountry = PublishSubject<Void>() | |
// MARK: - Table View Model and Data Source | |
var holidays = BehaviorSubject<[HolidayViewModel]>( | |
value: [] | |
) | |
// MARK: - API Call | |
func fetchHolidays(onError: @escaping (String) -> ()) { | |
self.selectedCountry | |
.subscribe(onNext: { [weak self] (country) in | |
guard let `self` = self else { return } | |
self.isLoading.onNext(true) | |
HolidaysService.shared.getHolidays(country: country, success: { (code, holidays) in | |
self.isLoading.onNext(false) | |
let holidayItems = holidays.holidays!.compactMap { HolidayViewModel(holiday: $0) | |
} | |
self.holidays.onNext(holidayItems) | |
}) { (error) in | |
self.isLoading.onNext(false) | |
onError(error) | |
} | |
}) | |
.disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment