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
217
218
219
220
221
222
223
224
225
226
227
228
//
//  Copyright © 2018年 dahua. All rights reserved.
//    设备类型选择界面
 
import UIKit
 
 
class DHSelectModelViewController: DHAddBaseViewController,  DHIdentifyContainerProtocol {
    
    @IBOutlet weak var inputBtn: UIButton!
    @IBOutlet weak var inputModelField: UITextField!
    @IBOutlet weak var otherModelBtn: UIButton!
    @IBOutlet weak var modelTip: UILabel!
    @IBOutlet weak var sureButton: UIButton!
    
    var topTipLabel: UILabel!
    
    var secTipLabel: UILabel!
    
    var imageTipLabel: UILabel!
    
    var tipImageView: UIImageView!
    
    var deviceId: String = ""
    
    var dataSource = [DHOMSDeviceType]()
    
    /// 每行显示的个数,数据源不足的填空的cell
    private var horizontalItemNumber = Int(4)
    
    /// 分隔线尺寸【iPhone6及以下,0.5的分隔线显示不了】
    private var separatorSize = dh_screenHeight <= 667 ? CGSize(width: 0.75, height: 0.75) : CGSize(width: 0.5, height: 0.5)
    
    private var presenter: DHIdentifyPresenter!
    
    public static func storyboardInstance() -> DHSelectModelViewController {
        let storyboard = UIStoryboard(name: "AddDevice", bundle: Bundle.dh_addDeviceBundle())
        if let controller = storyboard.instantiateViewController(withIdentifier: "DHSelectModelViewController") as? DHSelectModelViewController {
            return controller
        }
        return DHSelectModelViewController()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.setupView()
        self.setupPresenter()
        
        //【*】如果平台未加载成功过,仍需要重新更新数据
        dataSource.append(contentsOf: DHOMSConfigManager.sharedManager.types)//【*】每个类型加上通用
        
        if DHOMSConfigManager.sharedManager.isUpdatedSuccessfully == false {
            DHOMSConfigManager.sharedManager.checkUpdateDeviceModels()
        }
        
        self.addModelsUpdatedObserver()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func addModelsUpdatedObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(omsModelsUpdated), name: NSNotification.Name(rawValue: "LCNotificationOMSIModelsUpdated"), object: nil)
    }
    
    @objc func omsModelsUpdated() {
        if DHOMSConfigManager.sharedManager.types.count > 0 {
            self.dataSource.removeAll()
            self.dataSource.append(contentsOf: DHOMSConfigManager.sharedManager.types)
            
        }
    }
    
    func setupView() {
        self.inputBtn.setImage(UIImage.init(named: "adddevice_box_checkbox"), for: .normal)
        self.inputBtn.setImage(UIImage.init(named: "adddevice_box_checkbox_checked"), for: .selected)
        self.inputBtn.addTarget(self, action: #selector(inputAction), for: .touchUpInside)
        self.otherModelBtn.setImage(UIImage.init(named: "adddevice_box_checkbox"), for: .normal)
        self.otherModelBtn.setImage(UIImage.init(named: "adddevice_box_checkbox_checked"), for: .selected)
        self.otherModelBtn.addTarget(self, action: #selector(otherAction), for: .touchUpInside)
        self.inputModelField.isUserInteractionEnabled = false
        self.inputModelField.returnKeyType = .done
        self.modelTip.textColor = UIColor.dhcolor_c30()
        self.sureButton.backgroundColor = UIColor.dhcolor_c10()
        self.sureButton.setTitle("Button_Confirm".lc_T, for: .normal)
        self.sureButton.layer.cornerRadius = 22.5
        self.sureButton.layer.masksToBounds = true
        self.sureButton.addTarget(self, action: #selector(sureAction), for: .touchUpInside)
        
        self.topTipLabel = UILabel.init()
        self.topTipLabel.numberOfLines = 0
        self.view.addSubview(self.topTipLabel)
        self.topTipLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.modelTip)
            make.right.equalTo(self.modelTip)
            make.top.equalTo(self.modelTip.snp_bottom).offset(15)
        }
        let style = NSMutableParagraphStyle.init()
        let str = NSMutableAttributedString(string: "add_device_lechange_tip".lc_T, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)])
        str.addAttributes([NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 15)], range: NSMakeRange(0, 19))
        str.addAttributes([NSAttributedStringKey.paragraphStyle:style], range: NSMakeRange(0, str.length))
        self.topTipLabel.attributedText = str
        
        self.secTipLabel = UILabel.init()
        self.secTipLabel.numberOfLines = 0
        self.view.addSubview(self.secTipLabel)
        self.secTipLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.topTipLabel)
            make.right.equalTo(self.topTipLabel)
            make.top.equalTo(self.topTipLabel.snp_bottom).offset(15)
        }
        let secStr = NSMutableAttributedString(string: "add_device_dahua_tip".lc_T, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)])
        secStr.addAttributes([NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 15)], range: NSMakeRange(0, 17))
        secStr.addAttributes([NSAttributedStringKey.paragraphStyle:style], range: NSMakeRange(0, str.length))
        self.secTipLabel.attributedText = secStr
        
        self.imageTipLabel = UILabel.init()
        self.imageTipLabel.text = "add_device_image_tip".lc_T
        self.imageTipLabel.font = UIFont.dhFont_f2Bold()
        self.view.addSubview(self.imageTipLabel)
        self.imageTipLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.topTipLabel)
            make.right.equalTo(self.topTipLabel)
            make.top.equalTo(self.secTipLabel.snp_bottom).offset(15)
        }
        
        self.tipImageView = UIImageView.init()
        self.tipImageView.image = UIImage.init(named: "adddevice_tip_image")
        self.tipImageView.contentMode = .scaleToFill
        self.view.addSubview(self.tipImageView)
        self.tipImageView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(20)
            make.right.equalTo(self.view).offset(-20)
            make.top.equalTo(self.imageTipLabel.snp_bottom)
            make.height.equalTo(self.tipImageView.snp_width).multipliedBy(0.57)
        }
        
    }
    
    func setupPresenter() {
        self.presenter = DHIdentifyPresenter()
        self.presenter.setup(container: self)
    }
    
    // MARK: Private
    @objc func inputAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            self.otherModelBtn.isSelected = false
            self.inputModelField.isUserInteractionEnabled = true
        }
    }
    
    @objc func otherAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            self.inputBtn.isSelected = false
            self.inputModelField.isUserInteractionEnabled = false
        }
    }
    
    @objc func sureAction(_ sender: UIButton) {
        //没有选中按钮
        if !self.inputBtn.isSelected && !self.otherModelBtn.isSelected {
            LCProgressHUD.showMsg("请选择设备类型".lc_T)
            return
        }
        //选中输入按钮
        if self.inputBtn.isSelected {
            if self.inputModelField.text?.count == 0 {
                LCProgressHUD.showMsg("请输入设备类型".lc_T)
                return
            } else {
                self.presenter.getDeviceInfo(deviceId: self.deviceId, qrModel: nil, ncCode: nil, marketModel: self.inputModelField.text ?? "", modeOptional: true)
            }
        }
        
        //选中其他按钮,取数组最后一项来进行设备添加
        if self.otherModelBtn.isSelected {
            if self.dataSource.count > 0 {
                let item = self.dataSource.last?.modelItems.lastObject as? DHOMSDeviceModelItem
                let modelName = item?.deviceModelName
                self.presenter.getDeviceInfo(deviceId: deviceId, qrModel: nil, ncCode: nil, marketModel: modelName, modeOptional: true)
            }
        }
    }
    
    // MARK: DHIdentifyContainerProtocol
    func navigationVC() -> UINavigationController? {
        return self.navigationController
    }
    
    func mainView() -> UIView {
        return self.view
    }
    
    func mainController() -> UIViewController {
        return self
    }
    
    func pauseIdentify() {
        
    }
    
    func resumeIdenfity() {
        
    }
    
    func showAddBoxGuidView(needShoeBox: @escaping ((Bool) -> Void)) {
        
    }
    
    func retry() {
        
    }
    
    override func isRightActionHidden() -> Bool {
        return true
    }
    
    func smb_updateUI(deviceInfo: DHUserDeviceBindInfo) {
        
    }
}