Manos Chorianopoulos

add CouponView screen

...@@ -5,18 +5,194 @@ ...@@ -5,18 +5,194 @@
5 // Created by Βασιλης Σκουρας on 21/4/22. 5 // Created by Βασιλης Σκουρας on 21/4/22.
6 // 6 //
7 7
8 +
9 +#if canImport(SwiftUI)
8 import SwiftUI 10 import SwiftUI
11 +import Combine
12 +import Foundation
13 +import UIKit
9 14
10 -struct CouponView: View { 15 +
16 +
17 +extension CouponView {
18 + struct headerView: View {
19 + var goBack: () -> ()
20 +
21 + var uiscreen = UIScreen.main.bounds
22 +
23 + var body: some View {
24 + HStack(alignment: .center) {
25 + Button {
26 + // Button Action
27 + goBack()
28 + } label: {
29 + Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
30 + .resizable()
31 + .aspectRatio(contentMode: .fit)
32 + .frame(width: self.uiscreen.height * 0.025, height: self.uiscreen.height * 0.02)
33 + }
34 +
35 + Text("Εκπτωτικό κουπόνι")
36 + .fontWeight(.medium)
37 + .font(.system(size: 16))
38 + .foregroundColor(Color(red: 0.20784313725490197, green: 0.3176470588235294, blue: 0.40784313725490196))
39 + .multilineTextAlignment(.center)
40 + .frame(maxWidth: .infinity)
41 + .padding(.horizontal)
42 +
43 + }
44 + .frame(maxWidth: .infinity)
45 + .padding(.horizontal)
46 + .padding(.vertical, 10)
47 + }
48 + }
49 +
50 + struct couponContainer: View {
51 + @State var coupon: Dictionary<String, Any>
52 +// @State var couponset:CouponSetItemModel
53 +
54 + var uiscreen = UIScreen.main.bounds
55 +
56 + var body: some View {
57 + GeometryReader { geometry in
58 + ScrollView(showsIndicators: false) {
59 + VStack(alignment: .leading) {
60 +
61 + ImageView(withURL: coupon["img_preview"] as? String ?? "")
62 +
63 + Text(coupon["name"] as? String ?? "")
64 + .fontWeight(.bold)
65 + .font(.system(size: 16))
66 + .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
67 + .multilineTextAlignment(.leading)
68 + .padding(.horizontal)
69 + .padding(.top, self.uiscreen.height * 0.04)
70 +
71 + Text(coupon["short_description"] as? String ?? "")
72 + .fontWeight(.regular)
73 + .font(.system(size: 14))
74 + .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
75 + .multilineTextAlignment(.leading)
76 + .padding(.horizontal)
77 + .padding(.top, self.uiscreen.height * 0.02)
78 +
79 + Spacer()
80 +
81 + VStack(alignment: .center) {
82 + Button {
83 + // Button Action
84 + print("Back Button tapped!")
85 + } label: {
86 + HStack {
87 + Text("Απόκτησέ το")
88 + .fontWeight(.medium)
89 + .font(.system(size: 16))
90 + .foregroundColor(Color.white)
91 + .multilineTextAlignment(.center)
92 + .padding(.all)
93 + }
94 + }
95 + .frame(width: self.uiscreen.width * 0.8)
96 + .background(Color(red: 0.4745098039215686, green: 0.7490196078431373, blue: 0.0784313725490196))
97 + .cornerRadius(7)
98 + }
99 + .frame(maxWidth: .infinity)
100 + .padding(.top, self.uiscreen.height * 0.06)
101 +
102 + Button {
103 + // Button Action
104 + print("Terms Button tapped!")
105 + } label: {
106 + Text("Όροι χρήσης")
107 + .fontWeight(.medium)
108 + .font(.system(size: 14))
109 + .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
110 + .underline()
111 + .multilineTextAlignment(.center)
112 + .padding(.vertical, self.uiscreen.height * 0.03)
113 + .padding(.horizontal)
114 + .frame(maxWidth: .infinity)
115 + }
116 + .frame(maxWidth: .infinity)
117 +
118 +
119 + }
120 + .frame(width:self.uiscreen.width)
121 + .frame(minHeight: geometry.size.height)
122 + .padding(.bottom, self.uiscreen.height * 0.03)
123 + }
124 + .frame(width:self.uiscreen.width)
125 + .frame(maxHeight: .infinity)
126 + }
127 + .frame(width:self.uiscreen.width)
128 + .frame(maxHeight: .infinity)
129 + }
130 + }
131 +
132 + struct ImageView: View {
133 + @ObservedObject var imageLoader:ImageLoaderGifts
134 + @State var image:UIImage = UIImage()
135 +
136 + var uiscreen = UIScreen.main.bounds
137 +
138 + init(withURL url:String) {
139 + imageLoader = ImageLoaderGifts(urlString:url)
140 + }
141 +
142 + var body: some View {
143 +
144 + Image(uiImage: image)
145 + .resizable()
146 + .aspectRatio(contentMode: .fill)
147 + .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.25)
148 + .onReceive(imageLoader.didChange) { data in
149 + self.image = UIImage(data: data) ?? UIImage()
150 + }
151 + }
152 + }
11 153
154 +}
155 +
156 +@available(iOS 13.0.0, *)
157 +struct CouponView: View {
12 var parentView: UIView 158 var parentView: UIView
159 + var coupon: Dictionary<String, Any>
160 + @State var couponset: CouponSetItemModel?
13 161
14 var uiscreen = UIScreen.main.bounds 162 var uiscreen = UIScreen.main.bounds
15 163
164 + func goBack() {
165 + for subview in parentView.subviews {
166 + if(subview.tag == 2) {
167 + subview.removeFromSuperview()
168 + }
169 + }
170 + }
171 +
172 +
16 var body: some View { 173 var body: some View {
17 - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) 174 + VStack {
175 + headerView(goBack: goBack)
176 +
177 + VStack {
178 + couponContainer(coupon: coupon)
179 + }
180 + .background(Color.white)
181 + .cornerRadius(30, corners: [.topLeft])
182 + .frame(width:self.uiscreen.width)
183 + .frame(maxHeight: .infinity)
184 + }
185 + .edgesIgnoringSafeArea([.bottom])
186 + .frame(width:self.uiscreen.width)
187 + .frame(maxHeight: .infinity)
188 +// .onAppear {
189 +// // If you want to setup data with model
190 +// couponset = CouponSetItemModel(dictionary: coupon)
191 +//
192 +// }
18 } 193 }
19 } 194 }
195 +#endif
20 196
21 //struct CouponView_Previews: PreviewProvider { 197 //struct CouponView_Previews: PreviewProvider {
22 // static var previews: some View { 198 // static var previews: some View {
......
...@@ -11,8 +11,8 @@ import SwiftUI ...@@ -11,8 +11,8 @@ import SwiftUI
11 @available(iOS 13.0.0, *) 11 @available(iOS 13.0.0, *)
12 @objc public class CouponViewInterface : NSObject { 12 @objc public class CouponViewInterface : NSObject {
13 13
14 - @objc(couponViewController:) static public func couponViewController(parentView: UIView?) -> UIViewController { 14 + @objc(couponViewController::) static public func couponViewController(parentView: UIView?, coupon: Dictionary<String, Any>) -> UIViewController {
15 - return UIHostingController(rootView: CouponView(parentView: parentView!)) 15 + return UIHostingController(rootView: CouponView(parentView: parentView!, coupon: coupon))
16 } 16 }
17 17
18 } 18 }
......
...@@ -11,7 +11,6 @@ import SwiftUI ...@@ -11,7 +11,6 @@ import SwiftUI
11 import Combine 11 import Combine
12 import Foundation 12 import Foundation
13 import UIKit 13 import UIKit
14 -import WarplySDKFrameworkIOS
15 14
16 15
17 class CouponSetItemModel { 16 class CouponSetItemModel {
...@@ -40,12 +39,25 @@ class CouponSetItemModel { ...@@ -40,12 +39,25 @@ class CouponSetItemModel {
40 39
41 let dateFormatter = DateFormatter() 40 let dateFormatter = DateFormatter()
42 dateFormatter.dateFormat = "yyyy-MM-dd hh:mm" 41 dateFormatter.dateFormat = "yyyy-MM-dd hh:mm"
43 - let date = dateFormatter.date(from: expirationString ?? "") 42 + if let date = dateFormatter.date(from: expirationString ?? "") {
44 - dateFormatter.dateFormat = "dd/MM/yyyy" 43 + dateFormatter.dateFormat = "dd/MM/yyyy"
45 - let resultString = dateFormatter.string(from: date!) 44 + let resultString = dateFormatter.string(from: date)
46 - self.expiration = resultString 45 + self.expiration = resultString
46 + } else {
47 + self.expiration = ""
48 + }
49 +
47 50
48 } 51 }
52 +
53 + var asDictionary : [String:Any] {
54 + let mirror = Mirror(reflecting: self)
55 + let dict = Dictionary(uniqueKeysWithValues: mirror.children.lazy.map({ (label:String?, value:Any) -> (String, Any)? in
56 + guard let label = label else { return nil }
57 + return (label, value)
58 + }).compactMap { $0 })
59 + return dict
60 + }
49 } 61 }
50 62
51 class CouponDataModel { 63 class CouponDataModel {
...@@ -382,6 +394,7 @@ extension GiftsView { ...@@ -382,6 +394,7 @@ extension GiftsView {
382 struct couponsContainer: View { 394 struct couponsContainer: View {
383 @State var coupons:Array<CouponSetItemModel> = [] 395 @State var coupons:Array<CouponSetItemModel> = []
384 @State var title:String = "" 396 @State var title:String = ""
397 + @State var parentView:UIView
385 398
386 var uiscreen = UIScreen.main.bounds 399 var uiscreen = UIScreen.main.bounds
387 400
...@@ -401,7 +414,7 @@ extension GiftsView { ...@@ -401,7 +414,7 @@ extension GiftsView {
401 HStack(alignment: .center) { 414 HStack(alignment: .center) {
402 ForEach(Array(coupons.enumerated()), id: \.offset) { index, item in 415 ForEach(Array(coupons.enumerated()), id: \.offset) { index, item in
403 416
404 - couponItemView(item: item, isFirst: index == 0, isLast: index == (coupons.count-1)) 417 + couponItemView(item: item, isFirst: index == 0, isLast: index == (coupons.count-1), parentView: parentView)
405 } 418 }
406 419
407 } 420 }
...@@ -419,13 +432,21 @@ extension GiftsView { ...@@ -419,13 +432,21 @@ extension GiftsView {
419 var item: CouponSetItemModel 432 var item: CouponSetItemModel
420 var isFirst: Bool 433 var isFirst: Bool
421 var isLast: Bool 434 var isLast: Bool
435 + var parentView: UIView
422 436
423 var uiscreen = UIScreen.main.bounds 437 var uiscreen = UIScreen.main.bounds
424 438
425 var body: some View { 439 var body: some View {
426 Button { 440 Button {
427 // GiftItem Action 441 // GiftItem Action
428 - print("GiftItem tapped!") 442 + let tempItem = item.asDictionary
443 + let instanceOfMyApi = MyApi()
444 + let couponViewController = instanceOfMyApi.openCoupon(parentView, coupon: tempItem)!
445 + couponViewController.view.tag = 2
446 +// addChild(couponsViewController)
447 + couponViewController.view.frame = parentView.frame
448 + parentView.addSubview(couponViewController.view)
449 + couponViewController.didMove(toParent: UIHostingController(rootView: self))
429 } label: { 450 } label: {
430 451
431 HStack(alignment: .center) { 452 HStack(alignment: .center) {
...@@ -547,7 +568,7 @@ struct GiftsView: View { ...@@ -547,7 +568,7 @@ struct GiftsView: View {
547 } 568 }
548 569
549 if (coupons.count) > 0 { 570 if (coupons.count) > 0 {
550 - couponsContainer(coupons: coupons, title: "ΚΟΥΠΟΝΙΑ" ) 571 + couponsContainer(coupons: coupons, title: "ΚΟΥΠΟΝΙΑ", parentView: parentView )
551 } 572 }
552 573
553 } 574 }
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 - (void) setToStage; 16 - (void) setToStage;
17 - (void) setLang:(NSString*) lang; 17 - (void) setLang:(NSString*) lang;
18 - (UIViewController *) openCoupons:(UIView*) parentView; 18 - (UIViewController *) openCoupons:(UIView*) parentView;
19 -- (UIViewController *) openCoupon:(UIView*) parentView; 19 +- (UIViewController *) openCoupon:(UIView*) parentView coupon:(NSDictionary*) coupon;
20 - (UIViewController *) openGifts:(UIView*) parentView; 20 - (UIViewController *) openGifts:(UIView*) parentView;
21 - (UIViewController *) openWallet:(UIView*) parentView; 21 - (UIViewController *) openWallet:(UIView*) parentView;
22 - (UIViewController *) openMoreForYou:(UIView*) parentView; 22 - (UIViewController *) openMoreForYou:(UIView*) parentView;
......
...@@ -52,9 +52,9 @@ NSString *LANG; ...@@ -52,9 +52,9 @@ NSString *LANG;
52 return couponsViewController; 52 return couponsViewController;
53 } 53 }
54 54
55 -- (UIViewController *) openCoupon:(UIView*) parentView{ 55 +- (UIViewController *) openCoupon:(UIView*) parentView coupon:(NSDictionary*) coupon {
56 56
57 - UIViewController *couponViewController = [CouponViewInterface couponViewController:parentView]; 57 + UIViewController *couponViewController = [CouponViewInterface couponViewController:parentView :coupon];
58 return couponViewController; 58 return couponViewController;
59 } 59 }
60 60
......