JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
//  Copyright © 2018年 iblue. All rights reserved.
//
 
import UIKit
import CoreLocation
 
class DHApGuideViewController: DHAddBaseViewController {
 
    private var guideViews: [DHAddGuideView] = [DHAddGuideView]()
    
    private lazy var locationManager: CLLocationManager = {
        let location = CLLocationManager()
        return location
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        
        self.getGuideInfo()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //配置引导信息
    private func getGuideInfo() {
        let manager = DHAddDeviceManager.sharedInstance
        let introduction = manager.getIntroductionParser()
        
        //【*】已有数据,直接进行加载
        //【*】没有数据,但在更新中,使用默认值,等待更新完成后刷新界面
        //【*】没有数据,且不在更新,使用默认值,并更新
        if let steps = introduction?.softApGuideInfo.guideSteps, steps.count > 0 {
            self.setup(guideSteps: steps)
            return
        } else {
            var step = DHOMSSoftApGuideStep()
            step.content = DHOMSSoftApGuideDefault.content
            self.setup(guideSteps: [step])
            
        }
        
        //【*】监听回调通知
        self.baseAddOMSIntroductionObserver()
    
        if manager.isIntroductionUpdating() {
//            LCProgressHUD.showHub(on: self.view, duration: 5.0)
//            LCProgressHUD.show(on: self.view)
            
        } else {
            if let model = manager.deviceMarketModel {
                DHOMSConfigManager.sharedManager.checkUpdateIntrodution(byMarketModel: model)
            }
        }
    }
    
    /// 设置引导信息
    private func setup(guideSteps: [DHOMSSoftApGuideStep]) {
        guard guideSteps.count > 0 else {
            return
        }
        
        guideViews.removeAll()
        
        for guideStep in guideSteps {
            let guideView = DHAddGuideView.xibInstance()
            guideView.delegate = self
            guideView.frame = self.view.bounds
            guideViews.append(guideView)
            guideView.topTipLabel.dh_setAttributedText(text: guideStep.content, font: UIFont.dhFont_t1())
            guideView.errorButton.titleLabel?.font = UIFont.dhFont_t1()
            guideView.topTipLabel.textAlignment = .center
            
            guideView.topImageView.lc_setImage(withUrl: guideStep.imageUrl, placeholderImage: DHOMSSoftApGuideDefault.imagename, toDisk: true )
            guideView.setCheckHidden(hidden: true)
            guideView.setDetailButtonHidden(hidden: true)
            
            adjustConstraint(guideView: guideView)
        }
        
        if let lastGuideView = guideViews.last {
            lastGuideView.setDetailButtonHidden(hidden: false)
            
            let introduction = DHAddDeviceManager.sharedInstance.getIntroductionParser()
            lastGuideView.setDetailButton(text: introduction?.lightResetInfo.resetGuide ?? DHSoftApResetDefault.guide)
            
            //DTS000797168 问题修复
            if guideSteps.count == 1,guideSteps[0].content == DHOMSSoftApGuideDefault.content
            {
                lastGuideView.setDetailButtonHidden(hidden: true)
            }
        }
        
        
    
        resetGuideViewsToFirstStep()
    }
    
    /// 调整GuideView的约束
    private func adjustConstraint(guideView: DHAddGuideView) {
 
        if dh_screenHeight < 667 {
            guideView.updateTopImageViewConstraint(top: 0, width: 240, maxHeight: 240)
            guideView.updateContentLabelConstraint(top: 5)
        } else {
            guideView.updateTopImageViewConstraint(top: 0, width: UIScreen.main.bounds.size.width, maxHeight: 320)
            guideView.updateContentLabelConstraint(top: 10)
        }
    }
    
    /// 重置引导图至第一页
    public func resetGuideViewsToFirstStep() {
        guideViews.forEach { (view) in
            view.removeFromSuperview()
        }
        
        if guideViews.count > 0 {
            self.view.addSubview(guideViews[0])
            guideViews[0].dh_x = 0
        }
    }
    
    // MARK: DHAddBaseVCProtocol(OMS Introduction)
    override func needUpdateCurrentOMSIntroduction() {
        LCProgressHUD.hideAllHuds(self.view)
        let parser = DHAddDeviceManager.sharedInstance.getIntroductionParser()
        
        //【*】更新成功了使用实际的
        //【*】未更新成功,使用默认的
        if let steps = parser?.softApGuideInfo.guideSteps, steps.count > 0 {
            self.setup(guideSteps: steps)
        } else {
            var step = DHOMSSoftApGuideStep()
            step.content = DHOMSSoftApGuideDefault.content
            self.setup(guideSteps: [step])
        }
    }
}
 
extension DHApGuideViewController: DHAddGuideViewDelegate {
    
    func guideView(view: DHAddGuideView, action: DHAddGuideActionType) {
        if action == .detail {
            //进入重置
            pushToDeviceResetVC()
        } else if action == .next {
            let index = guideViews.index(of: view)!
            if index < guideViews.count - 1 {
                // 切换下一页
                let nextView = guideViews[index + 1]
                trans(view: view, toView: nextView)
            } else {
                //iOS13界面:
                if #available(iOS 13.0, *) {
                    let status = CLLocationManager.authorizationStatus()
                    if status == .notDetermined {
                        //申请权限
                        locationManager.requestWhenInUseAuthorization()
                    } else if status == .denied {
                        //被拒绝后,重新申请
                        LCSetJurisdictionHelper.setJurisdictionAlertView("mobile_common_permission_apply".lc_T, message: "mobile_common_permission_explain_access_location_usage".lc_T)
                    } else {
                        //有位置访问权限,跳转下一步
                        goLastStep()
                    }
                } else {
                    goLastStep()
                }
            }
        } else if action == .error {
            let vc = LCDeviceAddErrorController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func goLastStep() {
        //最后一步,跳转到网络检查;并重置引导
        resetGuideViewsToFirstStep()
        let controller = DHApWifiCheckViewController()
        navigationController?.pushViewController(controller, animated: true)
    }
    
    private func trans(view: DHAddGuideView, toView: DHAddGuideView) {
        toView.frame = self.view.bounds
        self.view.addSubview(toView)
        toView.dh_x = dh_screenWidth
        
        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
            view.dh_x = -dh_screenWidth
            toView.dh_x = 0
            view.nextButton.alpha = 0
            
        }) { _ in
            view.nextButton.alpha = 1
            view.removeFromSuperview()
        }
    }
    
    private func pushToDeviceResetVC() {
        let controller = DHResetDeviceViewController()
        let introductionParser = DHAddDeviceManager.sharedInstance.getIntroductionParser()
        controller.placeholderImage = DHSoftApResetDefault.imagename
        controller.imageUrl = introductionParser?.lightResetInfo.resetImageUrl
        
        controller.resetContent = introductionParser?.lightResetInfo.resetOperation ?? DHSoftApResetDefault.operation
        self.navigationController?.pushViewController(controller, animated: true)
    }
}