Manos Chorianopoulos

MyCouponsViewController part3

......@@ -96,7 +96,7 @@ public class CouponSetItemModel {
/// Try multiple date formats and return the first successful parse
/// - Parameter dateString: The date string to parse
/// - Returns: Parsed Date or nil if no format matched
private static func parseDate(_ dateString: String) -> Date? {
static func parseDate(_ dateString: String) -> Date? {
let formatter = DateFormatter()
for format in supportedDateFormats {
formatter.dateFormat = format
......@@ -411,7 +411,8 @@ public class CouponItemModel {
public let couponset_uuid: String?
public let name: String?
public let image: String?
public let expiration: String?
public let expiration: String? // raw string from API e.g. "2026-06-30 11:59:00"
public let expiration_formatted: String? // pre-formatted "dd/MM/yyyy" for display
public let created: String?
public let description: String?
public let discount: String?
......@@ -495,15 +496,16 @@ public class CouponItemModel {
}
// <==
let expirationString = dictionary["expiration"] as? String? ?? ""
let expirationString = dictionary["expiration"] as? String ?? ""
self.expiration = expirationString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let date = dateFormatter.date(from: expirationString ?? "") {
if let date = dateFormatter.date(from: expirationString) {
dateFormatter.dateFormat = "dd/MM/yyyy"
let resultString = dateFormatter.string(from: date)
self.expiration = resultString
self.expiration_formatted = dateFormatter.string(from: date)
} else {
self.expiration = ""
self.expiration_formatted = ""
}
// Extract created date: try changes_dates.created first (universal coupons), then top-level created
......@@ -558,6 +560,26 @@ public class CouponItemModel {
public func setCouponSetData(_ couponSet: CouponSetItemModel) {
self.couponset_data = couponSet
}
/// Returns the expiration as a Date object for date comparisons (e.g. days-left calculation)
public var expirationDate: Date? {
guard let raw = self.expiration, !raw.isEmpty else { return nil }
return CouponSetItemModel.parseDate(raw)
}
/// Format expiration date with a custom output format.
/// Uses the same multi-format parser as CouponSetItemModel.
/// - Parameter format: DateFormatter format string (e.g. "MMMM d, yyyy", "dd/MM/yyyy")
/// - Returns: Formatted date string, or empty string if expiration is absent or unparseable
public func formattedExpiration(format: String) -> String {
guard let raw = self.expiration, !raw.isEmpty else { return "" }
if let date = CouponSetItemModel.parseDate(raw) {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: date)
}
return ""
}
}
public class RedeemedSMHistoryModel {
......
......@@ -383,7 +383,7 @@ extension MyCouponsViewController: UITableViewDelegate, UITableViewDataSource {
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath.section <= 3) {
if (indexPath.section <= 1) {
// Do nothing
} else {
if let items = self.filteredOffersSection?.items,
......