Manos Chorianopoulos

add active coupons sortin by expiration date

...@@ -206,7 +206,8 @@ import SwiftEventBus ...@@ -206,7 +206,8 @@ import SwiftEventBus
206 if (dfyCoupons.count > 0) { 206 if (dfyCoupons.count > 0) {
207 if (dfyCoupons.count == 1) { 207 if (dfyCoupons.count == 1) {
208 let dateFormatter = DateFormatter() 208 let dateFormatter = DateFormatter()
209 - dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" 209 + // dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
210 + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
210 211
211 // sort dfyCoupons by date 212 // sort dfyCoupons by date
212 dfyCoupons.sort(by: { 213 dfyCoupons.sort(by: {
......
...@@ -644,7 +644,22 @@ public class swiftApi { ...@@ -644,7 +644,22 @@ public class swiftApi {
644 644
645 // SwiftEventBus.post("coupons_fetched") 645 // SwiftEventBus.post("coupons_fetched")
646 646
647 - let activeCoupons = tempCoupons.filter({ return $0.status == 1 }) 647 + var activeCoupons = tempCoupons.filter({ return $0.status == 1 })
648 + // sort activeCoupons by expiration date
649 + let dateFormatter = DateFormatter()
650 + dateFormatter.dateFormat = "dd/MM/yyyy"
651 +
652 + activeCoupons.sort(by: {
653 + let date1 = dateFormatter.date(from: $0.expiration ?? "")
654 + let date2 = dateFormatter.date(from: $1.expiration ?? "")
655 +
656 + if ((date1 != nil) && (date2 != nil)) {
657 + return date1!.compare(date2!) == .orderedAscending
658 + } else {
659 + return false
660 + }
661 +
662 + })
648 getCouponsCallback(activeCoupons) 663 getCouponsCallback(activeCoupons)
649 } 664 }
650 665
...@@ -3781,8 +3796,24 @@ public class swiftApi { ...@@ -3781,8 +3796,24 @@ public class swiftApi {
3781 3796
3782 public func setCouponList(_ coupons: Array<CouponItemModel>) { 3797 public func setCouponList(_ coupons: Array<CouponItemModel>) {
3783 // let filteredCoupons = coupons.filter { CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: $0.discount ?? "")) } 3798 // let filteredCoupons = coupons.filter { CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: $0.discount ?? "")) }
3784 - let activeCoupons = coupons.filter({ return $0.status == 1 }) 3799 + var activeCoupons = coupons.filter({ return $0.status == 1 })
3785 let oldCoupons = coupons.filter({ return $0.status == 0 }) 3800 let oldCoupons = coupons.filter({ return $0.status == 0 })
3801 +
3802 + // sort activeCoupons by expiration date
3803 + let dateFormatter = DateFormatter()
3804 + dateFormatter.dateFormat = "dd/MM/yyyy"
3805 +
3806 + activeCoupons.sort(by: {
3807 + let date1 = dateFormatter.date(from: $0.expiration ?? "")
3808 + let date2 = dateFormatter.date(from: $1.expiration ?? "")
3809 +
3810 + if ((date1 != nil) && (date2 != nil)) {
3811 + return date1!.compare(date2!) == .orderedAscending
3812 + } else {
3813 + return false
3814 + }
3815 +
3816 + })
3786 3817
3787 GlobalVariables.couponList = activeCoupons 3818 GlobalVariables.couponList = activeCoupons
3788 3819
......