Skip to content

Commit bc657f4

Browse files
author
Athul Sai
committed
Separate KWVerificationCodeView Code
1 parent 4fa43bf commit bc657f4

7 files changed

Lines changed: 280 additions & 194 deletions

File tree

Example/KWVerificationCodeView/Base.lproj/Main.storyboard

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,12 @@
2121
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2222
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2323
<subviews>
24-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ekS-Ew-GpT" userLabel="verificationView">
24+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ekS-Ew-GpT" userLabel="verificationCodeView" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
2525
<rect key="frame" x="33" y="273" width="310" height="62"/>
26-
<subviews>
27-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="pAW-9I-Grs" userLabel="verificationCodeView1" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
28-
<rect key="frame" x="40" y="6" width="50" height="50"/>
29-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
30-
<constraints>
31-
<constraint firstAttribute="height" constant="50" id="D70-kv-yjG"/>
32-
<constraint firstAttribute="width" constant="50" id="Tge-Fu-oIj"/>
33-
</constraints>
34-
</view>
35-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="KnL-ww-PSX" userLabel="verificationCodeView2" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
36-
<rect key="frame" x="100" y="6" width="50" height="50"/>
37-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
38-
</view>
39-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="4Am-VB-b0X" userLabel="verificationCodeView3" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
40-
<rect key="frame" x="160" y="6" width="50" height="50"/>
41-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
42-
</view>
43-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="jCB-ix-jVy" userLabel="verificationCodeView4" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
44-
<rect key="frame" x="220" y="6" width="50" height="50"/>
45-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
46-
</view>
47-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="IIA-q0-jPG" userLabel="topView">
48-
<rect key="frame" x="0.0" y="0.0" width="310" height="62"/>
49-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
50-
</view>
51-
</subviews>
5226
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
5327
<constraints>
54-
<constraint firstItem="KnL-ww-PSX" firstAttribute="height" secondItem="pAW-9I-Grs" secondAttribute="height" id="0w5-Iv-0aM"/>
55-
<constraint firstItem="4Am-VB-b0X" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" constant="6" id="467-IS-62H"/>
56-
<constraint firstItem="4Am-VB-b0X" firstAttribute="leading" secondItem="KnL-ww-PSX" secondAttribute="trailing" constant="10" id="9no-YN-a3g"/>
57-
<constraint firstItem="KnL-ww-PSX" firstAttribute="leading" secondItem="pAW-9I-Grs" secondAttribute="trailing" constant="10" id="EtW-5V-wRS"/>
58-
<constraint firstItem="jCB-ix-jVy" firstAttribute="height" secondItem="4Am-VB-b0X" secondAttribute="height" id="H6Z-iL-Gby"/>
59-
<constraint firstItem="jCB-ix-jVy" firstAttribute="leading" secondItem="4Am-VB-b0X" secondAttribute="trailing" constant="10" id="LUP-1p-GKY"/>
60-
<constraint firstItem="jCB-ix-jVy" firstAttribute="width" secondItem="4Am-VB-b0X" secondAttribute="width" id="Oo3-Mx-syM"/>
61-
<constraint firstAttribute="bottom" secondItem="IIA-q0-jPG" secondAttribute="bottom" id="QlT-T1-yte"/>
62-
<constraint firstItem="4Am-VB-b0X" firstAttribute="height" secondItem="KnL-ww-PSX" secondAttribute="height" id="Sht-Ab-1NM"/>
6328
<constraint firstAttribute="width" constant="310" id="SmR-e4-qbu"/>
64-
<constraint firstItem="IIA-q0-jPG" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" id="TDb-iM-ynn"/>
65-
<constraint firstItem="KnL-ww-PSX" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" constant="6" id="YIt-VA-9pj"/>
66-
<constraint firstItem="KnL-ww-PSX" firstAttribute="width" secondItem="pAW-9I-Grs" secondAttribute="width" id="af2-o8-CgB"/>
67-
<constraint firstAttribute="bottom" secondItem="pAW-9I-Grs" secondAttribute="bottom" constant="6" id="cr1-oF-szO"/>
68-
<constraint firstItem="jCB-ix-jVy" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" constant="6" id="dUg-Fd-YPP"/>
6929
<constraint firstAttribute="height" constant="62" id="mda-A6-NvC"/>
70-
<constraint firstItem="4Am-VB-b0X" firstAttribute="width" secondItem="KnL-ww-PSX" secondAttribute="width" id="qK6-Of-XBI"/>
71-
<constraint firstItem="pAW-9I-Grs" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" constant="6" id="tPY-XH-pWS"/>
72-
<constraint firstItem="pAW-9I-Grs" firstAttribute="leading" secondItem="ekS-Ew-GpT" secondAttribute="leading" constant="40" id="vJb-5J-83w"/>
73-
<constraint firstAttribute="trailing" secondItem="IIA-q0-jPG" secondAttribute="trailing" id="w1L-mH-VZy"/>
74-
<constraint firstItem="IIA-q0-jPG" firstAttribute="leading" secondItem="ekS-Ew-GpT" secondAttribute="leading" id="y1n-FM-Zsl"/>
7530
</constraints>
7631
</view>
7732
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Skh-28-8br" userLabel="submitButton">
@@ -113,10 +68,7 @@
11368
</constraints>
11469
</view>
11570
<connections>
116-
<outlet property="verificationCodeView1" destination="pAW-9I-Grs" id="SyF-Zo-vkq"/>
117-
<outlet property="verificationCodeView2" destination="KnL-ww-PSX" id="wzV-6o-vAm"/>
118-
<outlet property="verificationCodeView3" destination="4Am-VB-b0X" id="oU5-8m-0yx"/>
119-
<outlet property="verificationCodeView4" destination="jCB-ix-jVy" id="iVf-3u-DNn"/>
71+
<outlet property="verificationCodeView" destination="ekS-Ew-GpT" id="AtV-hT-QUf"/>
12072
</connections>
12173
</viewController>
12274
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>

