Vasilis
......@@ -13,146 +13,6 @@ import Foundation
import UIKit
class CouponSetItemModel {
let uuid: String?
let admin_name: String?
let name: String?
let img_preview: String?
let expiration: String?
let description: String?
let short_description: String?
let discount: String?
init(dictionary: [String: Any]) {
self.uuid = dictionary["uuid"] as? String? ?? ""
self.admin_name = dictionary["admin_name"] as? String? ?? ""
self.name = dictionary["name"] as? String? ?? ""
self.img_preview = dictionary["img_preview"] as? String? ?? ""
self.description = dictionary["description"] as? String? ?? ""
self.short_description = dictionary["short_description"] as? String? ?? ""
self.discount = dictionary["discount"] as? String? ?? ""
let expirationObject = dictionary["expiration"] as? [String: Any]? ?? ["":""]
let expirationString = expirationObject?["value"] as? String? ?? ""
// Example expirationString: Optional(2022-12-05 01:55)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm"
if let date = dateFormatter.date(from: expirationString ?? "") {
dateFormatter.dateFormat = "dd/MM/yyyy"
let resultString = dateFormatter.string(from: date)
self.expiration = resultString
} else {
self.expiration = ""
}
}
var asDictionary : [String:Any] {
let mirror = Mirror(reflecting: self)
let dict = Dictionary(uniqueKeysWithValues: mirror.children.lazy.map({ (label:String?, value:Any) -> (String, Any)? in
guard let label = label else { return nil }
return (label, value)
}).compactMap { $0 })
return dict
}
}
class CouponDataModel {
var data: Array<CouponSetItemModel> = []
init() { //initializer method
let instanceOfMyApi = MyApi()
let couponSets = instanceOfMyApi.getCouponSets(withActive: true, andVisible: true, andUuids: nil)
var couponSetsArray:Array<CouponSetItemModel> = []
if let myCouponsSetsDictionary = couponSets as? [String : AnyObject] {
let couponSetsData = (myCouponsSetsDictionary["MAPP_COUPON"] as! NSArray)
for couponset in couponSetsData {
let tempCouponset = CouponSetItemModel(dictionary: couponset as! [String : Any])
couponSetsArray.append(tempCouponset)
}
}
self.data = couponSetsArray
}
var getData: Array<CouponSetItemModel> {
get { // getter
return data
}
}
}
class CampaignItemModel {
let index_url: String?
let logo_url: String?
let offer_category: String?
let title: String?
let subtitle: String?
let session_uuid: String?
let subcategory: String?
init(dictionary: [String: Any]) {
self.index_url = dictionary["index_url"] as? String? ?? ""
self.logo_url = dictionary["logo_url"] as? String? ?? ""
self.offer_category = dictionary["offer_category"] as? String? ?? ""
self.title = dictionary["title"] as? String? ?? ""
self.subtitle = dictionary["subtitle"] as? String? ?? ""
self.session_uuid = dictionary["session_uuid"] as? String? ?? ""
// let extra_fields = dictionary["extra_fields"] as? [String: Any]? ?? ["":""]
let extra_fields = dictionary["extra_fields"] as AnyObject
var extra_fields_parsed:[String: Any]
let json = extra_fields.data(using: String.Encoding.utf8.rawValue)
do {
if let jsonArray = try JSONSerialization.jsonObject(with: json!, options: .allowFragments) as? [String:AnyObject]
{
extra_fields_parsed = jsonArray;
self.subcategory = extra_fields_parsed["subcategory"] as? String? ?? ""
} else {
self.subcategory = ""
print("bad json")
}
} catch let error as NSError {
self.subcategory = ""
print(error)
}
}
}
class CampaignDataModel {
var data: Array<CampaignItemModel> = []
init() { //initializer method
let instanceOfMyApi = MyApi()
let products = instanceOfMyApi.getInbox() as NSMutableArray?
var giftsArray:Array<CampaignItemModel> = []
for gift in products ?? [] {
let tempGift = CampaignItemModel(dictionary: gift as! [String : Any])
giftsArray.append(tempGift)
}
self.data = giftsArray;
}
var getData: Array<CampaignItemModel> {
get { // getter
return data
}
}
}
extension String {
func htmlToString() -> String {
......@@ -374,7 +234,7 @@ extension GiftsView {
struct giftItemView: View {
var item: CampaignItemModel
var item: swiftApi.CampaignItemModel
var isFirst: Bool
var isLast: Bool
var parentView: UIView
......@@ -416,7 +276,7 @@ extension GiftsView {
}
struct giftsContainer: View {
@State var gifts:Array<CampaignItemModel> = []
@State var gifts:Array<swiftApi.CampaignItemModel> = []
@State var title:String = ""
@State var parentView:UIView
......@@ -452,7 +312,7 @@ extension GiftsView {
}
struct couponsContainer: View {
@State var coupons:Array<CouponSetItemModel> = []
@State var coupons:Array<swiftApi.CouponSetItemModel> = []
@State var title:String = ""
@State var parentView:UIView
......@@ -489,7 +349,7 @@ extension GiftsView {
}
struct couponItemView: View {
var item: CouponSetItemModel
var item: swiftApi.CouponSetItemModel
var isFirst: Bool
var isLast: Bool
var parentView: UIView
......@@ -499,9 +359,9 @@ extension GiftsView {
var body: some View {
Button {
// GiftItem Action
let tempItem = item.asDictionary
let instanceOfMyApi = MyApi()
let couponViewController = instanceOfMyApi.openCoupon(parentView, coupon: tempItem)!
// let tempItem = item.asDictionary
let instanceOfMySwiftApi = swiftApi()
let couponViewController = instanceOfMySwiftApi.openCoupon(parentView, coupon: item)!
couponViewController.view.tag = 2
// addChild(couponsViewController)
couponViewController.view.frame = parentView.frame
......@@ -598,11 +458,26 @@ extension GiftsView {
struct GiftsView: View {
var parentView: UIView
var coupons:Array<CouponSetItemModel> = CouponDataModel().getData
var campains:Array<CampaignItemModel> = CampaignDataModel().getData.filter { $0.offer_category == "gifts_for_you" }
// var coupons:Array<CouponSetItemModel> = CouponDataModel().getData
// var campains:Array<CampaignItemModel> = CampaignDataModel().getData.filter { $0.offer_category == "gifts_for_you" }
var coupons:Array<swiftApi.CouponSetItemModel> = []
var campains:Array<swiftApi.CampaignItemModel> = []
var uiscreen = UIScreen.main.bounds
init(parentView: UIView) { //initializer method
let instanceOfMySwiftApi = swiftApi()
let couponsData = instanceOfMySwiftApi.getCouponSets()
let campaignsData = instanceOfMySwiftApi.getCampaigns()
self.coupons = couponsData
self.campains = campaignsData.filter { $0.offer_category == "gifts_for_you" }
self.parentView = parentView
}
func goBack(){
for subview in parentView.subviews {
if(subview.tag == 5) {
......