Repository navigation
Expand file tree
/
Copy pathViewController.swift
More file actions
133 lines (114 loc) · 3.98 KB
/
Copy pathViewController.swift
File metadata and controls
133 lines (114 loc) · 3.98 KB
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
//
// ViewController.swift
// HCaptcha
//
// Created by Flávio Caetano on 03/22/17.
// Copyright © 2018 HCaptcha. All rights reserved.
//
import HCaptcha
import HCaptcha_RxSwift
import RxCocoa
import RxSwift
import UIKit
import WebKit
class ViewController: BaseViewController {
var disposeBag = DisposeBag()
@IBAction private func didPressVerifyButton(button: UIButton) {
disposeBag = DisposeBag()
setupLoadingObservable()
let validate = createValidateObservable()
setupUIBindings(validate: validate)
setupResetButton()
}
private func setupLoadingObservable() {
hcaptcha.rx.didFinishLoading
.debug("did finish loading")
.subscribe()
.disposed(by: disposeBag)
}
private func createValidateObservable() -> Observable<String> {
if let phoneText = phoneTextField.text, !phoneText.isEmpty {
return createPhoneValidateObservable(phoneText: phoneText)
} else {
return createRegularValidateObservable()
}
}
private func createPhoneValidateObservable(phoneText: String) -> Observable<String> {
let verifyParams: HCaptchaVerifyParams
if phoneModeSwitch.isOn {
// Phone number mode
verifyParams = HCaptchaVerifyParams(phoneNumber: phoneText, rqdata: "some_rq")
} else {
// Phone prefix mode
verifyParams = HCaptchaVerifyParams(phonePrefix: phoneText)
}
return hcaptcha.rx.validate(on: view, verifyParams: verifyParams)
.catch { error in
return .just("Error \(error)")
}
.debug("validate with phone params")
.share()
}
private func createRegularValidateObservable() -> Observable<String> {
return hcaptcha.rx.validate(on: view, resetOnError: false)
.catch { error in
return .just("Error \(error)")
}
.debug("validate")
.share()
}
private func setupUIBindings(validate: Observable<String>) {
let isLoading = validate
.map { _ in false }
.startWith(true)
.share(replay: 1)
isLoading
.bind(to: spinner.rx.isAnimating)
.disposed(by: disposeBag)
let isEnabled = isLoading
.map { !$0 }
.catchAndReturn(false)
.share(replay: 1)
isEnabled
.bind(to: validateButton.rx.isEnabled)
.disposed(by: disposeBag)
validate
.map { [weak self] _ in
self?.view.viewWithTag(Constants.webViewTag)
}
.subscribe(onNext: { subview in
subview?.removeFromSuperview()
})
.disposed(by: disposeBag)
validate
.bind(to: label.rx.text)
.disposed(by: disposeBag)
}
private func setupResetButton() {
resetButton.rx.tap
.subscribe(onNext: { [weak hcaptcha] _ in
hcaptcha?.reset()
})
.disposed(by: disposeBag)
}
override func setupHCaptcha() {
// swiftlint:disable:next force_try
hcaptcha = try! HCaptcha(
apiKey: "00000000-0000-0000-0000-000000000000",
locale: locale,
diagnosticLog: true,
userJourney: journeySwitch.isOn
)
hcaptcha.configureWebView { [weak self] webview in
webview.frame = self?.view.bounds ?? CGRect.zero
webview.tag = Constants.webViewTag
// For testing purposes
// If the webview requires presentation, this should work as a way of detecting the webview in UI tests
self?.view.viewWithTag(Constants.testLabelTag)?.removeFromSuperview()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
label.tag = Constants.testLabelTag
label.accessibilityLabel = "webview"
self?.view.addSubview(label)
}
}
}