swiftApi.swift
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// swiftApi.swift
// WarplySDKFrameworkIOS
//
// Created by Βασιλης Σκουρας on 21/4/22.
//
import Foundation
import SwiftUI
public class swiftApi {
public init() {
}
public func getUserTag() -> String {
return "1"
}
public class DFY {
let couponCode: String?
let merchantId: String?
init(couponCode: String, merchantId: String) {
self.couponCode = couponCode
self.merchantId = merchantId
}
}
public func setDFY(couponCode: String, merchantId: String) {
DFY.init(couponCode: couponCode, merchantId: merchantId)
}
public class activeDFYCoupons {
let campaignIds: Array<String>
init(campaignIds: Array<String>) {
self.campaignIds = campaignIds
}
}
public func setActiveDFYCoupons(campaignIds: Array<String>) {
activeDFYCoupons.init(campaignIds: campaignIds)
}
public class CCMSLoyaltyCampaigns {
let campaigns: Array<Dictionary<String, String>>
init(campaigns: Array<Dictionary<String, String>>) {
self.campaigns = campaigns
}
}
public func setCCMSLoyaltyCampaigns(campaigns: Array<Dictionary<String, String>>) {
CCMSLoyaltyCampaigns.init(campaigns: campaigns)
}
public func getActiveDFYCoupons() -> Array<String>{
let array: Array<String> = []
return array
}
public 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?
let sorting: Int?
let inner_text: String?
let buyable: Bool?
let visible: Bool?
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? ?? ""
self.sorting = dictionary["sorting"] as? Int? ?? nil
self.inner_text = dictionary["inner_text"] as? String? ?? ""
self.buyable = dictionary["buyable"] as? Bool? ?? false
self.visible = dictionary["visible"] as? Bool? ?? false
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
// }
}
public class CouponSetsDataModel {
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
}
}
}
public func getCouponSets() -> Array<CouponSetItemModel> {
return CouponSetsDataModel().getData
}
public class CouponItemModel {
let couponset_uuid: String?
let name: String?
let image: String?
let expiration: String?
let description: String?
let discount: String?
let coupon: String?
let category: String?
let barcode: String?
let status: Int?
var couponset_data: CouponSetItemModel?
init(dictionary: [String: Any]) {
self.couponset_uuid = dictionary["couponset_uuid"] as? String? ?? ""
self.name = dictionary["name"] as? String? ?? ""
self.image = dictionary["image"] as? String? ?? ""
self.description = dictionary["description"] as? String? ?? ""
self.discount = dictionary["discount"] as? String? ?? ""
self.coupon = dictionary["coupon"] as? String? ?? ""
self.category = dictionary["category"] as? String? ?? ""
self.barcode = dictionary["barcode"] as? String? ?? ""
self.status = dictionary["status"] as? Int? ?? nil
if let couponSetData = dictionary["couponset_data"] as? [String: Any]? ?? ["":""] {
let tempCouponset = CouponSetItemModel(dictionary: couponSetData)
self.couponset_data = tempCouponset
} else {
self.couponset_data = nil
}
let expirationString = dictionary["expiration"] as? String? ?? ""
// Example expirationString: Optional(2022-12-05 01:55)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
if let date = dateFormatter.date(from: expirationString ?? "") {
dateFormatter.dateFormat = "dd/MM/yyyy"
let resultString = dateFormatter.string(from: date)
self.expiration = resultString
} else {
self.expiration = ""
}
}
public func setCouponSetData(_ couponSet: CouponSetItemModel) {
self.couponset_data = couponSet
}
// 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
// }
}
public class CouponsDataModel {
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({
return $0.status == 1
})
}
}
var getOldCoupons: Array<CouponItemModel> {
get { // getter
return data.filter({
return $0.status != 1
})
}
}
}
public func getCoupons() -> Array<CouponItemModel> {
return CouponsDataModel().getData
}
public func getOldCoupons() -> Array<CouponItemModel> {
return CouponsDataModel().getOldCoupons
}
public 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)
}
}
}
public 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
}
}
}
public func getCampaigns() -> Array<CampaignItemModel> {
return CampaignDataModel().getData
}
public func openCoupon(parent: UIView, coupon: CouponSetItemModel) -> UIViewController {
return UIHostingController(rootView: CouponView(parentView: parent, coupon: coupon))
}
public func openCouponBarcode(parent: UIView, coupon: CouponItemModel) -> UIViewController {
return UIHostingController(rootView: CouponBarcodeView(parentView: parent, coupon: coupon))
}
public func openSteps(parent: UIView) -> UIViewController {
return UIHostingController(rootView: StepsView(parentView: parent))
}
}