ViewController.swift 4.42 KB
Newer Older
1 2 3
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
Andre Natal's avatar
Andre Natal committed
4 5 6

import WebKit

7
class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate {
Andre Natal's avatar
Andre Natal committed
8 9
    var webView: WKWebView?
    var recorder: Recorder!
10
    var orientation: UIInterfaceOrientationMask!
Andre Natal's avatar
Andre Natal committed
11 12
    var browserViewController: UIViewController? = nil

13
    @IBOutlet weak var labelStatus: UILabel!
14
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
15

Andre Natal's avatar
Andre Natal committed
16 17 18
    override func loadView() {
        super.loadView()
        
19 20 21
        if !Reachability.isConnectedToNetwork(){
            labelStatus.text = "Internet Connection not available!"
        } else {
22
            self.activityIndicatorView.isHidden = false
23 24
            // create the webview and load the commonvoice website in it
            webView = WKWebView(frame: self.view.frame)
25 26
            webView?.isHidden = true
            webView?.navigationDelegate = self
27 28 29
            webView?.configuration.userContentController.add(self, name: "scriptHandler")
            webView?.scrollView.isScrollEnabled = false
            self.view.addSubview(webView!)
Andre Natal's avatar
Andre Natal committed
30
            let url = URL(string: "https://voice.mozilla.org/")
31 32 33 34 35
            let request = URLRequest(url: url!)
            webView?.load(request)
            // start the recorder object and ask permission to capture
            recorder = Recorder()
            recorder.webView = webView!
Andre Natal's avatar
Andre Natal committed
36 37
            let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            browserViewController = mainStoryboard.instantiateViewController(withIdentifier: "browser")
38
        }
Andre Natal's avatar
Andre Natal committed
39 40 41
    }
    
    public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
42 43 44 45 46 47 48 49
        let msg = message.body as! String
        switch msg {
            case "startCapture":
                recorder.startRecording()
            case "stopCapture":
                recorder.stopRecording()
            case "playCapture":
                recorder.playCapture()
50
            case "stopPlaying":
51
                recorder.stopPlayingCapture()
52 53
            case "openSettings":
                UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
54 55 56 57 58 59
            case "lockportrait":
                orientation = [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.portraitUpsideDown]
            case "locklandscape":
                orientation = UIInterfaceOrientationMask.landscape
            case "unlockall":
                orientation = UIInterfaceOrientationMask.all
60 61
            default :
                break
Andre Natal's avatar
Andre Natal committed
62 63
        }
    }
Michael Bebenita's avatar
Michael Bebenita committed
64 65 66 67
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
Andre Natal's avatar
Andre Natal committed
68
    
69 70 71 72 73
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
    {
        labelStatus.text = "Error loading the webapp"
    }
    
74 75 76 77 78 79 80 81
    override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
        if (orientation != nil) {
            return orientation
        }
        else {
            return  [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.portraitUpsideDown]
        }
    }
Andre Natal's avatar
Andre Natal committed
82
    
83 84 85 86
    func webView(_ webView: WKWebView,
                 didFinish navigation: WKNavigation!) {
        webView.isHidden = false
        self.activityIndicatorView.isHidden = true
87 88
        // register javascript function to open settings page
        webView.evaluateJavaScript("window.vcopensettings = function () { window.webkit.messageHandlers['scriptHandler'].postMessage('openSettings'); }")
89 90
    }
    
Andre Natal's avatar
Andre Natal committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping ((WKNavigationActionPolicy) -> Void)) {
        if (navigationAction.navigationType == WKNavigationType.linkActivated){
            print(navigationAction.request.url?.relativeString as Any)
            decisionHandler(WKNavigationActionPolicy.cancel)
            self.present(browserViewController!, animated: true, completion: nil)
            (browserViewController as! BrowserViewController).setUrl(url: (navigationAction.request.url?.absoluteString)!)
        } else {
            decisionHandler(WKNavigationActionPolicy.allow)
        }
    }
    

    @IBAction func closeBrowserView() {
        let prefs = UserDefaults.standard
        prefs.setValue(0, forKey: "userDetails")
    }
Andre Natal's avatar
Andre Natal committed
107
}