-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryViewController.swift
75 lines (61 loc) · 2.51 KB
/
RepositoryViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// RepositoryViewController.swift
// GithubQuery
//
// Created by leafy on 2016/12/21.
// Copyright © 2016年 leafy. All rights reserved.
//
import UIKit
import WebKit
class RepositoryViewController: UIViewController {
fileprivate var webView: WKWebView?
fileprivate var urlRequest: URLRequest?
fileprivate var progressView: UIProgressView?
fileprivate var timer: Timer?
var repository: Repository?
override func viewDidLoad() {
super.viewDidLoad()
title = repository?.name ?? ""
view.backgroundColor = UIColor.white
let configuration = WKWebViewConfiguration()
webView = WKWebView(frame: view.bounds, configuration: configuration)
webView?.navigationDelegate = self
webView?.backgroundColor = UIColor.white
let url = URL(string: (repository?.html_url)!)
urlRequest = URLRequest(url: url!)
view.addSubview(webView!)
progressView = UIProgressView(frame: CGRect(origin: view.bounds.origin, size: CGSize(width: view.bounds.width, height: 2.0)))
progressView?.progress = 0
view.addSubview(progressView!)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
_ = webView?.load(urlRequest!)
}
@objc fileprivate func updateProgress() {
progressView?.progress = Float((webView?.estimatedProgress)!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension RepositoryViewController: WKNavigationDelegate, WKUIDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
// print("***Start webView: \(webView), navigation: \(navigation)")
progressView?.isHidden = false
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// print("***Finish webView: \(webView), navigation: \(navigation)")
progressView?.isHidden = true
timer?.invalidate()
timer = nil
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// print("***Fail webView: \(webView), navigation: \(navigation)")
progressView?.isHidden = true
timer?.invalidate()
timer = nil
}
}