Skip to content
This repository has been archived by the owner on Jun 2, 2019. It is now read-only.

Add BrowserErrorView #434

Merged
merged 3 commits into from Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Trust.xcodeproj/project.pbxproj
Expand Up @@ -380,6 +380,7 @@
BB1CC266203BA4440042E56C /* SendMaxAmountTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB1CC265203BA4440042E56C /* SendMaxAmountTest.swift */; };
BB1CC27A203D061B0042E56C /* TrustDocumentPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB1CC279203D061B0042E56C /* TrustDocumentPickerViewController.swift */; };
BB5D6A9E20232EE8000FC5AB /* CurrencyRate+Fee.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5D6A9D20232EE8000FC5AB /* CurrencyRate+Fee.swift */; };
BBB61E122050993A00428BBD /* BrowserErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBB61E112050993A00428BBD /* BrowserErrorView.swift */; };
BBF4F9B72029D0B3009E04C0 /* GasViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBF4F9B62029D0B2009E04C0 /* GasViewModel.swift */; };
CCA4FE331FD3655900749AE4 /* CheckDeviceCoordinatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCA4FE321FD3655900749AE4 /* CheckDeviceCoordinatorTests.swift */; };
CCA4FE361FD4282400749AE4 /* DeviceChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCA4FE351FD4282400749AE4 /* DeviceChecker.swift */; };
Expand Down Expand Up @@ -801,6 +802,7 @@
BB1CC265203BA4440042E56C /* SendMaxAmountTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendMaxAmountTest.swift; sourceTree = "<group>"; };
BB1CC279203D061B0042E56C /* TrustDocumentPickerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustDocumentPickerViewController.swift; sourceTree = "<group>"; };
BB5D6A9D20232EE8000FC5AB /* CurrencyRate+Fee.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CurrencyRate+Fee.swift"; sourceTree = "<group>"; };
BBB61E112050993A00428BBD /* BrowserErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserErrorView.swift; sourceTree = "<group>"; };
BBF4F9B62029D0B2009E04C0 /* GasViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GasViewModel.swift; sourceTree = "<group>"; };
CCA4FE321FD3655900749AE4 /* CheckDeviceCoordinatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckDeviceCoordinatorTests.swift; sourceTree = "<group>"; };
CCA4FE351FD4282400749AE4 /* DeviceChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceChecker.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2197,6 +2199,7 @@
902CAB582051818A00F96FFE /* BookmarkViewCell.xib */,
77B3BF492019247200EEC15A /* BrowserNavigationBar.swift */,
902CAB5A2051835E00F96FFE /* BookmarkViewCell.swift */,
BBB61E112050993A00428BBD /* BrowserErrorView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -2823,6 +2826,7 @@
2923D9B31FDA49F8000CF3F8 /* SkipBackupFilesInitializer.swift in Sources */,
294DFBA01FE0CADE004CEB56 /* TransactionDetailsViewModel.swift in Sources */,
90DDF48520518AE50016E6D4 /* BookmarkViewModel.swift in Sources */,
BBB61E122050993A00428BBD /* BrowserErrorView.swift in Sources */,
29C80D4B1FB51C460037B1E0 /* Decimal.swift in Sources */,
297800521F71FDCF003185C1 /* FormAppearance.swift in Sources */,
77B3BF3C201908ED00EEC15A /* ConfirmCoordinator.swift in Sources */,
Expand Down
42 changes: 37 additions & 5 deletions Trust/Browser/ViewControllers/BrowserViewController.swift
Expand Up @@ -35,20 +35,35 @@ class BrowserViewController: UIViewController {
configuration: self.config
)
webView.allowsBackForwardNavigationGestures = true
webView.translatesAutoresizingMaskIntoConstraints = false
webView.navigationDelegate = self
webView.uiDelegate = self
if isDebug {
webView.configuration.preferences.setValue(true, forKey: Keys.developerExtrasEnabled)
}
return webView
}()

lazy var errorView: BrowserErrorView = {
let errorView = BrowserErrorView()
errorView.translatesAutoresizingMaskIntoConstraints = false
errorView.delegate = self
return errorView
}()

weak var delegate: BrowserViewControllerDelegate?
private let urlParser = BrowserURLParser()

var browserNavBar: BrowserNavigationBar? {
return navigationController?.navigationBar as? BrowserNavigationBar
}
let progressView = UIProgressView(progressViewStyle: .default)

