PopupMerchantsViewController.swift
8.03 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
//
// PopupSMMerchantsViewController.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 17/10/24.
//
import UIKit
class PopupMerchantsViewController: UIViewController {
@IBOutlet weak var popupView: UIView!
@IBOutlet weak var headerLabel: UILabel!
@IBOutlet weak var closeButton: UIButton!
@IBOutlet weak var tableView: DynamicSizeMerchantsTableView!
public var availableShopsList:Array<swiftApi.ShopAvailabilityItemModel> = []
// public var coupon: swiftApi.CouponItemModel?
public var headerText: String? = "Βρες το προϊον στα supermarket"
public var buttonTitle: String? = "Βρες το"
public var buttonIcon: String?
public var footerText: String? = "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία."
public var showEshops: Bool? = false
override func viewDidLoad() {
super.viewDidLoad()
if (showEshops == true) {
setupEshopsData()
}
tableView.delegate = self
tableView.dataSource = self
tableView.contentInset.top = 40
popupView.clipsToBounds = true
popupView.layer.cornerRadius = 16
popupView.layer.maskedCorners = [ .layerMinXMinYCorner, .layerMaxXMinYCorner] // Top left, right corner radius
closeButton.setImage(UIImage(named: "ic_close_3.png", in: MyEmptyClass.resourceBundle(), compatibleWith: nil), for: .normal)
closeButton.imageView?.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
// self.tableView.reloadData()
self.tableView.invalidateIntrinsicContentSize()
self.headerLabel.text = self.headerText
headerLabel.font = UIFont(name: "BTCosmo-Bold", size: 18)
headerLabel.textColor = UIColor(rgb: 0x000000)
// setupFooterView()
}
// MARK: - Functions
func setupEshopsData() {
let tempShop1: swiftApi.ShopAvailabilityItemModel = swiftApi.ShopAvailabilityItemModel()
tempShop1._merchant_uuid = "ab"
tempShop1._product_url = "https://www.ab.gr"
availableShopsList.append(tempShop1)
let tempShop2: swiftApi.ShopAvailabilityItemModel = swiftApi.ShopAvailabilityItemModel()
tempShop2._merchant_uuid = "masoutis"
tempShop2._product_url = "https://www.masoutis.gr"
availableShopsList.append(tempShop2)
}
// private func setupFooterView() {
// // Create a footer view container
// let footerView = UIView()
// footerView.backgroundColor = .clear
//
// // Create a label for the footer
// let footerLabel = UILabel()
// footerLabel.text = "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία."
// footerLabel.textAlignment = .center
// footerLabel.font = UIFont(name: "PeridotPE-Regular", size: 13)
// footerLabel.textColor = UIColor(rgb: 0x000000)
// footerLabel.numberOfLines = 0 // Allow for multiline text
//
// // Add label to the footer view
// footerView.addSubview(footerLabel)
// footerLabel.translatesAutoresizingMaskIntoConstraints = false
//
// // Set constraints for the label inside the footer view
// NSLayoutConstraint.activate([
// footerLabel.leadingAnchor.constraint(equalTo: footerView.leadingAnchor, constant: 16),
// footerLabel.trailingAnchor.constraint(equalTo: footerView.trailingAnchor, constant: -16),
// footerLabel.topAnchor.constraint(equalTo: footerView.topAnchor, constant: 10),
// footerLabel.bottomAnchor.constraint(equalTo: footerView.bottomAnchor, constant: -10)
// ])
//
// // Set the footer view's frame size to fit the content
// footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
//
// // Assign the footer view to the table view's footer
// tableView.tableFooterView = footerView
// }
// MARK: - UIButton Actions
@IBAction func closeButtonAction(_ sender: Any) {
self.dismiss(animated: true, completion: {})
}
}
// MARK: - TableView
extension PopupMerchantsViewController: 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.availableShopsList.count
} else if (section == 1) {
return 1
} else {
return 0
}
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.section == 0) {
return 56.0 + 32.0
// return UITableViewAutomaticDimension
} else if (indexPath.section == 1) {
return 50.0
} else {
return 0.0
}
// return UITableViewAutomaticDimension
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.section == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "MerchantTableViewCellId", for: indexPath) as! MerchantTableViewCell
cell.configureCell(shop: availableShopsList[indexPath.row], buttonTitle: self.buttonTitle ?? "Βρες το", iconImage: self.buttonIcon, showEshops: self.showEshops)
// Set the delegate to self
cell.delegate = self
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PopupMerchantsFooterTableViewCellId", for: indexPath) as! PopupMerchantsFooterTableViewCell
cell.configureCell(title: self.footerText ?? "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία.")
return cell
}
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Do nothing - tap handled with protocol
// print(" didSelectRowAt Button tapped in row \(indexPath.row)")
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
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
}
}
// MARK: - MerchantTableViewCellDelegate
extension PopupMerchantsViewController: MerchantTableViewCellDelegate {
func didTapButton(in cell: MerchantTableViewCell) {
// Find the index path of the cell where the button was tapped
if let indexPath = tableView.indexPath(for: cell) {
let urlString = availableShopsList[indexPath.row]._product_url
if let url = URL(string: urlString){
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
}
}
// Class for dynamic height table view
@objc public class DynamicSizeMerchantsTableView: UITableView
{
override public func layoutSubviews() {
super.layoutSubviews()
if bounds.size != intrinsicContentSize {
invalidateIntrinsicContentSize()
}
}
// override public var intrinsicContentSize: CGSize {
// return contentSize
// }
override public var intrinsicContentSize: CGSize {
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height + contentInset.top + contentInset.bottom + 30)
}
}