CouponsTableViewCell.swift
9.83 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
//
// CouponsTableViewCell.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 18/5/22.
//
import Foundation
import UIKit
@objc public class CouponsTableViewCell: UITableViewCell {
@IBOutlet weak var couponBgImage: UIImageView!
@IBOutlet weak var couponImage: UIImageView!
@IBOutlet weak var borderView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var dicountLabel: UILabel!
@IBOutlet weak var discriptionLabel: UILabel!
@IBOutlet weak var expirationLabel: UILabel!
@IBOutlet weak var expirationRedView: UIView!
@IBOutlet weak var expirationRedImage: UIImageView!
@IBOutlet weak var expirationRedLabel: UILabel!
var postImageURL: String? {
didSet {
if let url = postImageURL {
self.couponImage.image = UIImage() // UIImage(named: "loading")
UIImage.loadImageUsingCacheWithUrlString(url) { image in
// set the image only when we are still displaying the content for the image we finished downloading
if url == self.postImageURL {
self.couponImage.image = image
}
}
}
else {
self.couponImage.image = nil
}
}
}
public override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// 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
couponBgImage.image = UIImage(named: "coupon_bg_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
borderView.addDashedBorderVertical(color: UIColor(red: 0.62, green: 0.62, blue: 0.61, alpha: 1.00), width: 1.0, height: 110.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: 0, left: 0, bottom: 8, right: 0)
contentView.frame = contentView.frame.inset(by: margins)
}
func configureCell(coupon: swiftApi.CouponItemModel) {
// COUPONSET: desc, img_preview, name, terms, merchant_uuid, discount_type, final_price
// COUPON: coupon, expiration, discount, status
// MERCHANT: _img_preview,_admin_name
let merchantList:Array<swiftApi.MerchantModel> = swiftApi().getMerchantList()
let couponSetData: swiftApi.CouponSetItemModel? = coupon.couponset_data
nameLabel.text = ""
for merchant in merchantList {
if (merchant._uuid == couponSetData?.merchant_uuid) {
// couponImage.load(link: merchant._img_preview, placeholder: UIImage(), cache: URLCache())
self.postImageURL = merchant._img_preview
nameLabel.text = merchant._admin_name
break;
}
}
// couponImage.load(link: couponSetData?.img_preview ?? "", placeholder: UIImage(), cache: URLCache())
// nameLabel.text = couponSetData?.name ?? ""
let discount = couponSetData?.discount_type == "value" ? ((coupon.discount ?? "") + "€")
: couponSetData?.discount_type == "percentage" ? ((coupon.discount ?? "") + "%")
: couponSetData?.discount_type == "plus_one" ? "1+1"
: ((coupon.discount ?? "") + "€")
dicountLabel.text = discount
// let htmlText = couponSetData?.inner_text ?? ""
// discriptionLabel.text = htmlText.htmlToString
discriptionLabel.text = couponSetData?.inner_text ?? ""
expirationLabel.text = "Ισχύει έως "+(coupon.expiration ?? "")
}
func configureCell(coupon: swiftApi.CouponItemModel, isMarket: Bool) {
// COUPONSET: desc, img_preview, name, terms, merchant_uuid
// COUPON: coupon, expiration, discount, status
// MERCHANT: _img_preview,_admin_name
expirationLabel.isHidden = true
expirationRedView.isHidden = true
expirationLabel.text = ""
expirationRedLabel.text = ""
if (coupon.status == 1) { // Active status 1
// Add shadow
// self.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor
// self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
// self.layer.shadowOpacity = 1.0
// self.layer.shadowRadius = 6.0
// couponBgImage.image = UIImage(named: "coupon_bg", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
expirationLabel.isHidden = false
expirationRedView.isHidden = true
expirationLabel.text = "Ισχύει έως "+(coupon.expiration ?? "")
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
let today = Date()
// let firstDate = formatter.date(from: "01/08/2017")
// let secondDate = formatter.date(from: "22/04/2023")
if let couponExpiration = coupon.expiration, let secondDate = formatter.date(from: couponExpiration) {
let calendar = NSCalendar.current
let date1 = calendar.startOfDay(for: today)
let date2 = calendar.startOfDay(for: secondDate)
let differenceComponents = calendar.dateComponents([.day], from: date1, to: date2)
if let daysDifference = differenceComponents.day {
if ((daysDifference >= 0) && (daysDifference <= 15)) {
expirationLabel.isHidden = true
expirationRedView.isHidden = false
expirationRedView.layer.cornerRadius = 10.0
expirationRedView.backgroundColor = UIColor(red: 0.72, green: 0.88, blue: 0.94, alpha: 1.00)
expirationRedImage.image = UIImage(named: "ic_time_forward_blue", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
let normalText1 = "Ισχύει έως "
let redText = (coupon.expiration ?? "")
let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-SBold", size: 12) ?? UIFont.systemFont(ofSize: 11), NSAttributedString.Key.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)]
let attrRed = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-SBold", size: 12) ?? UIFont.systemFont(ofSize: 11), NSAttributedString.Key.foregroundColor: UIColor(red: 0.00, green: 0.29, blue: 0.53, alpha: 1.00)]
let attributedString = NSMutableAttributedString(string:normalText1, attributes:attrRegular)
let RedString = NSMutableAttributedString(string: redText, attributes:attrRed)
attributedString.append(RedString)
expirationRedLabel.attributedText = attributedString
} else {
expirationLabel.isHidden = false
expirationRedView.isHidden = true
expirationLabel.text = "Ισχύει έως "+(coupon.expiration ?? "")
}
}
}
} else if (coupon.status == 0) { // Redeemed status 0
// couponBgImage.image = UIImage(named: "coupon_bg_grey", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
expirationLabel.isHidden = false
expirationRedView.isHidden = true
expirationLabel.text = "Το κουπόνι έληξε"
couponImage.layer.opacity = 0.29
borderView.layer.opacity = 0.29
nameLabel.layer.opacity = 0.29
dicountLabel.layer.opacity = 0.29
discriptionLabel.layer.opacity = 0.29
} else { // Expired status -1
expirationLabel.isHidden = false
expirationRedView.isHidden = true
expirationLabel.text = "Το κουπόνι έληξε"
couponImage.layer.opacity = 0.29
borderView.layer.opacity = 0.29
nameLabel.layer.opacity = 0.29
dicountLabel.layer.opacity = 0.29
discriptionLabel.layer.opacity = 0.29
}
let merchantList:Array<swiftApi.MerchantModel> = swiftApi().getMerchantList()
nameLabel.text = ""
for merchant in merchantList {
if (merchant._uuid == coupon.merchant_uuid) {
// couponImage.load(link: merchant._img_preview, placeholder: UIImage(), cache: URLCache())
self.postImageURL = merchant._img_preview
nameLabel.text = merchant._admin_name
break;
}
}
// let priceFloat = Float(round(100 * (Float(coupon.discount ?? "0.0") ?? 0.0 )) / 100)
// var priceString = "0"
// priceString = String(format: "%.2f", priceFloat).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
// dicountLabel.text = priceString + "€"
dicountLabel.text = (coupon.discount ?? "").replacingOccurrences(of: ".", with: ",", options: .literal, range: nil) + "€"
let htmlText = coupon.inner_text ?? ""
discriptionLabel.text = htmlText.htmlToString
// discriptionLabel.text = coupon.inner_text ?? ""
}
}