//
|
// YYLocationViewModel.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/5/29.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import RxSwift
|
import RxCocoa
|
|
class YYLocationViewModel: YYViewModel {
|
|
/// 当前城市
|
let currentCity = BehaviorRelay<String>(value: YYLocationManager.shared.currentCity)
|
/// 当前城市代码
|
let currentCityCode = BehaviorRelay<String>(value: YYLocationManager.shared.currentCityCode)
|
|
/// 当前定位城市
|
let currentLocationCity = BehaviorRelay<String>(value: "")
|
|
let locationSubject = PublishSubject<RequestStatus>()
|
let validateCitySubject = PublishSubject<RequestStatus>()
|
|
/// 请求定位
|
func requestLocation() {
|
|
locationSubject.onNext(.loading)
|
|
YYLocationManager.shared.requestLocation(success: { (model) in
|
YYLocationManager.shared.province = model.province
|
YYLocationManager.shared.currentCity = model.city
|
YYLocationManager.shared.district = model.district
|
YYLocationManager.shared.currentCityCode = model.citycode
|
self.currentLocationCity.accept(model.city)
|
self.locationSubject.onNext(.success(model))
|
}) { (error) in
|
self.locationSubject.onNext(.error(error))
|
}
|
}
|
|
/// 验证是否是开通城市
|
func validateCity() {
|
validateCitySubject.onNext(.loading)
|
APIManager.shared.provider.rx
|
.request(.openCity(code: YYLocationManager.shared.currentCityCode))
|
.map(YYModel<ValidateCityModel>.self)
|
.validate()
|
.subscribe(onSuccess: { [unowned self] (model) in
|
|
if model.data?.open == 1 {
|
YYLocationManager.shared.isValidated.accept(true)
|
} else {
|
YYLocationManager.shared.isValidated.accept(false)
|
}
|
self.validateCitySubject.onNext(.success(model))
|
}) { [unowned self] (error) in
|
self.validateCitySubject.onNext(.error(error))
|
}
|
.disposed(by: disposeBag)
|
}
|
}
|