<commit_before>//
//  WebViewController.swift
//  MercadoPagoSDK
//
//  Created by <NAME> on 22/5/16.
//  Copyright © 2016 MercadoPago. All rights reserved.
//

import UIKit

class WebViewController: MercadoPagoUIViewController, UIWebViewDelegate {

    var url: URL
    var navBarTitle: String
    let webView: UIWebView
    let forceAddNavBar: Bool
    private var loadingVC: PXLoadingViewController

    init(url: URL, navigationBarTitle: String, forceAddNavBar: Bool = false) {
        self.url = url
        self.navBarTitle = navigationBarTitle
        self.forceAddNavBar = forceAddNavBar
        self.loadingVC = PXLoadingViewController()
        self.webView = UIWebView(frame: .zero)
        super.init(nibName: nil, bundle: nil)
    }

    @objc func close() {
        self.dismiss(animated: true, completion: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadUrl(url)

        self.webView.delegate = self
        self.webView.translatesAutoresizingMaskIntoConstraints = false
        self.webView.backgroundColor = .white
        self.view.addSubview(webView)
        PXLayout.pinLeft(view: webView).isActive = true
        PXLayout.pinRight(view: webView).isActive = true
        PXLayout.pinBottom(view: webView).isActive = true

        if self.navigationController == nil, forceAddNavBar {
            addNavigationBar()
        } else {
            PXLayout.pinTop(view: webView).isActive = true
        }

        self.view.backgroundColor = ThemeManager.shared.getMainColor()
        loadingVC.modalPresentationStyle = .fullScreen
        self.present(loadingVC, animated: false, completion: nil)
    }

    func addNavigationBar() {
        let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: PXLayout.getSafeAreaTopInset(), width: PXLayout.getScreenWidth(), height: PXLayout.NAV_BAR_HEIGHT))
        self.view.addSubview(navBar)

        navBar.isTranslucent = true
        navBar.barTintColor = ThemeManager.shared.whiteColor()
        navBar.tintColor = ThemeManager.shared.navigationBar().tintColor
        navBar.backgroundColor = ThemeManager.shared.getMainColor()
        navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)

        let textAttributes = [NSAttributedString.Key.foregroundColor: ThemeManager.shared.navigationBar().tintColor]
        navBar.titleTextAttributes = textAttributes

        let navItem = UINavigationItem(title: navBarTitle)
        let closeImage = ResourceManager.shared.getImage("modal_close_button")?.ml_tintedImage(with: ThemeManager.shared.navigationBar().getTintColor())
        let closeButton = UIBarButtonItem(image: closeImage, style: .plain, target: self, action: #selector(close))
        navItem.leftBarButtonItem = closeButton
        navBar.setItems([navItem], animated: false)
        PXLayout.put(view: webView, onBottomOf: navBar).isActive = true
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showNavBar()
        trackScreen()
    }

    override func getNavigationBarTitle() -> String {
        return navBarTitle
    }

    func loadUrl(_ url: URL) {
        if let html = HtmlStorage.shared.getHtml(url.absoluteString) {
            webView.loadHTMLString(html, baseURL: nil)
            loadingVC.dismiss(animated: false, completion: nil)
        } else {
            let requestObj = URLRequest(url: url)
            webView.loadRequest(requestObj)
        }
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {
        self.loadingVC.dismiss(animated: false, completion: nil)
    }
}

// MARK: Tracking
extension WebViewController {
    func trackScreen() {
        var properties: [String: Any] = [:]
        properties["url"] = url.absoluteString
        trackScreen(path: TrackingPaths.Screens.getTermsAndCondiontionPath(), properties: properties)
    }
}<commit_msg>Replace UIWebView with WKWebView<commit_after>//
//  WebViewController.swift
//  MercadoPagoSDK
//
//  Created by <NAME> on 22/5/16.
//  Copyright © 2016 MercadoPago. All rights reserved.
//

import UIKit
import WebKit

class WebViewController: MercadoPagoUIViewController {

    var url: URL
    var navBarTitle: String
    let webView: WKWebView
    let forceAddNavBar: Bool
    private var loadingVC: PXLoadingViewController

    init(url: URL, navigationBarTitle: String, forceAddNavBar: Bool = false) {
        self.url = url
        self.navBarTitle = navigationBarTitle
        self.forceAddNavBar = forceAddNavBar
        self.loadingVC = PXLoadingViewController()
        let webConfiguration = WKWebViewConfiguration()
        self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
        super.init(nibName: nil, bundle: nil)
    }

    @objc func close() {
        self.dismiss(animated: true, completion: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.navigationDelegate = self
        loadUrl(url)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.backgroundColor = .white
        view.addSubview(webView)
        PXLayout.pinLeft(view: webView).isActive = true
        PXLayout.pinRight(view: webView).isActive = true
        PXLayout.pinBottom(view: webView).isActive = true

        if self.navigationController == nil, forceAddNavBar {
            addNavigationBar()
        } else {
            PXLayout.pinTop(view: webView).isActive = true
        }

        view.backgroundColor = ThemeManager.shared.getMainColor()
        loadingVC.modalPresentationStyle = .fullScreen
        present(loadingVC, animated: false, completion: nil)
    }

    func addNavigationBar() {
        let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: PXLayout.getSafeAreaTopInset(), width: PXLayout.getScreenWidth(), height: PXLayout.NAV_BAR_HEIGHT))
        self.view.addSubview(navBar)

        navBar.isTranslucent = true
        navBar.barTintColor = ThemeManager.shared.whiteColor()
        navBar.tintColor = ThemeManager.shared.navigationBar().tintColor
        navBar.backgroundColor = ThemeManager.shared.getMainColor()
        navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)

        let textAttributes = [NSAttributedString.Key.foregroundColor: ThemeManager.shared.navigationBar().tintColor]
        navBar.titleTextAttributes = textAttributes

        let navItem = UINavigationItem(title: navBarTitle)
        let closeImage = ResourceManager.shared.getImage("modal_close_button")?.ml_tintedImage(with: ThemeManager.shared.navigationBar().getTintColor())
        let closeButton = UIBarButtonItem(image: closeImage, style: .plain, target: self, action: #selector(close))
        navItem.leftBarButtonItem = closeButton
        navBar.setItems([navItem], animated: false)
        PXLayout.put(view: webView, onBottomOf: navBar).isActive = true
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showNavBar()
        trackScreen()
    }

    override func getNavigationBarTitle() -> String {
        return navBarTitle
    }

    func loadUrl(_ url: URL) {
        if let html = HtmlStorage.shared.getHtml(url.absoluteString) {
            webView.loadHTMLString(html, baseURL: nil)
            loadingVC.dismiss(animated: false, completion: nil)
        } else {
            let requestObj = URLRequest(url: url)
            webView.load(requestObj)
        }
    }
}

extension WebViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        loadingVC.dismiss(animated: false, completion: nil)
    }
}

// MARK: Tracking
extension WebViewController {
    func trackScreen() {
        var properties: [String: Any] = [:]
        properties["url"] = url.absoluteString
        trackScreen(path: TrackingPaths.Screens.getTermsAndCondiontionPath(), properties: properties)
    }
}
{'fraction_non_alphanumeric': 0.09498097926125905, 'fraction_numerical': 0.0024542888697999753, 'mean_word_length': 3.0506958250497016, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7554981', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2324, 'n_words': 481}