Showing
1 changed file
with
73 additions
and
22 deletions
| ... | @@ -128,27 +128,9 @@ public class CampaignItemModel { | ... | @@ -128,27 +128,9 @@ public class CampaignItemModel { |
| 128 | self.description = dictionary["description"] as? String? ?? "" | 128 | self.description = dictionary["description"] as? String? ?? "" |
| 129 | self.workflow_settings = dictionary["workflow_settings"] as? [String: Any] | 129 | self.workflow_settings = dictionary["workflow_settings"] as? [String: Any] |
| 130 | 130 | ||
| 131 | - let startDateString = dictionary["start_date"] as? String? ?? "" | 131 | + // Store raw date strings as received from server for better flexibility |
| 132 | - let dateFormatter = DateFormatter() | 132 | + self.start_date = dictionary["start_date"] as? String? ?? "" |
| 133 | - dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" | 133 | + self.end_date = dictionary["end_date"] as? String? ?? "" |
| 134 | - if let date = dateFormatter.date(from: startDateString ?? "") { | ||
| 135 | - dateFormatter.dateFormat = "dd/MM/yyyy" | ||
| 136 | - let resultString = dateFormatter.string(from: date) | ||
| 137 | - self.start_date = resultString | ||
| 138 | - } else { | ||
| 139 | - self.start_date = "" | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | - let endDateString = dictionary["end_date"] as? String? ?? "" | ||
| 143 | - let dateFormatter2 = DateFormatter() | ||
| 144 | - dateFormatter2.dateFormat = "yyyy-MM-dd HH:mm:ss" | ||
| 145 | - if let date = dateFormatter2.date(from: endDateString ?? "") { | ||
| 146 | - dateFormatter2.dateFormat = "dd/MM/yyyy" | ||
| 147 | - let resultString = dateFormatter2.string(from: date) | ||
| 148 | - self.end_date = resultString | ||
| 149 | - } else { | ||
| 150 | - self.end_date = "" | ||
| 151 | - } | ||
| 152 | 134 | ||
| 153 | if let extra_fields = dictionary["extra_fields"] as? [String: Any] { | 135 | if let extra_fields = dictionary["extra_fields"] as? [String: Any] { |
| 154 | self.subcategory = extra_fields["subcategory"] as? String? ?? "" | 136 | self.subcategory = extra_fields["subcategory"] as? String? ?? "" |
| ... | @@ -159,7 +141,7 @@ public class CampaignItemModel { | ... | @@ -159,7 +141,7 @@ public class CampaignItemModel { |
| 159 | self.type = extra_fields["type"] as? String? ?? "" | 141 | self.type = extra_fields["type"] as? String? ?? "" |
| 160 | self.carousel = extra_fields["carousel"] as? String? ?? "false" | 142 | self.carousel = extra_fields["carousel"] as? String? ?? "false" |
| 161 | self.category_title = extra_fields["category_title"] as? String ?? "" | 143 | self.category_title = extra_fields["category_title"] as? String ?? "" |
| 162 | - self.banner_img = CampaignItemModel.cleanEscapedUrl(extra_fields["Banner_img"] as? String ?? "") | 144 | + self.banner_img = CampaignItemModel.cleanEscapedUrl(extra_fields["banner_img"] as? String ?? "") |
| 163 | self.banner_title = extra_fields["Banner_title"] as? String ?? "" | 145 | self.banner_title = extra_fields["Banner_title"] as? String ?? "" |
| 164 | self.category_id = extra_fields["category_id"] as? String ?? "" | 146 | self.category_id = extra_fields["category_id"] as? String ?? "" |
| 165 | self.filter = extra_fields["filter"] as? String ?? "" | 147 | self.filter = extra_fields["filter"] as? String ?? "" |
| ... | @@ -354,6 +336,75 @@ public class CampaignItemModel { | ... | @@ -354,6 +336,75 @@ public class CampaignItemModel { |
| 354 | set(newValue) { self.banner_img_mobile = newValue } | 336 | set(newValue) { self.banner_img_mobile = newValue } |
| 355 | } | 337 | } |
| 356 | 338 | ||
| 339 | + // MARK: - Date Formatting Functions | ||
| 340 | + | ||
| 341 | + /// Format start date for UI display | ||
| 342 | + /// - Parameter format: Desired output format (default: "dd/MM/yyyy") | ||
| 343 | + /// - Returns: Formatted date string or empty string if parsing fails | ||
| 344 | + public func formattedStartDate(format: String = "dd/MM/yyyy") -> String { | ||
| 345 | + return CampaignItemModel.formatDate(self.start_date, outputFormat: format) | ||
| 346 | + } | ||
| 347 | + | ||
| 348 | + /// Format end date for UI display | ||
| 349 | + /// - Parameter format: Desired output format (default: "dd/MM/yyyy") | ||
| 350 | + /// - Returns: Formatted date string or empty string if parsing fails | ||
| 351 | + public func formattedEndDate(format: String = "dd/MM/yyyy") -> String { | ||
| 352 | + return CampaignItemModel.formatDate(self.end_date, outputFormat: format) | ||
| 353 | + } | ||
| 354 | + | ||
| 355 | + /// Generic date formatter with multiple input format support | ||
| 356 | + /// - Parameters: | ||
| 357 | + /// - dateString: Raw date string from server | ||
| 358 | + /// - outputFormat: Desired output format | ||
| 359 | + /// - Returns: Formatted date string or empty string if parsing fails | ||
| 360 | + private static func formatDate(_ dateString: String?, outputFormat: String) -> String { | ||
| 361 | + guard let dateString = dateString, !dateString.isEmpty else { return "" } | ||
| 362 | + | ||
| 363 | + let inputFormats = [ | ||
| 364 | + "yyyy-MM-dd HH:mm:ssZZZZZ", // With timezone: "2025-07-12 11:55:00+03:00" | ||
| 365 | + "yyyy-MM-dd HH:mm:ss" // Without timezone: "2025-07-12 11:55:00" | ||
| 366 | + ] | ||
| 367 | + | ||
| 368 | + let inputFormatter = DateFormatter() | ||
| 369 | + let outputFormatter = DateFormatter() | ||
| 370 | + outputFormatter.dateFormat = outputFormat | ||
| 371 | + | ||
| 372 | + for format in inputFormats { | ||
| 373 | + inputFormatter.dateFormat = format | ||
| 374 | + if let date = inputFormatter.date(from: dateString) { | ||
| 375 | + return outputFormatter.string(from: date) | ||
| 376 | + } | ||
| 377 | + } | ||
| 378 | + | ||
| 379 | + // If all formats fail, return the original string | ||
| 380 | + print("⚠️ [CampaignItemModel] Failed to parse date: \(dateString)") | ||
| 381 | + return dateString | ||
| 382 | + } | ||
| 383 | + | ||
| 384 | + // MARK: - Backward Compatibility | ||
| 385 | + | ||
| 386 | + /// Formatted start date for backward compatibility | ||
| 387 | + /// Returns start date in "dd/MM/yyyy" format | ||
| 388 | + public var _start_date_formatted: String { | ||
| 389 | + return formattedStartDate() | ||
| 390 | + } | ||
| 391 | + | ||
| 392 | + /// Formatted end date for backward compatibility | ||
| 393 | + /// Returns end date in "dd/MM/yyyy" format | ||
| 394 | + public var _end_date_formatted: String { | ||
| 395 | + return formattedEndDate() | ||
| 396 | + } | ||
| 397 | + | ||
| 398 | + /// Raw start date as received from server | ||
| 399 | + public var _start_date_raw: String? { | ||
| 400 | + return self.start_date | ||
| 401 | + } | ||
| 402 | + | ||
| 403 | + /// Raw end date as received from server | ||
| 404 | + public var _end_date_raw: String? { | ||
| 405 | + return self.end_date | ||
| 406 | + } | ||
| 407 | + | ||
| 357 | // MARK: - Computed properties for UI compatibility | 408 | // MARK: - Computed properties for UI compatibility |
| 358 | 409 | ||
| 359 | public var _valid_until: String? { | 410 | public var _valid_until: String? { | ... | ... |
-
Please register or login to post a comment