RxSwift와 ViewModel을 활용한 날씨 데이터 처리

sonny·2025년 1월 12일
1

TIL

목록 보기
100/140
import Foundation
import RxSwift
import RxCocoa

class WeatherViewModel {
    private let repository: WeatherRepositoryProtocol
    private let disposeBag = DisposeBag()
    
    let currentWeather = PublishRelay<Current>()
    
    init(repository: WeatherRepositoryProtocol) {
        self.repository = repository
    }
    
    func fetchWeatherResponse(lat: Double, lon: Double) {
        repository.fetchWeather(lat: lat, lon: lon)
            .subscribe(onSuccess: { [weak self] (response: WeatherResponse) in
                let current = Current(weatherDescription: response.currentWeather.weather[0].description,
                                      currentTemperature: response.currentWeather.temperature,
                                      weatherCode: response.currentWeather.weather[0].code,
                                      feelsLikeTemperature: response.currentWeather.feelsLike,
                                      minTemperature: response.dailyWeather[0].temperature.minTemperature,
                                      maxTemperature: response.dailyWeather[0].temperature.maxTemperature,
                                      windSpeed: response.currentWeather.windSpeed,
                                      humidity: response.currentWeather.humidity,
                                      rainProbability: response.hourlyWeather[0].rain)
                self?.currentWeather.accept(current)
            }, onFailure: { error in
                print("에러 발생: \(error)")
            })
            .disposed(by: disposeBag)
    }
}

설명은 나중에

profile
iOS 좋아. swift 좋아.

2개의 댓글

comment-user-thumbnail
2025년 1월 12일

설명주세요!!!!!!!!!!!

1개의 답글

관련 채용 정보