宽窄优行-由【嘉易行】项目成品而来
无故事王国
2023-05-25 dc1998fc1ac124f6b9a0e434ccf91103dd936409
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  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)
    }
}