Manos Chorianopoulos

refactor WalletVC v1

......@@ -8,10 +8,172 @@
import UIKit
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()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// 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 <===
activeCodeView.layer.cornerRadius = 5.0
activeCodeView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor
activeCodeView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
activeCodeView.layer.shadowOpacity = 1.0
activeCodeView.layer.shadowRadius = 6.0
activeCodeImage.image = UIImage(named: "active_code_logo", 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: "PFSquareSansPro-Bold", size: 19)
newLabel.textColor = UIColor(rgb: 0x3C5365)
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: "PFSquareSansPro-Bold", size: 19)
newLabel.textColor = UIColor(rgb: 0x3C5365)
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
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
......
......@@ -5,19 +5,91 @@
// Created by Manos Chorianopoulos on 3/4/23.
//
import Foundation
import UIKit
class UnifiedCouponsTableViewCell: 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!
var postImageURL: String? {
didSet {
if let url = postImageURL {
self.couponImage.image = UIImage() // UIImage(named: "loading")
override func awakeFromNib() {
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
couponBgImage.image = UIImage(named: "coupon_bg", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
borderView.addDashedBorderVertical(color: UIColor(red: 0.44, green: 0.44, blue: 0.44, alpha: 1.00), width: 1.0, height: 110.0)
}
override func setSelected(_ selected: Bool, animated: Bool) {
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 ?? "")
}
}
......