HomeiOS Developmentswift - Tips on how to join two ViewModels inside MVVM, iOS,...

swift – Tips on how to join two ViewModels inside MVVM, iOS, UIKit


I’m writing climate app which accommodates two controllers and two view fashions WeatherViewModel and SearchViewModel

class WeatherViewModel {

var searchViewModel: SearchViewModel
var bindableWeather = Bindable<OverallWeatherModel>()


init(searchViewModel: SearchViewModel){
    self.searchViewModel = searchViewModel
    searchViewModel.bindableSearchWeather.bind { [weak self] climate in
        self?.bindableWeather.worth = climate
    }
}

}

class SearchViewModel {

var bindableSearchWeather = Bindable<OverallWeatherModel>()
var searchCity: String? {didSet {fetchCityData(metropolis: searchCity ?? "")}}

fileprivate var overallWeatherModel =  OverallWeatherModel()

fileprivate func fetchCityData(metropolis: String) {
    Service.shared.fetchCityData(metropolis: metropolis) {[weak self] cityGroup in
        guard let metropolis = cityGroup.first else {return}
        self?.overallWeatherModel.metropolis = metropolis.identify
        self?.overallWeatherModel.nation = metropolis.nation
        self?.fetchWeatherData(lat: metropolis.lat, lengthy: metropolis.lon)
    }
}

fileprivate func fetchWeatherData(lat: Double, lengthy: Double) {
    Service.shared.fetchTempData(lat: lat, lengthy: lengthy) {[weak self] weatherGroup in
        self?.overallWeatherModel.date = DateFormat.shared.currentDate()
        self?.overallWeatherModel.temp = Int(weatherGroup.major.temp - 273)
        let weatherIcon = "http://openweathermap.org/img/w/(weatherGroup.climate?.first?.icon ?? "").png"
        self?.overallWeatherModel.weatherIconUrl = URL(string: weatherIcon)
        self?.overallWeatherModel.windStatus = Int(weatherGroup.wind.pace)
        self?.overallWeatherModel.humidity = weatherGroup.major.humidity
        self?.overallWeatherModel.visibility = weatherGroup.visibility
        self?.overallWeatherModel.airPressure = weatherGroup.major.strain
        self?.fetchNextWeekData(lat: lat, lengthy: lengthy)
    }
}
fileprivate func fetchNextWeekData(lat: Double, lengthy: Double) {
    Service.shared.fetchTempDataForWeek(lat: lat, lengthy: lengthy) { [weak self] nextweek in
        self?.overallWeatherModel.temp1 = Int(nextweek.record[0].major.temp - 273)
        self?.overallWeatherModel.temp2 = Int(nextweek.record[1].major.temp - 273)
        self?.overallWeatherModel.temp3 = Int(nextweek.record[2].major.temp - 273)
        self?.overallWeatherModel.temp4 = Int(nextweek.record[3].major.temp - 273)
        self?.overallWeatherModel.temp5 = Int(nextweek.record[4].major.temp - 273)
        let weatherIcon1 = "http://openweathermap.org/img/w/(nextweek.record[0].climate.first?.icon ?? "").png"
        let weatherIcon2 = "http://openweathermap.org/img/w/(nextweek.record[1].climate.first?.icon ?? "").png"
        let weatherIcon3 = "http://openweathermap.org/img/w/(nextweek.record[2].climate.first?.icon ?? "").png"
        let weatherIcon4 = "http://openweathermap.org/img/w/(nextweek.record[3].climate.first?.icon ?? "").png"
        let weatherIcon5 = "http://openweathermap.org/img/w/(nextweek.record[4].climate.first?.icon ?? "").png"
        self?.overallWeatherModel.icon1 = URL(string: weatherIcon1)
        self?.overallWeatherModel.icon2 = URL(string: weatherIcon2)
        self?.overallWeatherModel.icon3 = URL(string: weatherIcon3)
        self?.overallWeatherModel.icon4 = URL(string: weatherIcon4)
        self?.overallWeatherModel.icon5 = URL(string: weatherIcon5)
        self?.overallWeatherModel.date1 = DateFormat.shared.nextDate(day: 1)
        self?.overallWeatherModel.date2 = DateFormat.shared.nextDate(day: 2)
        self?.overallWeatherModel.date3 = DateFormat.shared.nextDate(day: 3)
        self?.overallWeatherModel.date4 = DateFormat.shared.nextDate(day: 4)
        self?.overallWeatherModel.date5 = DateFormat.shared.nextDate(day: 5)
        self?.bindableSearchWeather.worth = self?.overallWeatherModel
    }
}

}

SearchViewModel is creating an API name and getting knowledge, then I’m sending this knowledge to the WeatherVewModel, which is in control of altering view on its controller. For passing knowledge I’m utilizing Observable/Bindable and in addition embedding SearchViewModel within WeatherVewModel so it could possibly ship knowledge to it. My query is am I doing this appropriately or not? Have a sense that embedding one viewmodel within one other is not proper.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments