Statistics
| Branch: | Tag: | Revision:

chatsecureios / ChatSecure / Classes / Views / Cells / OTRUsernameCell.swift @ 9a71f7ed

History | View | Annotate | Download (4.74 KB)

1
//
2
//  OTRUsernameCell.swift
3
//  ChatSecure
4
//
5
//  Created by Christopher Ballinger on 8/4/15.
6
//  Copyright (c) 2015 Chris Ballinger. All rights reserved.
7
//
8

    
9
import UIKit
10
import XLForm
11
import ParkedTextField
12
import OTRAssets
13

    
14
open class OTRUsernameValidator: NSObject, XLFormValidatorProtocol {
15
    open func isValid(_ row: XLFormRowDescriptor!) -> XLFormValidationStatus! {
16
        var isValid = false
17
        if let value = row.value as? String {
18
            let (username, domain) = OTRUsernameCell.splitJID(value)
19
            if username.count > 0 && domain.count > 0 {
20
                isValid = true
21
            }
22
        }
23
        let status: XLFormValidationStatus = XLFormValidationStatus(msg: "", status: isValid, rowDescriptor: row)
24
        return status
25
    }
26
}
27

    
28
@objc(OTRUsernameCell)
29
open class OTRUsernameCell: XLFormBaseCell, UITextFieldDelegate {
30

    
31
    @IBOutlet var usernameLabel: UILabel!
32
    @IBOutlet var usernameField: ParkedTextField!
33
    
34
    deinit {
35
        self.usernameField.delegate = nil
36
    }
37
    
38
    override open func awakeFromNib() {
39
        super.awakeFromNib()
40
        // Initialization code
41
        self.usernameField.delegate = self
42
    }
43
    
44
    // MARK: XLFormBaseCell overrides
45
    
46
    override open func configure() {
47
        super.configure()
48
        self.selectionStyle = UITableViewCellSelectionStyle.none
49
    }
50
    
51
    
52
    override open func highlight() {
53
        super.highlight()
54
        self.usernameLabel.textColor = self.tintColor
55
    }
56
    
57
    override open func unhighlight() {
58
        super.unhighlight()
59
        self.formViewController().updateFormRow(self.rowDescriptor)
60
    }
61
    
62
   
63
    override open func update() {
64
        super.update()
65
        self.usernameField.delegate = self
66
        self.usernameLabel.textColor = UIColor.darkText
67
        
68
        if let value = self.rowDescriptor!.value as? NSString {
69
            let (username, domain) = OTRUsernameCell.splitJID(value as String)
70
            if username.count > 0 {
71
                self.usernameField.typedText = username
72
            }
73
            if domain.count > 0 {
74
                self.usernameField.parkedText = "@" + domain
75
            }
76
        }
77
    }
78
    
79
    // MARK: XLFormDescriptorCell
80
    
81
    override open static func formDescriptorCellHeight(for rowDescriptor: XLFormRowDescriptor!) -> CGFloat {
82
        return 43
83
    }
84
    
85
    override open func formDescriptorCellCanBecomeFirstResponder() -> Bool {
86
        return !self.rowDescriptor!.isDisabled()
87
    }
88
    
89
    override open func formDescriptorCellBecomeFirstResponder() -> Bool {
90
        self.highlight()
91
        return self.usernameField.becomeFirstResponder()
92
    }
93
    
94
    // MARK: UITextFieldDelegate
95
    
96
    open func textFieldShouldClear(_ textField: UITextField) -> Bool {
97
        return self.formViewController().textFieldShouldClear(textField)
98
    }
99
    
100
    open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
101
        return self.formViewController().textFieldShouldReturn(textField)
102
    }
103
    
104
    open func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
105
        return self.formViewController().textFieldShouldBeginEditing(textField)
106
    }
107
    
108
    open func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
109
        return self.formViewController().textFieldShouldEndEditing(textField)
110
    }
111
    
112
    open func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
113
        return self.formViewController().textField(textField, shouldChangeCharactersIn: range, replacementString: string)
114
    }
115
   
116
    open func textFieldDidBeginEditing(_ textField: UITextField) {
117
        self.formViewController().beginEditing(self.rowDescriptor)
118
        self.formViewController().textFieldDidBeginEditing(textField)
119
    }
120
    
121
    open func textFieldDidEndEditing(_ textField: UITextField) {
122
        self.textFieldValueChanged(self.usernameField)
123
        self.formViewController().endEditing(self.rowDescriptor)
124
        self.formViewController().textFieldDidEndEditing(textField)
125
    }
126
    
127
    // MARK: UITextField value changes
128
    
129
    @IBAction func textFieldValueChanged(_ sender: ParkedTextField) {
130
        let value = sender.typedText + sender.parkedText
131
        rowDescriptor?.value = value
132
    }
133
    
134
    // MARK: Private methods
135
    
136
    
137
    fileprivate static func splitJID(_ jid: String) -> (username: String, domain: String) {
138
        let value = jid as NSString
139
        var username: String = ""
140
        var domain: String = ""
141
        if value.contains("@") {
142
            let components = value.components(separatedBy: "@")
143
            username = components.first ?? ""
144
            domain = components.last ?? ""
145
        } else {
146
            domain = value as String
147
        }
148
        return (username, domain)
149
    }
150
    
151
}