UnifiedCouponsViewController.swift
20.3 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
// UnifiedCouponsViewController.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 20/10/23.
//
import Foundation
import UIKit
import SwiftEventBus
@objc public class UnifiedCouponsViewController: UIViewController {
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var emptyView: UIView!
@IBOutlet weak var emptyViewHeight: NSLayoutConstraint!
@IBOutlet weak var emptyLabel: UILabel!
@IBOutlet weak var submitButton: UIButton!
public var unifiedCoupons:Array<swiftApi.UnifiedCouponModel> = []
public var smCoupons:Array<swiftApi.CouponItemModel> = []
public var smCouponsSelected:Array<swiftApi.CouponItemModel> = []
// A reference to the Select All image view for later use
var circleImageView: UIImageView!
// Track the selection state
var isSelectAllActive: Bool = false
public override func viewDidLoad() {
super.viewDidLoad()
self.hidesBottomBarWhenPushed = true
SwiftEventBus.onBackgroundThread(self, name: "unified_coupons_fetched") { result in
DispatchQueue.main.async {
self.unifiedCoupons = swiftApi().getUnifiedCouponList()
// self.tableView.reloadData()
// Reload the Unified Coupons section only
self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
if (self.unifiedCoupons.count == 0 && self.smCoupons.count == 0) {
self.emptyView.isHidden = false
self.emptyViewHeight.constant = self.emptyView.intrinsicContentSize.height
} else {
self.emptyView.isHidden = true
self.emptyViewHeight.constant = 0
}
}
}
SwiftEventBus.onBackgroundThread(self, name: "sm_coupons_fetched") { result in
DispatchQueue.main.async {
self.smCoupons = swiftApi().getSMCouponList()
// self.tableView.reloadData()
// Reload the SM Coupons section only
self.tableView.reloadSections(IndexSet(integer: 1), with: .automatic)
if (self.unifiedCoupons.count == 0 && self.smCoupons.count == 0) {
self.emptyView.isHidden = false
self.emptyViewHeight.constant = self.emptyView.intrinsicContentSize.height
} else {
self.emptyView.isHidden = true
self.emptyViewHeight.constant = 0
}
}
}
setBackButton()
setNavigationTitle("SUPERMARKET DEALS")
// Setup the info button with an action
setNavBarRightInfoButton() {
// This closure is the action that will be executed when the info button is tapped
self.presentInfoPopup()
}
// backgroundImage.image = UIImage(named: "coupons_scrollview_dark", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
tableView.delegate = self
tableView.dataSource = self
// tableView.clipsToBounds = true
// tableView.layer.cornerRadius = 30
// tableView.layer.maskedCorners = [ .layerMinXMinYCorner] // Top left corner radius
// tableView.contentInset.top = 30
tableView.contentInset.bottom = 115
emptyLabel.text = "Αυτήν τη στιγμή δεν έχεις κάποιο ενεργό κουπόνι. Στην ενότητα FOR YOU μπορείς να βρεις κουπόνια αποκλειστικά για σένα!"
submitButton.setTitle("Συνδύασε τις προσφορές σε ένα κουπόνι", for: .normal)
submitButton.setTitleColor(.white, for: .normal)
submitButton.layer.cornerRadius = 16.0
// submitButton.backgroundColor = UIColor(red: 0.05, green: 0.65, blue: 0.00, alpha: 1.00)
submitButton.backgroundColor = UIColor(rgb: 0x9D9D9C)
submitButton.isEnabled = false
// submitButton.frame = CGRect(x: 0.0, y: 0.0, width: submitButton.intrinsicContentSize.width, height: 50)
// submitButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25)
// Fix width for ipad
if UIDevice.current.userInterfaceIdiom == .pad {
// iPad
submitButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 18)
// submitButton.widthAnchor.constraint(equalToConstant: 250).isActive = true
} else {
// not iPad (iPhone, mac, tv, carPlay, unspecified)
submitButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 16)
// submitButton.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.6).isActive = true
}
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
swiftApi().logTrackersEvent("screen", "ActiveUnifiedCouponsScreen")
self.unifiedCoupons = swiftApi().getUnifiedCouponList()
self.smCoupons = swiftApi().getSMCouponList()
self.tableView.reloadData()
if (self.unifiedCoupons.count == 0 && self.smCoupons.count == 0) {
self.emptyView.isHidden = false
self.emptyViewHeight.constant = self.emptyView.intrinsicContentSize.height
} else {
self.emptyView.isHidden = true
self.emptyViewHeight.constant = 0
}
self.navigationController?.hideHairline()
}
// MARK: - Functions
// Button action for "select all"
@objc func selectAllTapped() {
// Toggle the selection state
isSelectAllActive.toggle()
// Update the image based on the new state
if (isSelectAllActive) {
circleImageView.image = UIImage(named: "circle_checked", in: MyEmptyClass.resourceBundle(), compatibleWith: nil) // Selected image
smCouponsSelected = smCoupons
} else {
circleImageView.image = UIImage(named: "circle_unchecked", in: MyEmptyClass.resourceBundle(), compatibleWith: nil) // Unselected image
smCouponsSelected = []
}
handleSubmitButtonUI()
// Reload the SM Coupons section only
tableView.reloadSections(IndexSet(integer: 1), with: .automatic)
}
func handleSelectCouponAction(indexPath: IndexPath) {
let currentCoupon = self.smCoupons[indexPath.row]
let isCouponSelected = self.smCouponsSelected.contains { $0.coupon == currentCoupon.coupon }
if (isCouponSelected) {
self.smCouponsSelected = self.smCouponsSelected.filter({ return $0.coupon != currentCoupon.coupon })
if (isSelectAllActive) {
isSelectAllActive = false
// Reload the SM Coupons section only
tableView.reloadSections(IndexSet(integer: 1), with: .automatic)
} else {
tableView.reloadRows(at: [indexPath], with: .automatic) // Reload the specific row
}
} else {
self.smCouponsSelected.append(currentCoupon)
if (self.smCouponsSelected.count == self.smCoupons.count) {
isSelectAllActive = true
// Reload the SM Coupons section only
tableView.reloadSections(IndexSet(integer: 1), with: .automatic)
} else {
tableView.reloadRows(at: [indexPath], with: .automatic) // Reload the specific row
}
}
handleSubmitButtonUI()
}
private func presentInfoPopup() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
if let popupVC = storyboard.instantiateViewController(withIdentifier: "PopupInfoViewController") as? SwiftWarplyFramework.PopupInfoViewController {
popupVC.modalPresentationStyle = .overCurrentContext
popupVC.modalTransitionStyle = .crossDissolve
self.present(popupVC, animated: true, completion: nil)
}
}
func handleSubmitButtonUI() {
if (self.smCouponsSelected.count > 0) {
submitButton.backgroundColor = UIColor(rgb: 0x0EA600)
submitButton.isEnabled = true
} else {
submitButton.backgroundColor = UIColor(rgb: 0x9D9D9C)
submitButton.isEnabled = false
}
}
func showSuccessDialog() -> Void {
let alert = UIAlertController(title: "Δημιουργία Κουπονιού", message: "Το ενιαίο κουπόνι σου δημιουργήθηκε επιτυχώς.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
// self.navigationController?.popViewController(animated: true)
// self.dismiss(animated: true, completion: {})
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
}
func showFailureDialog() -> Void {
let alert = UIAlertController(title: "Αποτυχία Δημιουργίας Κουπονιού", message: "Το ενιαίο κουπόνι σου δεν δημιουργήθηκε. Προσπάθησε ξανά.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
}
// MARK: - Actions
@IBAction func submitButtonAction(_ sender: Any) {
// let smCouponsString = self.smCouponsSelected.map { $0.coupon ?? "" }.joined(separator: ",")
// swiftApi().logTrackersEvent("click", ("UnifySMCoupons:" + (smCouponsString)))
let smCouponCodes = self.smCouponsSelected.map { $0.coupon ?? "" }
swiftApi().createUnifiedCouponAsync(couponCodes: smCouponCodes) { responseData in
if (responseData != nil) {
DispatchQueue.main.async {
if (responseData?.getStatus == 1) {
self.smCouponsSelected = []
self.circleImageView.image = UIImage(named: "circle_unchecked", in: MyEmptyClass.resourceBundle(), compatibleWith: nil) // Unselected image
self.handleSubmitButtonUI()
// Reload the SM Coupons section only
self.tableView.reloadSections(IndexSet(integer: 1), with: .automatic)
self.showSuccessDialog()
}
}
}
} failureCallback: { errorCode in
self.showFailureDialog()
}
}
}
// MARK: - TableView
extension UnifiedCouponsViewController: UITableViewDelegate, UITableViewDataSource{
public func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return self.unifiedCoupons.count
} else if (section == 1) {
return self.smCoupons.count
} else {
return 0
}
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.section == 0) {
return 130.0 + 8.0
// return UITableViewAutomaticDimension
} else if (indexPath.section == 1) {
return 130.0 + 8.0
} else {
return 0.0
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.section == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "UnifiedCouponsTableViewCellId", for: indexPath) as! UnifiedCouponsTableViewCell
cell.configureCell(coupon: unifiedCoupons[indexPath.row])
return cell
} else {
let currentCoupon = self.smCoupons[indexPath.row]
let isCouponSelected = self.smCouponsSelected.contains { $0.coupon == currentCoupon.coupon }
let cell = tableView.dequeueReusableCell(withIdentifier: "CouponsTableViewCellId", for: indexPath) as! CouponsTableViewCell
cell.configureCell(coupon: smCoupons[indexPath.row])
cell.showSelectButton(isSelectViewVisible: true, isSelected: isCouponSelected) {
self.handleSelectCouponAction(indexPath: indexPath)
}
return cell
}
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath.section == 0) {
let couponBarcode = unifiedCoupons[indexPath.row]._barcode
swiftApi().logTrackersEvent("click", ("UnifiedCoupon:" + couponBarcode))
let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
let vc = storyboard.instantiateViewController(withIdentifier: "UnifiedCouponBarcodeViewController") as! SwiftWarplyFramework.UnifiedCouponBarcodeViewController
vc.coupon = unifiedCoupons[indexPath.row]
vc.isFromWallet = true
self.navigationController?.pushViewController(vc, animated: true)
} else if (indexPath.section == 1) {
let couponSetData: swiftApi.CouponSetItemModel? = smCoupons[indexPath.row].couponset_data
let couponName = couponSetData?.name ?? ""
swiftApi().logTrackersEvent("click", ("Coupon:" + couponName))
let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
let vc = storyboard.instantiateViewController(withIdentifier: "CouponBarcodeViewController") as! SwiftWarplyFramework.CouponBarcodeViewController
vc.coupon = smCoupons[indexPath.row]
vc.isFromWallet = true
vc.isSMCoupon = true
self.navigationController?.pushViewController(vc, animated: true)
} else {
// Do nothing
}
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if (section == 0) {
if (self.unifiedCoupons.count > 0) {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 70))
view.backgroundColor = .clear
let titleLabel = UILabel(frame: CGRect(x: 16, y: 25, width: view.frame.width - 32, height: 25))
titleLabel.font = UIFont(name: "BTCosmo-Bold", size: 17)
// titleLabel.textColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)
titleLabel.textColor = UIColor(rgb: 0x212121)
titleLabel.text = "Ενιαία κουπόνια"
view.addSubview(titleLabel)
return view
} else {
return nil
}
} else if (section == 1) {
if (self.smCoupons.count > 0) {
let headerView = UIView()
headerView.backgroundColor = .clear
// Create the title label (Κουπόνια)
let titleLabel = UILabel()
titleLabel.text = "Κουπόνια"
titleLabel.font = UIFont(name: "BTCosmo-Bold", size: 17)
// Create the select-all label (Επιλογή όλων)
let selectAllLabel = UILabel()
selectAllLabel.text = "Επιλογή όλων"
selectAllLabel.font = UIFont(name: "PeridotPE-SBold", size: 17)
// Create a UIImageView for the circle image
circleImageView = UIImageView()
// let circleImage = UIImage(named: "circle_unchecked", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
// Update the image based on the new state
if (isSelectAllActive) {
circleImageView.image = UIImage(named: "circle_checked", in: MyEmptyClass.resourceBundle(), compatibleWith: nil) // Selected image
} else {
circleImageView.image = UIImage(named: "circle_unchecked", in: MyEmptyClass.resourceBundle(), compatibleWith: nil) // Unselected image
}
// circleImageView.image = circleImage
circleImageView.contentMode = .scaleAspectFit
circleImageView.translatesAutoresizingMaskIntoConstraints = false
// Set size for the imageView
circleImageView.widthAnchor.constraint(equalToConstant: 30).isActive = true
circleImageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
// Create a transparent UIButton on top of the image
let selectAllButton = UIButton(type: .custom)
selectAllButton.backgroundColor = .clear // Keep it transparent
selectAllButton.addTarget(self, action: #selector(selectAllTapped), for: .touchUpInside)
selectAllButton.translatesAutoresizingMaskIntoConstraints = false
// Set the button size to match the image size
selectAllButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
selectAllButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
// Create a horizontal stack view to align the title, label, and image
let mainStackView = UIStackView(arrangedSubviews: [titleLabel, selectAllLabel, circleImageView])
mainStackView.axis = .horizontal
mainStackView.alignment = .center // Centers them vertically
mainStackView.spacing = 10
// Add the stack view to the header view
headerView.addSubview(mainStackView)
headerView.addSubview(selectAllButton) // Add the button separately on top of the image view
// Set constraints for stack view
mainStackView.translatesAutoresizingMaskIntoConstraints = false
mainStackView.leadingAnchor.constraint(equalTo: headerView.leadingAnchor, constant: 16).isActive = true
mainStackView.trailingAnchor.constraint(equalTo: headerView.trailingAnchor, constant: -16).isActive = true
mainStackView.topAnchor.constraint(equalTo: headerView.topAnchor, constant: 8).isActive = true
mainStackView.bottomAnchor.constraint(equalTo: headerView.bottomAnchor, constant: -8).isActive = true
// Set button position on top of the imageView
selectAllButton.centerXAnchor.constraint(equalTo: circleImageView.centerXAnchor).isActive = true
selectAllButton.centerYAnchor.constraint(equalTo: circleImageView.centerYAnchor).isActive = true
return headerView
} else {
return nil
}
} else {
return nil
}
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (section == 0) {
if (self.unifiedCoupons.count > 0) {
return 70.0
} else {
return 0.0
}
} else if (section == 1) {
if (self.smCoupons.count > 0) {
return 70.0
} else {
return 0.0
}
} else {
return 0.0
}
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// return CGFloat.leastNormalMagnitude
return 0.0
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
}