Example/KWVerificationCodeView/VerificationCodeViewController.swift

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,20 @@ import KWVerificationCodeView
1212
class VerificationCodeViewController: UIViewController {
1313

1414
// MARK: - IBOutlets
15-
@IBOutlet weak var verificationCodeView1: KWVerificationCodeView!
16-
@IBOutlet weak var verificationCodeView2: KWVerificationCodeView!
17-
@IBOutlet weak var verificationCodeView3: KWVerificationCodeView!
18-
@IBOutlet weak var verificationCodeView4: KWVerificationCodeView!
19-
20-
// MARK: - Variables
21-
var mobile: String!
22-
var selectedVerificationCodeViewIndex = 0
23-
24-
lazy var verificationCodeViews: [KWVerificationCodeView] = {
25-
[unowned self] in
26-
27-
return [self.verificationCodeView1, self.verificationCodeView2, self.verificationCodeView3, self.verificationCodeView4]
28-
}()
15+
@IBOutlet weak var verificationCodeView: KWVerificationCodeView!
2916

3017
// MARK: - Lifecycle
3118
override func viewDidLoad() {
3219
super.viewDidLoad()
33-
setupVerificationCodeViews()
3420
}
3521

3622
// MARK: - IBAction
3723
@IBAction func submitButtonTapped(_ sender: UIButton) {
38-
if validateTextFields() {
39-
let alertController = UIAlertController(title: "Success", message: "Code is \(getVerificationCode())", preferredStyle: .alert)
24+
if verificationCodeView.validateCode() {
25+
let alertController = UIAlertController(title: "Success", message: "Code is \(verificationCodeView.getVerificationCode())", preferredStyle: .alert)
4026
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
4127
alertController.addAction(okAction)
4228
self.present(alertController, animated: true, completion: nil)
4329
}
4430
}
45-
46-
// MARK: - FilePrivate Methods
47-
fileprivate func setupVerificationCodeViews() {
48-
for verificationCodeView in verificationCodeViews {
49-
verificationCodeView.delegate = self
50-
}
51-
52-
verificationCodeViews.first?.activate()
53-
}
54-
55-
fileprivate func validateTextFields() -> Bool {
56-
for verificationCodeView in verificationCodeViews {
57-
if Int(verificationCodeView.numberTextField.text!) == nil {
58-
return false
59-
}
60-
}
61-
62-
return true
63-
}
64-
65-
fileprivate func getVerificationCode() -> String {
66-
var verificationCode = ""
67-
for verificationCodeView in verificationCodeViews {
68-
verificationCode += verificationCodeView.numberTextField.text!
69-
}
70-
71-
return verificationCode
72-
}
73-
7431
}
75-
76-
// MARK: - KWVerificationCodeDelegate
77-
extension VerificationCodeViewController: KWVerificationCodeDelegate {
78-
func moveToNext(_ verificationCodeView: KWVerificationCodeView) {
79-
let validIndex = verificationCodeViews.index(of: verificationCodeView) == verificationCodeViews.count - 1 ? verificationCodeViews.index(of: verificationCodeView)! : verificationCodeViews.index(of: verificationCodeView)! + 1
80-
verificationCodeViews[validIndex].activate()
81-
}
82-
83-
func moveToPrevious(_ verificationCodeView: KWVerificationCodeView, oldCode: String) {
84-
if verificationCodeViews.last == verificationCodeView && oldCode != " " {
85-
return
86-
}
87-
88-
let validIndex = verificationCodeViews.index(of: verificationCodeView)! == 0 ? 0 : verificationCodeViews.index(of: verificationCodeView)! - 1
89-
verificationCodeViews[validIndex].activate()
90-
verificationCodeViews[validIndex].reset()
91-
}
92-
}
93-

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)