Skip to content

Instantly share code, notes, and snippets.

@zafarivaev
Created January 14, 2020 14:59
Show Gist options
  • Save zafarivaev/394b617a5b44d5825b757e15c87133c9 to your computer and use it in GitHub Desktop.
Save zafarivaev/394b617a5b44d5825b757e15c87133c9 to your computer and use it in GitHub Desktop.
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