lazy var progressView: UIProgressView = {
let progressView = UIProgressView(progressViewStyle: .default)
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.tintColor = Colors.darkBlue
return progressView
}()

lazy var config: WKWebViewConfiguration = {
return WKWebViewConfiguration.make(for: account, with: sessionConfig, in: self)
Expand All @@ -63,14 +78,12 @@ class BrowserViewController: UIViewController {

super.init(nibName: nil, bundle: nil)

webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
injectUserAgent()

progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.tintColor = Colors.darkBlue
webView.addSubview(progressView)
webView.bringSubview(toFront: progressView)
view.addSubview(errorView)

NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor),
Expand All @@ -82,6 +95,11 @@ class BrowserViewController: UIViewController {
progressView.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
progressView.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
progressView.heightAnchor.constraint(equalToConstant: 3),

errorView.topAnchor.constraint(equalTo: webView.topAnchor),
errorView.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
errorView.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
errorView.bottomAnchor.constraint(equalTo: webView.bottomAnchor),
])
view.backgroundColor = .white
webView.addObserver(self, forKeyPath: Keys.estimatedProgress, options: .new, context: &myContext)
Expand Down Expand Up @@ -126,11 +144,13 @@ class BrowserViewController: UIViewController {

private func goHome() {
guard let url = URL(string: Constants.dappsBrowserURL) else { return }
hideErrorView()
webView.load(URLRequest(url: url))
browserNavBar?.textField.text = url.absoluteString
}

private func reload() {
hideErrorView()
webView.reload()
}

Expand All @@ -147,6 +167,10 @@ class BrowserViewController: UIViewController {
browserNavBar?.goForward.isEnabled = webView.canGoForward
}

private func hideErrorView() {
errorView.isHidden = true
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
guard let change = change else { return }
if context != &myContext {
Expand Down Expand Up @@ -236,7 +260,7 @@ class BrowserViewController: UIViewController {
if error.code == NSURLErrorCancelled {
return
} else {
displayError(error: error)
errorView.show(error: error)
}
}
}
Expand Down Expand Up @@ -264,10 +288,12 @@ extension BrowserViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
refreshURL()
reloadButtons()
hideErrorView()
}

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
reloadButtons()
hideErrorView()
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
Expand Down Expand Up @@ -296,3 +322,9 @@ extension BrowserViewController: WKScriptMessageHandler {
delegate?.didCall(action: action, callbackID: command.id)
}
}

extension BrowserViewController: BrowserErrorViewDelegate {
func didTapReload(_ sender: Button) {
reload()
}
}
73 changes: 73 additions & 0 deletions Trust/Browser/Views/BrowserErrorView.swift
@@ -0,0 +1,73 @@
// Copyright SIX DAY LLC. All rights reserved.

import UIKit

protocol BrowserErrorViewDelegate: class {
func didTapReload(_ sender: Button)
}

class BrowserErrorView: UIView {

weak var delegate: BrowserErrorViewDelegate?

private let topMargin: CGFloat = 120
private let leftMargin: CGFloat = 40
private let buttonTopMargin: CGFloat = 6

lazy var textLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textColor = Colors.gray
label.font = UIFont.systemFont(ofSize: 18)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

lazy var reloadButton: Button = {
let button = Button(size: .normal, style: .borderless)
button.addTarget(self, action: #selector(reloadTapped), for: .touchUpInside)
button.setTitle(NSLocalizedString("browser.reload.button.title", value: "Reload", comment: ""), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.sizeToFit()
return button
}()

init() {
super.init(frame: CGRect.zero)
finishInit()
}

override init(frame: CGRect) {
super.init(frame: frame)
finishInit()
}

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

func show(error: Error) {
self.isHidden = false
textLabel.text = error.localizedDescription
textLabel.textAlignment = .center
textLabel.setNeedsLayout()
}

@objc func reloadTapped() {
delegate?.didTapReload(reloadButton)
}

private func finishInit() {
self.backgroundColor = .white
addSubview(textLabel)
addSubview(reloadButton)
NSLayoutConstraint.activate([
textLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: leftMargin),
textLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -leftMargin),
textLabel.topAnchor.constraint(equalTo: topAnchor, constant: topMargin),

reloadButton.centerXAnchor.constraint(equalTo: textLabel.centerXAnchor),
reloadButton.topAnchor.constraint(equalTo: textLabel.bottomAnchor, constant: buttonTopMargin),
])
}
}