CampaignWebview.swift 3.06 KB
//
//  CampaignWebview.swift
//  WarplySDKFrameworkIOS
//
//  Created by Manos Chorianopoulos on 20/4/22.
//


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


extension CampaignWebview {
    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)
                }
                Spacer().frame(width: 0)
                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)
                    .offset(x: -self.uiscreen.height * 0.0125)
            }
            .frame(maxWidth: .infinity)
            .padding(.horizontal)
            .padding(.vertical, 10)
        }
    }
    
    struct WebView: UIViewRepresentable {
     
        var url: String
     
        func makeUIView(context: Context) -> WKWebView {
            return WKWebView()
        }
     
        func updateUIView(_ webView: WKWebView, context: Context) {
            var request = URLRequest(url: URL(string: url)!)
            request.cachePolicy = .reloadRevalidatingCacheData
            webView.load(request)
        }
    }
 
}

@available(iOS 13.0.0, *)
struct CampaignWebview: View {
    var parentView: UIView
    var campaign: String
    
    var uiscreen = UIScreen.main.bounds
    
    func goBack(){
        for subview in parentView.subviews {
            if(subview.tag == 6) {
                subview.removeFromSuperview()
            }
        }
    }
    
    var body: some View {
        VStack {
            headerView(goBack: goBack)
            
            VStack {
                WebView(url: campaign)
             }
            .background(
                LinearGradient(gradient: Gradient(colors: [Color(red: 0.06, green: 0.67, blue: 0.84), Color(red: 0.47, green: 0.75, blue: 0.43)]), startPoint: .top, endPoint: .bottom)
            )
            .cornerRadius(30, corners: [.topLeft])
            .frame(width:self.uiscreen.width)
            .frame(maxHeight: .infinity)
        }
        .edgesIgnoringSafeArea([.bottom])
        .frame(width:self.uiscreen.width)
        .frame(maxHeight: .infinity)
    }
}
#endif

//@available(iOS 13.0.0, *)
//struct CampaignWebview_Previews: PreviewProvider {
//    static var previews: some View {
//
//        CampaignWebview()
//
//    }
//}