-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVgsShowReactNativeViewManager.swift
199 lines (162 loc) · 6.46 KB
/
VgsShowReactNativeViewManager.swift
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
import UIKit
import VGSShowSDK
@objc(VgsShowReactNativeViewManager)
class VgsShowReactNativeViewManager: RCTViewManager {
override func view() -> (VgsShowReactNativeView) {
return VgsShowReactNativeView()
}
@objc(revealData:path:method:payload:resolver:rejecter:) func revealData(_ node: NSNumber, path: String, method: String, payload: VGSJSONData, resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock) {
DispatchQueue.main.async {
let component = self.bridge.uiManager.view(
forReactTag: node
) as! VgsShowReactNativeView
component.revealData(path: path, method: method, payload: payload, resolve: resolve, reject: reject)
}
}
}
class VgsShowReactNativeView : UIView, VGSLabelDelegate {
var vgsShow: VGSShow?;
let attributeLabel = VGSLabel()
@objc var initParams: NSDictionary = NSDictionary() {
didSet {
let vaultId = initParams["vaultId"] as! String;
let environment = initParams["environment"] as! String;
let customHeaders = initParams.object(forKey: "customHeaders") != nil ? initParams["customHeaders"] as! Dictionary<String, String> : [:];
if (vaultId != nil && environment != nil) {
if (environment == "live") {
vgsShow = VGSShow(id: vaultId, environment: .live);
vgsShow!.subscribe(attributeLabel)
} else if (environment == "sandbox") {
vgsShow = VGSShow(id: vaultId, environment: .sandbox);
vgsShow!.subscribe(attributeLabel)
}
if (vgsShow != nil) {
vgsShow?.customHeaders = customHeaders;
}
}
}
}
@objc var textColor: String = "" {
didSet {
let color = hexStringToUIColor(hex: textColor);
attributeLabel.textColor = color;
attributeLabel.placeholderStyle.color = color;
}
}
@objc var placeholderColor: String = "" {
didSet {
attributeLabel.placeholderStyle.color = hexStringToUIColor(hex: placeholderColor)
}
}
@objc var bgColor: String = "" {
didSet {
attributeLabel.backgroundColor = hexStringToUIColor(hex: bgColor)
}
}
@objc var borderColor: String = "" {
didSet {
attributeLabel.borderColor = hexStringToUIColor(hex: borderColor)
}
}
@objc var placeholder: String = "" {
didSet {
attributeLabel.placeholder = placeholder;
}
}
@objc var contentPath: String = "" {
didSet {
attributeLabel.contentPath = contentPath;
}
}
@objc var fontFamily: String = "" {
didSet {
let font = UIFont.init(name: fontFamily, size: fontSize);
attributeLabel.font = font;
attributeLabel.placeholderStyle.font = font;
}
}
@objc var fontSize: CGFloat = 16 {
didSet {
var font: UIFont?;
if (fontFamily == "") {
font = UIFont.systemFont(ofSize: fontSize);
} else {
font = UIFont.init(name: fontFamily, size: fontSize);
}
attributeLabel.font = font;
attributeLabel.placeholderStyle.font = font;
}
}
@objc var characterSpacing: CGFloat = 0.83 {
didSet {
attributeLabel.characterSpacing = characterSpacing;
}
}
@objc var borderRadius: CGFloat = 0 {
didSet {
attributeLabel.layer.cornerRadius = borderRadius;
}
}
override init(frame: CGRect) {
super.init(frame: frame)
createVgsShow();
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private func createVgsShow() {
let stackView = UIStackView.init(arrangedSubviews: [attributeLabel])
stackView.axis = .vertical
stackView.distribution = .fill
stackView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leftAnchor.constraint(equalTo: self.leftAnchor),
stackView.rightAnchor.constraint(equalTo: self.rightAnchor),
stackView.topAnchor.constraint(equalTo: self.topAnchor),
stackView.heightAnchor.constraint(equalTo: self.heightAnchor)
])
attributeLabel.delegate = self
}
@objc func revealData(path: String, method: String, payload: VGSJSONData,
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
if (vgsShow == nil) {
return;
}
print("vgsshow revealData, path: \(path), method= \(method), payload = \(payload)")
vgsShow!.request(path: path,
method: method == "post" ? .post : .get,
payload: method == "post" ? payload : nil) { (requestResult) in
switch requestResult {
case .success(let code):
print("vgsshow success, code: \(code)")
resolve(code);
case .failure(let code, let error):
print("vgsshow failed, code: \(code), error: \(error)")
reject("error", error?.localizedDescription, error)
}
}
}
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if (cString.hasPrefix("TRANSPARENT")) {
return UIColor.clear;
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt64 = 0
Scanner(string: cString).scanHexInt64(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}