Manos Chorianopoulos

add dynamic dfyCoupons at WalletVC

...@@ -29,6 +29,7 @@ import UIKit ...@@ -29,6 +29,7 @@ import UIKit
29 @IBOutlet weak var activeCodeImage: UIImageView! 29 @IBOutlet weak var activeCodeImage: UIImageView!
30 30
31 public var coupons:Array<swiftApi.CouponItemModel> = [] 31 public var coupons:Array<swiftApi.CouponItemModel> = []
32 + public var dfyCoupons:Array<swiftApi.ActiveDFYCouponModel> = swiftApi().getActiveDFYCoupons()
32 33
33 public override func viewDidLoad() { 34 public override func viewDidLoad() {
34 super.viewDidLoad() 35 super.viewDidLoad()
...@@ -84,9 +85,12 @@ import UIKit ...@@ -84,9 +85,12 @@ import UIKit
84 dfyEarnView.layer.shadowOpacity = 1.0 85 dfyEarnView.layer.shadowOpacity = 1.0
85 dfyEarnView.layer.shadowRadius = 3.0 86 dfyEarnView.layer.shadowRadius = 3.0
86 87
87 - // TODO: dynamic 88 + var totalCouponValue = self.dfyCoupons.reduce(0) { $0 + (Float($1._value) ?? 0) }
88 - dfyEarnLabel.text = "Μέχρι τώρα έχεις κερδίσει 18,00€ με το DEALS for YOU!" 89 + totalCouponValue = Float(round(100 * totalCouponValue) / 100)
89 - dfyEarnAmountLabel.text = "18,00€" 90 + let totalCouponValueString = String(format: "%.2f", totalCouponValue).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
91 +
92 + dfyEarnLabel.text = "Μέχρι τώρα έχεις κερδίσει " + totalCouponValueString + "€ με το DEALS for YOU!"
93 + dfyEarnAmountLabel.text = totalCouponValueString + "€"
90 94
91 couponEarnView.layer.cornerRadius = 5.0 95 couponEarnView.layer.cornerRadius = 5.0
92 couponEarnView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor 96 couponEarnView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor
...@@ -104,9 +108,40 @@ import UIKit ...@@ -104,9 +108,40 @@ import UIKit
104 activeCodeView.layer.shadowOpacity = 1.0 108 activeCodeView.layer.shadowOpacity = 1.0
105 activeCodeView.layer.shadowRadius = 6.0 109 activeCodeView.layer.shadowRadius = 6.0
106 110
107 - // TODO: dynamic 111 + if (dfyCoupons.count > 0) {
108 - activeCodeLabel.text = "961544809" 112 + let dateFormatter = DateFormatter()
109 - activeCodeExpirationLabel.text = "Λήγει σε 4 ημέρες" 113 + dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
114 +
115 + // sort dfyCoupons by date
116 + dfyCoupons.sort(by: {
117 + let date1 = dateFormatter.date(from: $0._date)
118 + let date2 = dateFormatter.date(from: $1._date)
119 +
120 + if ((date1 != nil) && (date2 != nil)) {
121 + return date1!.compare(date2!) == .orderedAscending
122 + } else {
123 + return false
124 + }
125 +
126 + })
127 +
128 + // Get days from now of the most recet coupon
129 + var daysFromNow = ""
130 + let calendar = Calendar.current
131 +
132 + // Replace the hour (time) of both dates with 00:00
133 + let date1 = calendar.startOfDay(for: Date())
134 + if let date2 = dateFormatter.date(from: dfyCoupons[0]._date) {
135 + let components = calendar.dateComponents([.day], from: date1, to: date2)
136 + daysFromNow = (components.day) != nil ? String(components.day ?? 0) : ""
137 + }
138 +
139 + activeCodeLabel.text = dfyCoupons[0]._code
140 + activeCodeExpirationLabel.text = "Λήγει σε " + daysFromNow + " ημέρες"
141 + } else {
142 + activeCodeLabel.text = "-"
143 + activeCodeExpirationLabel.text = ""
144 + }
110 } 145 }
111 146
112 // MARK: - API Functions 147 // MARK: - API Functions
......