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
//
//  Copyright © 2018年 Zhejiang Dahua Technology Co.,Ltd. All rights reserved.
//    密码检验
//    【*】国内:根据Auth能力集,点下一步时,绑定
//    【*】海外:登录设备检验密码,如果前面初始化流程传入了密码,则不需要进入,直接在连接云平台绑定设备;
 
import UIKit
import LCBaseModule
 
class DHAuthPasswordViewController: DHAddBaseViewController, UITextFieldDelegate, UITextViewDelegate, DHBindContainerProtocol {
 
    public static func storyboardInstance() -> DHAuthPasswordViewController {
        let storyboard = UIStoryboard(name: "AddDevice", bundle: Bundle.dh_addDeviceBundle())
        if let controller = storyboard.instantiateViewController(withIdentifier: "DHAuthPasswordViewController") as? DHAuthPasswordViewController {
            return controller
        }
        return DHAuthPasswordViewController()
    }
    
    public var presenter: DHAuthPasswordPresenterProtocol?
    
    @IBOutlet weak var inputTipLabel: UILabel!
    @IBOutlet weak var passwordInputView: LCInputView!
    @IBOutlet weak var bottomLine: UIView!
    @IBOutlet weak var topLine: UIView!
    @IBOutlet weak var warmTipView: UIView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var detailTextView: UITextView!
    @IBOutlet weak var nextButton: UIButton!
    @IBOutlet weak var textViewHeightConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view.
        setupContents()
        
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func setupContents() {
        view.backgroundColor = UIColor.dhcolor_c43()
        
        //SMB:色值修改
        tipLabel.text = "add_device_kindly_reminder".lc_T
        tipLabel.textColor = UIColor.dhcolor_c0() //UIColor.dhcolor_c2()
        nextButton.setTitle("common_next".lc_T, for: .normal)
        
        
        inputTipLabel.text = "add_device_input_device_password".lc_T
        inputTipLabel.textColor = UIColor.dhcolor_c2()
        passwordInputView.textField.placeholder = "add_device_input_device_password".lc_T
        passwordInputView.backgroundColor = UIColor.dhcolor_c43()
        
        nextButton.layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
        nextButton.backgroundColor = DHModuleConfig.shareInstance().commonButtonColor()
        nextButton.dh_enable = false
        nextButton.setTitleColor(UIColor.dhcolor_c43(), for: .normal)
        
        topLine.backgroundColor = UIColor.dhcolor_c8()
        bottomLine.backgroundColor = UIColor.dhcolor_c8()
        
        //设置TextView,dataDetectorTypes为何无效??
        detailTextView.delegate = self
        detailTextView.isUserInteractionEnabled = true
        detailTextView.isEditable = false
        detailTextView.isSelectable = true
        detailTextView.dataDetectorTypes = .link
        detailTextView.textColor = UIColor.dhcolor_c5()
        
        let style = NSMutableParagraphStyle()
        style.lineBreakMode = NSLineBreakMode.byWordWrapping
        style.lineSpacing = 5
        let attr1: [NSAttributedStringKey: Any] = [NSAttributedStringKey.paragraphStyle: style,
                                                    NSAttributedStringKey.foregroundColor: UIColor.dhcolor_c5(),
                                                    NSAttributedStringKey.font: UIFont.dhFont_t6()]
        
        let text = "add_device_password_initial_tip".lc_T
        let rang1 = NSMakeRange(0, text.count)
        let attrText = NSMutableAttributedString(string: text)
        attrText.addAttributes(attr1, range: rang1)
        
        if DHModuleConfig.shareInstance().isLeChange, DHModuleConfig.shareInstance().addDevice_showServiceCall() {
            let phoneNumber = DHModuleConfig.shareInstance().serviceCall() ?? ""
            let rang2 = NSMakeRange(0, phoneNumber.count)
            let attr2: [NSAttributedStringKey: Any] = [NSAttributedStringKey.paragraphStyle: style,
                                                        NSAttributedStringKey.link: "serviceCall",
                                                        NSAttributedStringKey.foregroundColor: UIColor.dhcolor_c32(),
                                                        //NSAttributedStringKey.underlineStyle: 1,
                                                        NSAttributedStringKey.font: UIFont.dhFont_t6()]
            let attrPhone = NSMutableAttributedString(string: phoneNumber)
            attrPhone.addAttributes(attr2, range: rang2)
            attrText.append(attrPhone)
        }
        
        detailTextView.attributedText = attrText
        detailTextView.scrollRangeToVisible(NSMakeRange(0, 0))
        let rect = attrText.string.boundingRect(with: CGSize(width: view.bounds.width - 30, height: view.bounds.height),
                                                options: NSStringDrawingOptions.usesLineFragmentOrigin,
                                                attributes: attr1,
                                                context: nil)
        textViewHeightConstraint.constant = rect.height + 30
        
        //验证密码不需要限制
        //passwordInputView.textField.lc_setInputRule(withRegEx: "^[A-Za-z0-9]$", andInputLength: UInt32(DH_PASSWORD_MAX_LENGTH))
        passwordInputView.textField.delegate = self
        passwordInputView.textField.keyboardType = .asciiCapable
        passwordInputView.textField.returnKeyType = .done
        passwordInputView.textField.textChanged = { [unowned self] text in
            let password = text ?? ""
            self.nextButton.dh_enable = password.count > 0
        }
    }
    
    @IBAction func onNextAction(_ sender: Any) {
        let password = passwordInputView.textField.text ?? ""
        let device = DHAddDeviceManager.sharedInstance.getLocalDevice()
        
        passwordInputView?.textField.resignFirstResponder()
        
        //如果是AP是在线的,可能局域网搜索不到,无法用netsdk方式进行检验
        presenter?.nextStepAction(password: password, device: device, deviceId: DHAddDeviceManager.sharedInstance.deviceId)
        
    }
 
    // MARK: DHAddBaseVCProtocol
    override func leftActionType() -> DHAddBaseLeftAction {
        return .quit
    }
    
    override func isLeftActionShowAlert() -> Bool {
        return true
    }
    
    override func rightActionType() -> [DHAddBaseRightAction] {
        return [.restart]
    }
}
 
extension DHAuthPasswordViewController {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func textView(_ textView: UITextView, shouldInteractWith txtViewURL: URL, in characterRange: NSRange) -> Bool {
        if txtViewURL.absoluteString == "serviceCall" {
            let phone = "telprompt://" + DHModuleConfig.shareInstance().serviceCall()
            if let url = URL(string: phone) {
                UIApplication.shared.openURL(url)
            }
            
            return true
        }
        
        return false
    }
}
 
extension DHAuthPasswordViewController {
    
    func navigationVC() -> UINavigationController? {
        return self.navigationController
    }
    
    func mainView() -> UIView {
        return self.view
    }
    
    func mainController() -> UIViewController {
        return self
    }
    
    func retry() {
        
    }
}