ActiveCodeTableViewCell.swift
8.55 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
//
// ActiveCodeTableViewCell.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 3/4/23.
//
import UIKit
@objc public class ActiveCodeTableViewCell: UITableViewCell {
@IBOutlet weak var activeCodeView: UIView!
@IBOutlet weak var activeCodeViewHeight: NSLayoutConstraint!
@IBOutlet weak var activeCodesCountLabel: UILabel!
@IBOutlet weak var activeCodeLabel: UILabel!
@IBOutlet weak var activeCodeExpirationLabel: UILabel!
@IBOutlet weak var activeCodeImage: UIImageView!
@IBOutlet weak var activeCodeScrollView: UIScrollView!
@IBOutlet weak var activeCodeScrollViewHeight: NSLayoutConstraint!
@IBOutlet weak var activeCodeContentView: UIView!
@IBOutlet weak var activeCodeContentViewHeight: NSLayoutConstraint!
public var dfyCoupons:Array<swiftApi.ActiveDFYCouponModel> = swiftApi().getActiveDFYCoupons()
public override func awakeFromNib() {
super.awakeFromNib()
// TODO: DELETE ===>
// let coupon = swiftApi.ActiveDFYCouponModel()
// coupon._value = "12"
// // coupon._date = "2022-12-05 01:55:01"
// coupon._date = "2022-10-26 23:59:01"
// coupon._code = "123456789"
// let coupon2 = swiftApi.ActiveDFYCouponModel()
// coupon2._value = "23"
// coupon2._date = "2022-11-05 01:55"
// coupon2._code = "234567891"
// let coupon3 = swiftApi.ActiveDFYCouponModel()
// coupon3._value = "34"
// coupon3._date = "2022-07-01 01:55"
// coupon3._code = "345678912"
// let couponsArray: Array<swiftApi.ActiveDFYCouponModel> = [coupon, coupon2, coupon3, coupon3, coupon3]
// swiftApi().setActiveDFYCoupons(dfyCoupons: couponsArray)
// dfyCoupons = swiftApi().getActiveDFYCoupons()
// TODO: DELETE <===
// Add shadow
self.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 1.0
activeCodeView.layer.cornerRadius = 16.5
// activeCodeView.layer.borderWidth = 1
// activeCodeView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
activeCodeImage.image = UIImage(named: "active_code_logo_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
if (dfyCoupons.count > 0) {
if (dfyCoupons.count == 1) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
// sort dfyCoupons by date
dfyCoupons.sort(by: {
let date1 = dateFormatter.date(from: $0._date)
let date2 = dateFormatter.date(from: $1._date)
if ((date1 != nil) && (date2 != nil)) {
return date1!.compare(date2!) == .orderedAscending
} else {
return false
}
})
// Get days from now of the most recet coupon
var daysFromNow = ""
let calendar = Calendar.current
// Replace the hour (time) of both dates with 00:00
let date1 = calendar.startOfDay(for: Date())
if let date2 = dateFormatter.date(from: dfyCoupons[0]._date) {
let components = calendar.dateComponents([.day], from: date1, to: date2)
daysFromNow = (components.day) != nil ? String((components.day ?? 0) + 1) : ""
}
activeCodesCountLabel.text = "Ενεργός κωδικός:"
// activeCodeLabel.text = dfyCoupons[0]._code
let newLabel = CopyableLabel()
newLabel.text = String(dfyCoupons[0]._code)
newLabel.font = UIFont(name: "PeridotPE-Bold", size: 18)
newLabel.textColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)
newLabel.frame.size.width = newLabel.intrinsicContentSize.width
newLabel.frame.size.height = newLabel.intrinsicContentSize.height // tagHeight
activeCodeContentView.addSubview(newLabel)
// set the btn frame origin
newLabel.frame.origin.x = 0
newLabel.frame.origin.y = 0
let scrollHeight = newLabel.intrinsicContentSize.height
activeCodeContentViewHeight.constant = scrollHeight
activeCodeScrollViewHeight.constant = scrollHeight
activeCodeExpirationLabel.isHidden = false
if (daysFromNow == "1") {
activeCodeExpirationLabel.text = "Λήγει σε " + daysFromNow + " ημέρα"
} else {
activeCodeExpirationLabel.text = "Λήγει σε " + daysFromNow + " ημέρες"
}
} else {
var tagHeight:CGFloat = 30
let tagPadding: CGFloat = 0
let tagSpacingX: CGFloat = 0
let tagSpacingY: CGFloat = 2
let containerWidth = activeCodeContentView.frame.size.width
var currentOriginX: CGFloat = 0
var currentOriginY: CGFloat = 0
// var couponCodesString = ""
for (index, item) in dfyCoupons.enumerated() {
let newLabel = CopyableLabel()
newLabel.font = UIFont(name: "PeridotPE-Bold", size: 18)
newLabel.textColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)
if (index == (dfyCoupons.endIndex - 1)) {
// couponCodesString += String(item._code)
newLabel.text = String(item._code)
} else {
// couponCodesString += String(item._code) + ", "
newLabel.text = String(item._code) + ", "
}
newLabel.frame.size.width = newLabel.intrinsicContentSize.width + tagPadding
newLabel.frame.size.height = newLabel.intrinsicContentSize.height // tagHeight
tagHeight = newLabel.intrinsicContentSize.height
activeCodeContentView.addSubview(newLabel)
// if current X + label width will be greater than container view width
// "move to next row"
if currentOriginX + newLabel.frame.width > containerWidth {
currentOriginX = 0
currentOriginY += tagHeight + tagSpacingY
}
// set the btn frame origin
newLabel.frame.origin.x = currentOriginX
newLabel.frame.origin.y = currentOriginY
// increment current X by btn width + spacing
currentOriginX += newLabel.frame.width + tagSpacingX
}
activeCodesCountLabel.text = String(dfyCoupons.count) + " Ενεργοί κωδικοί:"
// activeCodeLabel.text = couponCodesString
activeCodeExpirationLabel.isHidden = true
// update container view height
activeCodeContentViewHeight.constant = currentOriginY + tagHeight
if ((currentOriginY + tagHeight) <= (2 * tagHeight + tagSpacingY)) {
activeCodeScrollViewHeight.constant = currentOriginY + tagHeight
} else {
activeCodeScrollViewHeight.constant = 2 * tagHeight + tagSpacingY
}
}
} else {
activeCodeLabel.text = "-"
activeCodeExpirationLabel.text = ""
activeCodeView.isHidden = true
activeCodeViewHeight.constant = 0
}
}
public override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
public override func layoutSubviews() {
super.layoutSubviews()
//set the values for top,left,bottom,right margins
let margins = UIEdgeInsets(top: 2, left: 0, bottom: 4, right: 0)
contentView.frame = contentView.frame.inset(by: margins)
}
}