CampaignWebviewViewController.swift 4.93 KB
//
//  CampaignWebviewViewController.swift
//  swift_example
//
//  Created by Manos Chorianopoulos on 18/5/22.
//

import UIKit
import WebKit
// TODO: UNCOMMENT
//import SwiftEventBus
// TODO: DELETE
import SwiftWarplyFramework

class CampaignWebviewViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
    
    @IBOutlet weak var webview: WKWebView!
    
    public var campaignUrl: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var campaignUrl = campaignUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print("Webview url: " + (campaignUrl ?? ""))
        
        self.hidesBottomBarWhenPushed = true

        setBackButton()
        
        webview.navigationDelegate = self
        if let url = URL(string: campaignUrl ?? "") {
            webview.load(URLRequest(url: url))
            webview.allowsBackForwardNavigationGestures = true
        }
        
        
        webview.configuration.userContentController.add(self, name: "Cosmote")
        
    }
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        print("=== event callback ===")
        print(message.name, message.body)
        print("=== event callback ===")
        print(message.body)
        
        
        if let event = message.body as? String {
            let eventArray = event.split(separator: ":")
            
            if (eventArray[0] == "event") {
                if (eventArray[1] == "closeArtwork") {
                    print("=== event closeArtwork ===")
                    self.navigationController?.popViewController(animated: true)
                    
                } else if (eventArray[1] == "addUserTag") {
                    let questionnaireEvent = swiftApi.QuestionnaireEventModel()
                    questionnaireEvent._name = String(eventArray[1])
                    if (eventArray.count > 2) {
                        questionnaireEvent._parameter = String(eventArray[2])
                        swiftApi().setUserTag(String(eventArray[2]))
                    } else {
                        questionnaireEvent._parameter = ""
                        swiftApi().setUserTag("")
                    }
//                    if ((eventArray[2] != nil) && (eventArray[2] != "")) {
//                        questionnaireEvent._parameter = String(eventArray[2])
//                        swiftApi().setUserTag(String(eventArray[2]))
//                    } else {
//                        questionnaireEvent._parameter = ""
//                        swiftApi().setUserTag("")
//                    }
                    
                    // TODO: UNCOMMENT
//                    SwiftEventBus.post("questionnaire", sender: questionnaireEvent)
                    
                    // swiftApi().setUserTag(String(eventArray[2]))
                    // self.navigationController?.popViewController(animated: true)
                    
                } else if (eventArray[1] == "couponRetrieved") {
                    let couponEvent = swiftApi.CouponEventModel()
                    // TODO: UNCOMMENT
//                    SwiftEventBus.post("coupon_retrieved", sender: couponEvent)
                    
                } else if (eventArray[2] == "serviceDisabled") {
                    let pacingService = swiftApi.WarplyPacingEventModel()
                    pacingService._isEnabled = false
                    // TODO: UNCOMMENT
//                    SwiftEventBus.post("pacing", sender: pacingService)
                    
                } else if (eventArray[2] == "serviceEnabled") {
                    let pacingService = swiftApi.WarplyPacingEventModel()
                    pacingService._isEnabled = true
                    // TODO: UNCOMMENT
//                    SwiftEventBus.post("pacing", sender: pacingService)
                    
                } else if (eventArray[2] == "shortcutEnabled") {
                    let pacingEvent = swiftApi.WarplyPacingEventModel()
                    pacingEvent._isVisible = true
                    // TODO: UNCOMMENT
//                    SwiftEventBus.post("pacing", sender: pacingEvent)
                    
                } else if (eventArray[2] == "shortcutDisabled") {
                    let pacingEvent = swiftApi.WarplyPacingEventModel()
                    pacingEvent._isVisible = false
                    // TODO: UNCOMMENT
//                    SwiftEventBus.post("pacing", sender: pacingEvent)
                    
                } else if (eventArray[1] == "request" || eventArray[1] == "response") {
                    print("**************** WARPLY Webview Log START *****************")
                    print(eventArray[2])
                    print("**************** WARPLY Webview Log END *****************")
                }
            }
        }
        
//        webview.evaluateJavaScript("window.asyncResponseFromIos(123)")
    }
    
}