MyRewardsOffersScrollTableViewCell.swift
9.07 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
//
// MyRewardsOffersScrollTableViewCell.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 23/5/25.
//
import UIKit
protocol MyRewardsOffersScrollTableViewCellDelegate: AnyObject {
func didSelectOffer(_ offer: OfferModel)
}
@objc(MyRewardsOffersScrollTableViewCell)
public class MyRewardsOffersScrollTableViewCell: UITableViewCell {
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var allButtonView: UIView!
@IBOutlet weak var allButtonLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var collectionViewBottom: NSLayoutConstraint!
weak var delegate: MyRewardsOffersScrollTableViewCellDelegate?
var data: SectionModel?
public override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
allButtonView.layer.borderWidth = 1.0
allButtonView.layer.borderColor = UIColor(rgb: 0x000F1E).cgColor
allButtonView.layer.cornerRadius = 4.0
allButtonLabel.text = "Όλα"
allButtonLabel.font = UIFont(name: "PingLCG-Regular", size: 16)
allButtonLabel.textColor = UIColor(rgb: 0x00111B)
allButtonLabel.frame.size.width = allButtonLabel.intrinsicContentSize.width
allButtonLabel.frame.size.height = allButtonLabel.intrinsicContentSize.height
// UPDATED: Safe XIB registration for collection view cells
registerCollectionViewCells()
// Configure UI
setupUI()
// Configure collection view layout
setupCollectionViewLayout()
// Set delegates
collectionView.delegate = self
collectionView.dataSource = self
}
// NEW: Safe collection view cell registration
private func registerCollectionViewCells() {
XIBLoader.registerCollectionViewCell(
collectionView,
cellClass: UICollectionViewCell.self,
nibName: "MyRewardsOfferCollectionViewCell",
identifier: "MyRewardsOfferCollectionViewCell"
)
}
// NEW: Separate UI configuration
private func setupUI() {
// Fix background colors
collectionView.backgroundColor = UIColor.clear
self.backgroundColor = UIColor.clear
self.contentView.backgroundColor = UIColor.clear
// Remove content insets and gaps
collectionView.contentInset = UIEdgeInsets.zero
collectionView.scrollIndicatorInsets = UIEdgeInsets.zero
if #available(iOS 11.0, *) {
collectionView.contentInsetAdjustmentBehavior = .never
}
collectionView.showsHorizontalScrollIndicator = false
}
// NEW: Separate layout configuration
private func setupCollectionViewLayout() {
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 7
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
}
}
public override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func configureCell(data: SectionModel?, isInProfile: Bool? = false) {
self.data = data
if (isInProfile ?? false) {
collectionViewBottom.constant = 40
} else {
collectionViewBottom.constant = 0
}
// Update collection view height based on section
if data?.title == "Αγαπημένα" {
collectionViewHeightConstraint.constant = 350 // Match cell height
} else {
collectionViewHeightConstraint.constant = 232 // Default height
}
// Configure layout based on section type
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
if data?.title == "Αγαπημένα" {
layout.minimumLineSpacing = 24
} else {
layout.minimumLineSpacing = 7
}
}
let catBoldText = (data?.title ?? "") + " "
let catRegText = "(" + String(data?.count ?? 0) + ")"
let attrBold = [NSAttributedString.Key.font : UIFont(name: "PingLCG-Bold", size: 18) ?? UIFont.boldSystemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor(rgb: 0x000F1E)]
let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor(rgb: 0x000F1E)]
let catAttributedString = NSMutableAttributedString(string:catBoldText, attributes:attrBold)
let catRegString = NSMutableAttributedString(string: catRegText, attributes:attrRegular)
catAttributedString.append(catRegString)
categoryLabel.attributedText = catAttributedString
self.collectionView.reloadData();
}
}
extension MyRewardsOffersScrollTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.data?.itemCount ?? 0
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyRewardsOfferCollectionViewCell", for: indexPath) as! MyRewardsOfferCollectionViewCell
// Handle only CouponSetItemModel and OfferModel - no campaigns
guard let data = self.data,
let items = data.items,
indexPath.row < items.count else {
return cell
}
switch data.itemType {
case .couponSets:
if let couponSet = items[indexPath.row] as? CouponSetItemModel {
cell.configureCell(data: couponSet)
}
default:
// Handle OfferModel for ProfileViewController compatibility
if let offer = items[indexPath.row] as? OfferModel {
cell.configureCell(data: offer)
}
}
return cell;
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Handle only CouponSetItemModel and OfferModel - no campaigns
guard let data = self.data,
let items = data.items,
indexPath.row < items.count else {
return
}
switch data.itemType {
case .couponSets:
if let couponSet = items[indexPath.row] as? CouponSetItemModel {
// Convert CouponSetItemModel to OfferModel for delegate compatibility
let offer = OfferModel(
category: "",
title: couponSet.name ?? "",
description: couponSet.short_description ?? "",
discount: couponSet.discount ?? "",
discountType: couponSet.discount_type ?? "",
bannerImage: couponSet.img_preview ?? "",
merchantLogo: couponSet.img?.first ?? "",
expirationDate: couponSet.expiration ?? "",
color: 0x000000,
isFavorite: false
)
delegate?.didSelectOffer(offer)
}
default:
// Handle OfferModel directly for ProfileViewController compatibility
if let offer = items[indexPath.row] as? OfferModel {
delegate?.didSelectOffer(offer)
}
}
}
// MARK: - UICollectionViewDelegateFlowLayout
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if self.data?.title == "Αγαπημένα" {
return CGSize(width: 275, height: 350)
} else {
return CGSize(width: 257, height: 232)
}
}
// Distance Between Item Cells
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if self.data?.title == "Αγαπημένα" {
return 24
} else {
return 7
}
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
// Cell Margin
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
}
}