JLChen
2021-10-08 f8457b624a75bf8e41489b74f66009eee6891b8b
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
//
//  Copyright © 2018 dahua. All rights reserved.
//
 
import UIKit
 
class DHWiFiConfigVC: DHBaseViewController {
    
    // todo: 修改wifi内容显示
    
    @IBOutlet weak var refreshButton: UIBarButtonItem!
    @IBOutlet weak var tableView: UITableView!
    private var presenter: DHWiFiConfigPresenter!
    public var deviceId: String = ""
    
    // MARK: - 🍇public method
    
    public static func storyboardInstance() -> DHWiFiConfigVC {
        let storyboard = UIStoryboard(name: "DHHomePage", bundle: Bundle.dh_addDeviceBundle())
        let controller = storyboard.instantiateViewController(withIdentifier: "DHWiFiConfigVC")
        return controller as! DHWiFiConfigVC
    }
    
    deinit {
        tableView.lc_clearTipsView()
        print(self)
    }
    
    // MARK: - 🍎override method
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "mobile_common_network_config".lc_T
        self.refreshButton.image = UIImage(named: "common_image_nav_refresh")
        //获取数据
        presenter = DHWiFiConfigPresenter.init(deviceId: self.deviceId)
        presenter.setContainer(container: self)
        presenter.loadWiFiList()
        //设置UI
        initTable()
    }
    // MARK: - 🍎private method
    
    private func initTable() {
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorColor = UIColor.dhcolor_c8()
        tableView.backgroundColor = UIColor.dhcolor_c7()
        tableView.register(DHWiFiConfigHeader.self, forHeaderFooterViewReuseIdentifier: "DHWiFiConfigHeader")
    }
 
    @objc func iconClicked() {
        presenter.explainWifiInfo()
    }
  
    // MARK: - 🍉action
    
    @IBAction func refreshAction(_ sender: UIBarButtonItem) {
        self.presenter.refresh()
    }
    
}
 
// MARK: - 🍑container protocol
 
extension DHWiFiConfigVC: IDHWiFiConfigContainer {
    func table() -> UITableView {
        return tableView
    }
    
    func navigationVC() -> UINavigationController? {
        return navigationController
    }
    
    func mainView() -> UIView {
        return view
    }
    
    func mainController() -> UIViewController {
        return self
    }
    
    func refreshEnable(isEnable: Bool) {
        if !isEnable {
            self.refreshButton.isEnabled = false
            var img = UIImage(named: "common_image_nav_refresh_disable")
            img = img?.withRenderingMode(.alwaysOriginal)
            self.refreshButton.image = img
        } else {
            self.refreshButton.isEnabled = true
            var img = UIImage(named: "common_image_nav_refresh")
            img = img?.withRenderingMode(.alwaysOriginal)
            self.refreshButton.image = img
        }
    }
    
}
 
// MARK: - 🍑table view datasource
 
// todo: 改了之后写得不好,需要修改
extension DHWiFiConfigVC: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return presenter.sectionNumber()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return presenter.numberOfRowsInSection(section: section)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section != (presenter.sectionNumber() - 1) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DHWiFiConfigListCell")
            presenter.configCell(cell: cell!, indexPath: indexPath)
            return cell!
        } else {
            let cell = UITableViewCell()
            cell.textLabel?.text = "其他..."
            cell.textLabel?.textColor = UIColor.dhcolor_c0()
            cell.selectionStyle = .none
            return cell
        }
    }
}
 
// MARK: - 🍑table view delegate
 
extension DHWiFiConfigVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if !presenter.hasConfigedWifi(), indexPath.section == 0, presenter.sectionNumber() != 1 {
            return 0
        }
        return 45.0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if !presenter.hasConfigedWifi(), section == 0, DHAddDeviceManager.sharedInstance.isSupport5GWifi {
            return 0.01
        } else if !DHAddDeviceManager.sharedInstance.isSupport5GWifi, section == 0 {
            return presenter.hasConfigedWifi() ? 186.0 : 156.0
        } else if section != (presenter.sectionNumber() - 1) {
            return 40.0
        }
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
 
        if !DHAddDeviceManager.sharedInstance.isSupport5GWifi, section == 0 {
            guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "DHWiFiConfigHeader") as? DHWiFiConfigHeader else {
                return nil
            }
            header.delegate = self
            header.titleImageView.image = DHAddDeviceManager.sharedInstance.isSupport5GWifi ? UIImage(named: "adddevice_icon_wifipassword") : UIImage(named: "adddevice_icon_wifipassword_nosupport5g")
            header.titleLabel.isHidden = DHAddDeviceManager.sharedInstance.isSupport5GWifi
            header.iconImageView.isHidden = DHAddDeviceManager.sharedInstance.isSupport5GWifi
            header.descButton.isHidden = !presenter.hasConfigedWifi()
            return header
        } else if !presenter.hasConfigedWifi(), section == 0 {
            return nil
        } else if section != (presenter.sectionNumber() - 1) {
            let headerview = UIView.init(frame: CGRect.init(x: 0, y: 0, width: dh_screenWidth, height: 40))
            let headerLabel = UILabel()
            headerview.clipsToBounds = true
            headerLabel.font = UIFont.dhFont_t5()
            headerLabel.textAlignment = .left
            headerLabel.text = ((section == 0) ? "device_manager_connected_wifi".lc_T : "device_manager_select_wifi".lc_T)
            headerview.addSubview(headerLabel)
            headerLabel.snp.makeConstraints { (make) in
                make.centerY.equalTo(headerview)
                make.left.equalTo(headerview).offset(16)
            }
            let iconImage = UIImageView()
            iconImage.image = UIImage(named: "adddevice_icon_help")
            iconImage.isUserInteractionEnabled = true
            headerview.addSubview(iconImage)
            iconImage.snp.makeConstraints { (make) in
                make.centerY.equalTo(headerview)
                make.left.equalTo(headerLabel.snp.right).offset(2)
            }
            let tap = UITapGestureRecognizer(target: self, action: #selector(iconClicked))
            iconImage.addGestureRecognizer(tap)
            iconImage.isHidden = (section == 0)
            return headerview
        }
        
        return nil
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section != (presenter.sectionNumber() - 1) {
            presenter.connectWifi(indexPath: indexPath)
        } else {
            presenter.connectHideWifi()
        }
    }
}
 
extension DHWiFiConfigVC: DHWiFiConfigHeaderDelegate {
    func iconDidClicked(type: DHWiFiConfigHeaderClickType) {
        presenter.explain5GInfo()
    }
}