-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserAccountInfo.swift
253 lines (230 loc) · 10.6 KB
/
UserAccountInfo.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// UserAccountInfo.swift
// Allergy Without StoryBoard
//
// Created by Owen Hu on 2/12/23.
//
import UIKit
class UserAccountInfo : UIViewController{
lazy var dict = UserDefaults.standard.dictionary(forKey: "UserDB")
var arr:[String] = []
var user = User(email: "", name: "", password: "", race: Race.Asian)
let scrollView = UIScrollView()
func setup(){
// if let bundleID = Bundle.main.bundleIdentifier {
// UserDefaults.standard.removePersistentDomain(forName: bundleID)
// }
// create a vertical stack view to hold the rows of buttons
if dict?.count != nil{
let verticalStackView = UIStackView()
verticalStackView.axis = .vertical
// we're going to use auto-layout
scrollView.translatesAutoresizingMaskIntoConstraints = false
verticalStackView.translatesAutoresizingMaskIntoConstraints = false
// add Scrollview to view
self.view.addSubview(scrollView)
// add stack view to scrollView
scrollView.addSubview(verticalStackView)
arr = Array(dict!.keys)
for (index, element) in arr.enumerated(){
// add row
let row = UIStackView()
// add it to the vertical stack view
verticalStackView.addArrangedSubview(row)
verticalStackView.setCustomSpacing(15, after: row)
let lb = UILabel()
lb.font = UIFont.boldSystemFont(ofSize: 20)
// lb.backgroundColor = UIColor.systemGray
// lb.textColor = UIColor.black
lb.textAlignment = .left
lb.text = element
row.addArrangedSubview(lb)
row.setCustomSpacing(5, after: lb)
NSLayoutConstraint.activate([
lb.widthAnchor.constraint(equalToConstant: 150.0),
lb.heightAnchor.constraint(equalToConstant: 30.0),
])
let bt1 = UIButton()
bt1.setTitle("Edit", for: .normal)
bt1.layer.cornerRadius = 15
bt1.backgroundColor = .systemGreen
bt1.tag = index
bt1.addTarget(self, action: #selector(handle_C), for: .touchUpInside)
row.addArrangedSubview(bt1)
row.setCustomSpacing(5, after: bt1)
NSLayoutConstraint.activate([
bt1.widthAnchor.constraint(equalToConstant: 80.0),
bt1.heightAnchor.constraint(equalToConstant: 30.0),
])
let bt2 = UIButton()
bt2.setTitle("X", for: .normal)
bt2.layer.cornerRadius = 15
bt2.backgroundColor = .systemRed
bt2.tag = index
bt2.addTarget(self, action: #selector(del_user), for: .touchUpInside)
row.addArrangedSubview(bt2)
row.setCustomSpacing(5, after: bt2)
NSLayoutConstraint.activate([
bt2.widthAnchor.constraint(equalToConstant: 30.0),
bt2.heightAnchor.constraint(equalToConstant: 30.0),
])
}
// finally, let's set our constraints
// respect safe-area
let safeG = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain scrollView
// 10-pts from bottom of label
// Leading and Trailing to safe-area with 10-pts "padding"
// Bottom to safe-area with 50-pts "padding"
scrollView.topAnchor.constraint(equalTo: safeG.topAnchor, constant: 50.0),
scrollView.leadingAnchor.constraint(equalTo: safeG.leadingAnchor, constant: 5.0),
scrollView.trailingAnchor.constraint(equalTo: safeG.trailingAnchor, constant: -5.0),
scrollView.bottomAnchor.constraint(equalTo: safeG.bottomAnchor, constant: -75.0),
// constrain vertical stack view to scrollView Content Layout Guide
// 8-pts all around (so we have a little "padding")
verticalStackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 5.0),
verticalStackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 5.0),
verticalStackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -5.0),
verticalStackView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -5.0),
])
} else {
return
}
}
override func viewDidLoad(){
super.viewDidLoad()
view.backgroundColor = .systemBackground
setup()
view.addSubview(AddUser)
view.addSubview(UserAccount)
AddUser.translatesAutoresizingMaskIntoConstraints = false
UserAccount.translatesAutoresizingMaskIntoConstraints = false
let safeG = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
UserAccount.topAnchor.constraint(equalTo: safeG.topAnchor,constant:5.0),
UserAccount.widthAnchor.constraint(equalTo: safeG.widthAnchor,multiplier: 0.75),
UserAccount.heightAnchor.constraint(equalToConstant: 30.0),
AddUser.topAnchor.constraint(equalTo: safeG.topAnchor,constant:5.0),
AddUser.leftAnchor.constraint(equalTo: UserAccount.rightAnchor,constant: 10.0),
AddUser.widthAnchor.constraint(equalTo: safeG.widthAnchor,multiplier: 0.2),
AddUser.heightAnchor.constraint(equalToConstant: 30.0),
])
// start2()
navi()
}
lazy var AddUser : UIButton = {
let AU = UIButton()
AU.setTitle("Add User", for: .normal)
AU.backgroundColor = UIColor.systemGreen
AU.layer.cornerRadius = 10
AU.tag = 999
AU.addTarget(self, action: #selector(handle_C), for : .touchUpInside)
return AU
}()
private func start2(){
AddUser.frame = CGRect(x : 300, y: 60, width: 80, height: 30 )
view.addSubview(AddUser)
UserAccount.frame = CGRect(x : 0, y: 60, width: 300, height: 30 )
view.addSubview(UserAccount)
}
private let UserAccount: UILabel = {
let userAccount = UILabel()
// userAccount.numberOfLines = 0
userAccount.textAlignment = .center
// userAccount.textColor = UIColor.black
userAccount.font = UIFont.boldSystemFont(ofSize: 25)
userAccount.text = "User Accounts"
// userAccount.backgroundColor = UIColor.systemYellow
return userAccount
}()
@objc func handle_C(sender:UIButton){
//pushing the current VC to another T(x) ---> X
//step one : instance or object declaration
let index = sender.tag
if(index == 999){
let vc = RegistrationVC()
vc.user = self.user
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated : true)
}else{
let vc = RegistrationVC()
user.name = arr[index]
vc.user = self.user
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated : true)
}
}
@objc func del_user(sender:UIButton){
//pushing the current VC to another T(x) ---> X
//step one : instance or object declaration
let key = arr[sender.tag]
dict?.removeValue(forKey: key)
UserDefaults.standard.setValue(dict, forKey: "UserDB")
let vc = UserAccountInfo()
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated : true)
}
let user_bt : UIButton = {
let bt = UIButton()
bt.setImage(UIImage(systemName: "person"), for: .normal)
bt.addTarget(self, action: #selector(User_Account), for: .touchUpInside)
return bt
}()
let sb_bt : UIButton = {
let bt = UIButton()
bt.setImage(UIImage(systemName: "barcode"), for: .normal)
bt.addTarget(self, action: #selector(Scan_Barcode), for: .touchUpInside)
return bt
}()
let sl_bt : UIButton = {
let bt = UIButton()
bt.setImage(UIImage(systemName: "camera"), for: .normal)
bt.addTarget(self, action: #selector(Scan_Label), for: .touchUpInside)
return bt
}()
private func navi(){
user_bt.translatesAutoresizingMaskIntoConstraints = false
sb_bt.translatesAutoresizingMaskIntoConstraints = false
sl_bt.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(user_bt)
view.addSubview(sb_bt)
view.addSubview(sl_bt)
let safeG = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
user_bt.bottomAnchor.constraint(equalTo: safeG.bottomAnchor,constant: -20.0),
user_bt.leftAnchor.constraint(equalTo: safeG.leftAnchor, constant: 20.0),
user_bt.widthAnchor.constraint(equalToConstant: 20.0),
user_bt.heightAnchor.constraint(equalToConstant: 20.0),
sb_bt.bottomAnchor.constraint(equalTo: safeG.bottomAnchor,constant: -20.0),
sb_bt.centerXAnchor.constraint(equalTo: safeG.centerXAnchor,constant: 0.0),
sb_bt.widthAnchor.constraint(equalToConstant: 20.0),
sb_bt.heightAnchor.constraint(equalToConstant: 20.0),
sl_bt.bottomAnchor.constraint(equalTo: safeG.bottomAnchor,constant: -20.0),
sl_bt.rightAnchor.constraint(equalTo: safeG.rightAnchor, constant: -20.0),
sl_bt.widthAnchor.constraint(equalToConstant: 20.0),
sl_bt.heightAnchor.constraint(equalToConstant: 20.0),
])
}
@objc func User_Account(sender : UIButton){
//pushing the current VC to another T(x) ---> X
//step one : instance or object declaration
let vc = UserAccountInfo()
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated : true)
}
@objc func Scan_Label(sender : UIButton){
//pushing the current VC to another T(x) ---> X
//step one : instance or object declaration
let vc = ScanLabel()
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated : true)
}
@objc func Scan_Barcode(sender : UIButton){
//pushing the current VC to another T(x) ---> X
//step one : instance or object declaration
let vc = ScanBarcode()
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated : true)
}
}