AllGiftsView.swift 7.26 KB
//
//  AllGiftsView.swift
//  WarplySDKFrameworkIOS
//
//  Created by Manos Chorianopoulos on 27/4/22.
//


#if canImport(SwiftUI)
import SwiftUI
import Combine
import Foundation
import UIKit


extension AllGiftsView {
    struct headerView: View {
        var goBack: () -> ()
        
        var uiscreen = UIScreen.main.bounds
        
        var body: some View {
            HStack(alignment: .center) {
                Button {
                    // Button Action
                    print("Back Button tapped!")
                    goBack()
                } label: {
                    Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: self.uiscreen.height * 0.025, height: self.uiscreen.height * 0.02)
                }
               
                Text("Όλα τα δώρα μου")
                    .fontWeight(.medium)
                    .font(.system(size: 16))
                    .foregroundColor(Color(red: 0.20784313725490197, green: 0.3176470588235294, blue: 0.40784313725490196))
                    .multilineTextAlignment(.center)
                    .frame(maxWidth: .infinity)
                    .padding(.horizontal)
            }
            .frame(maxWidth: .infinity)
            .padding(.horizontal)
            .padding(.vertical, 10)
        }
    }
    
    struct ImageView: View {
        @ObservedObject var imageLoader:UrlImageModel
        @State var width:CGFloat
        @State var isFill:Bool
        
        var uiscreen = UIScreen.main.bounds

        init(withURL url:String , width:CGFloat, isFill:Bool) {
            imageLoader = UrlImageModel(urlString:url)
            self.width = width
            self.isFill = isFill
        }

        var body: some View {
            
            Image(uiImage: imageLoader.image ?? UIImage())
                .resizable()
                .aspectRatio(contentMode: isFill ? .fill : .fit)
                .frame(width: self.width)
                .frame(maxHeight: .infinity)
        }
    }
    
    
    struct giftItemView: View {
        var item: swiftApi.CouponItemModel
        var parentView: UIView
        
        var uiscreen = UIScreen.main.bounds
        
        var body: some View {
            let couponSetData = item.couponset_data
            
            Button {
                // GiftItem Action
                let instanceOfMySwiftApi = swiftApi()
                let couponBarcodeViewController = instanceOfMySwiftApi.openCouponBarcode(parent: parentView, coupon: item)
                couponBarcodeViewController.view.tag = 7
//                addChild(couponsViewController)
                couponBarcodeViewController.view.frame = parentView.frame
                parentView.addSubview(couponBarcodeViewController.view)
//                couponBarcodeViewController.didMove(toParent: UIHostingController(rootView: self))
            } label: {
                HStack(alignment: .center, spacing: 0.0) {
                    
                    ImageView(withURL: couponSetData?.img_preview ?? "", width: self.uiscreen.width * 0.5, isFill: true)
                    
                    VStack(alignment: .leading, spacing: 5.0) {
                        Text(couponSetData?.name ?? "")
                            .fontWeight(.bold)
                            .font(.system(size: 16))
                            .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
                            .multilineTextAlignment(.leading)
                            .padding([.top,.horizontal], 15)
                            .background(Color.white)
                        
                        Text(couponSetData?.short_description ?? "")
                            .fontWeight(.regular)
                            .font(.system(size: 16))
                            .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
                            .multilineTextAlignment(.leading)
                            .padding([.bottom,.horizontal], 15)
                            .background(Color.white)
                            .frame(maxWidth: .infinity, maxHeight: .infinity)
                    }
                    .background(Color.white)
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                }
                .background(Color.white)
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
            .frame(width: self.uiscreen.width * 0.95, height: self.uiscreen.height * 0.17)
            .background(Color.white)
            .cornerRadius(5)
            .shadow(color: Color(red: 0, green: 0, blue: 0, opacity: 0.16), radius: 6, x: 0, y: 3)
        }
    }

    struct couponsContainer: View {
        @State var coupons:Array<swiftApi.CouponItemModel> = []
        @State var parentView:UIView
        
        var uiscreen = UIScreen.main.bounds
        
        var body: some View {
            VStack(alignment: .center, spacing: self.uiscreen.height * 0.03) {

                ForEach(Array(coupons.enumerated()), id: \.offset) { index, item in
                            
                    giftItemView(item: item, parentView: parentView)
                }
            }
            .frame(maxWidth: .infinity)
            .padding(.top, self.uiscreen.height * 0.05)
            .padding(.bottom, self.uiscreen.height * 0.1)
        }
    }
  
}

@available(iOS 13.0.0, *)
struct AllGiftsView: View {
    var parentView: UIView
    
    var coupons:Array<swiftApi.CouponItemModel> = []
    
    var uiscreen = UIScreen.main.bounds
    
    init(parentView: UIView) { //initializer method

        let instanceOfMySwiftApi = swiftApi()
        let couponsData = instanceOfMySwiftApi.getCoupons()

        self.coupons = couponsData
        self.parentView = parentView
    }
    
    func goBack(){
        for subview in parentView.subviews {
            if(subview.tag == 8) {
                subview.removeFromSuperview()
            }
        }
    }
    
    var body: some View {
        VStack {
            headerView(goBack: goBack)
            
            VStack {
                ScrollView(showsIndicators: false) {
                   VStack {
                       
                       if (coupons.count) > 0 {
                           couponsContainer(coupons: coupons, parentView: parentView )
                       }
                       
                    }
                    .frame(width:self.uiscreen.width)
                }
                .cornerRadius(30, corners: [.topLeft])
                .frame(width:self.uiscreen.width)
                .frame(maxHeight: .infinity)
            }
            .frame(width:self.uiscreen.width)
            .frame(maxHeight: .infinity)
            .padding(.top, 5)
            .background(
                Image("coupons_scrollview", bundle: Bundle(for: MyEmptyClass.self))
                    .resizable()
            )
            
        }
        .edgesIgnoringSafeArea([.bottom])
        .frame(width:self.uiscreen.width)
        .frame(maxHeight: .infinity)
    }
}
#endif

//struct AllGiftsView_Previews: PreviewProvider {
//    static var previews: some View {
//        AllGiftsView()
//    }
//}