Manos Chorianopoulos

add getCoupons async Request

......@@ -75,6 +75,8 @@
- (NSDictionary*)redeemCouponWithCoupon:(NSString*)coupon;
- (NSDictionary*)validateCouponWithCoupon:(NSString*)coupon;
- (NSDictionary*)loginCosmoteWithGuid:(NSString*)guid andAppUuid:(NSString*)appUuid andTicket:(NSString*)ticket;
- (void)getCouponsWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCouponsetsAsync:(NSNumber*) active andVisible:(NSNumber*) visible andUuids:(NSArray*) uuids :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
@end
#endif /* MyApi_h */
......
......@@ -1257,4 +1257,31 @@ CMPedometer *pedometer;
}
return resp;
}
- (void)getCouponsWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
[[Warply sharedService] getCouponsWithSuccessBlock:^(NSDictionary *response) {
if (success) {
success(response);
}
} failureBlock:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
- (void) getCouponsetsAsync:(NSNumber*) active andVisible:(NSNumber*) visible andUuids:(NSArray*) uuids :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
[[Warply sharedService] getCouponSetsWithSuccessBlock:active andVisible:visible andUuids:uuids :^(NSDictionary *response) {
if (success) {
success(response);
}
} failureBlock:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
@end
......
......@@ -220,54 +220,9 @@ public class swiftApi {
var data: Array<CouponItemModel> = []
init() { //initializer method
let instanceOfMyApi = MyApi()
let coupons = instanceOfMyApi.getCoupons()
let couponSets = instanceOfMyApi.getCouponSets(withActive: true, andVisible: true, andUuids: nil)
var couponsArray:Array<CouponItemModel> = []
if let myCouponsSetsDictionary = couponSets as? [String : AnyObject] {
let couponSetsData = (myCouponsSetsDictionary["MAPP_COUPON"] as! Array<NSMutableDictionary>)
if let myCouponsDictionary = coupons as? [String : AnyObject] {
let couponsData = (myCouponsDictionary["result"] as! Array<NSMutableDictionary>)
if let sets = couponSetsData as? NSArray {
for set in sets {
let s = set as! NSDictionary
if let cpns = couponsData as? NSArray {
for coupon in cpns {
var c = coupon as! NSDictionary
// var temp = NSMutableDictionary(dictionary: s);
if c["couponset_uuid"] as! String == s["uuid"] as! String {
let temp = NSMutableDictionary(dictionary: c);
temp.setValue(s as! [AnyHashable : Any],forKey: "couponset_data")
let tempCoupon = CouponItemModel(dictionary: temp as! [String : Any])
couponsArray.append(tempCoupon)
}
}
}
}
}
}
}
// if let myCouponsDictionary = coupons as? [String : AnyObject] {
// let couponsData = (myCouponsDictionary["result"] as! NSArray)
//
//
// for coupon in couponsData {
// let tempCoupon = CouponItemModel(dictionary: coupon as! [String : Any])
// couponsArray.append(tempCoupon)
// }
//
// }
self.data = couponsArray
}
var getData: Array<CouponItemModel> {
get { // getter
return data.filter({
......@@ -285,6 +240,105 @@ public class swiftApi {
}
}
func getCouponsData(_ getCouponsCallback: @escaping (_ couponsData: Array<CouponItemModel>?) -> Void) -> Void {
var coupons: [AnyHashable : Any]?
var couponSets: [AnyHashable : Any]?
let instanceOfMyApi = MyApi()
instanceOfMyApi.getCouponsWithSuccessBlock(couponsCallback, failureBlock: (couponsFailureCallback))
func couponsCallback(_ couponsData: [AnyHashable : Any]?) -> Void {
coupons = couponsData ?? ["":""]
// On Coupons request success, make CouponSets request
instanceOfMyApi.getCouponsetsAsync(true, andVisible: true, andUuids: nil, couponSetsCallback, failureBlock: couponSetsFailureCallback)
}
func couponsFailureCallback(_ error: Error?) -> Void {
print("getCoupons error: ")
getCouponsCallback(nil)
}
func couponSetsCallback(_ couponSetsData: [AnyHashable : Any]?) -> Void {
couponSets = couponSetsData ?? ["":""]
// On CouponSets request Success, match coupons with couponsets
let tempCoupons = matchCoupons()
getCouponsCallback(tempCoupons)
}
func couponSetsFailureCallback(_ error: Error?) -> Void {
print("getCouponSets error: ")
getCouponsCallback(nil)
}
func matchCoupons() -> Array<CouponItemModel> {
var couponsArray:Array<CouponItemModel> = []
if let myCouponsSetsDictionary = couponSets as? [String : AnyObject] {
let couponSetsData = (myCouponsSetsDictionary["MAPP_COUPON"] as! Array<NSMutableDictionary>)
if let myCouponsDictionary = coupons as? [String : AnyObject] {
let couponsData = (myCouponsDictionary["result"] as! Array<NSMutableDictionary>)
if let sets = couponSetsData as? NSArray {
for set in sets {
let s = set as! NSDictionary
if let cpns = couponsData as? NSArray {
for coupon in cpns {
var c = coupon as! NSDictionary
// var temp = NSMutableDictionary(dictionary: s);
if c["couponset_uuid"] as! String == s["uuid"] as! String {
let temp = NSMutableDictionary(dictionary: c);
temp.setValue(s as! [AnyHashable : Any],forKey: "couponset_data")
let tempCoupon = CouponItemModel(dictionary: temp as! [String : Any])
couponsArray.append(tempCoupon)
}
}
}
}
}
}
}
// if let myCouponsDictionary = coupons as? [String : AnyObject] {
// let couponsData = (myCouponsDictionary["result"] as! NSArray)
//
//
// for coupon in couponsData {
// let tempCoupon = CouponItemModel(dictionary: coupon as! [String : Any])
// couponsArray.append(tempCoupon)
// }
//
// }
return couponsArray
}
}
}
public func filterActiveCoupons(_ coupons: Array<CouponItemModel>) -> Array<CouponItemModel> {
return coupons.filter({
return $0.status == 1
})
}
public func filterOldCoupons(_ coupons: Array<CouponItemModel>) -> Array<CouponItemModel> {
return coupons.filter({
return $0.status != 1
})
}
public func getCouponsAsync(_ getCouponsCallback: @escaping (_ couponsData: Array<CouponItemModel>?) -> Void) -> Void {
CouponsDataModel().getCouponsData(getCouponsCallback)
}
public func getCoupons() -> Array<CouponItemModel> {
......