[iOS] add browser navigation toolbar

This commit is contained in:
Aleksey Belouosv 2018-08-02 14:47:57 +03:00 committed by Aleksey Belousov
parent ded49f413f
commit ccc66e8c7f
15 changed files with 150 additions and 50 deletions

View file

@ -4,11 +4,20 @@ final class CatalogWebViewController: WebViewController {
let progressView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
let progressImageView = UIImageView(image: #imageLiteral(resourceName: "ic_24px_spinner"))
let numberOfTasksLabel = UILabel()
let loadingIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
var deeplink: URL?
var statSent = false
var backButton: UIBarButtonItem!
var fwdButton: UIBarButtonItem!
@objc init() {
super.init(url: MWMBookmarksManager.catalogFrontendUrl()!, title: L("routes_and_bookmarks"))!
backButton = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_catalog_back"), style: .plain, target: self, action: #selector(onBack))
fwdButton = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_catalog_fwd"), style: .plain, target: self, action: #selector(onFwd))
backButton.tintColor = .blackSecondaryText()
fwdButton.tintColor = .blackSecondaryText()
backButton.isEnabled = false
fwdButton.isEnabled = false
}
required init?(coder aDecoder: NSCoder) {
@ -26,6 +35,7 @@ final class CatalogWebViewController: WebViewController {
numberOfTasksLabel.translatesAutoresizingMaskIntoConstraints = false
progressImageView.translatesAutoresizingMaskIntoConstraints = false
progressView.translatesAutoresizingMaskIntoConstraints = false
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
numberOfTasksLabel.font = UIFont.medium14()
numberOfTasksLabel.textColor = UIColor.white
numberOfTasksLabel.text = "0"
@ -34,46 +44,36 @@ final class CatalogWebViewController: WebViewController {
progressView.contentView.addSubview(progressImageView)
progressView.contentView.addSubview(numberOfTasksLabel)
view.addSubview(progressView)
loadingIndicator.startAnimating()
view.addSubview(loadingIndicator)
loadingIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
numberOfTasksLabel.centerXAnchor.constraint(equalTo: progressView.centerXAnchor).isActive = true
numberOfTasksLabel.centerYAnchor.constraint(equalTo: progressView.centerYAnchor).isActive = true
progressImageView.centerXAnchor.constraint(equalTo: progressView.centerXAnchor).isActive = true
progressImageView.centerYAnchor.constraint(equalTo: progressView.centerYAnchor).isActive = true
progressView.widthAnchor.constraint(equalToConstant: 48).isActive = true
progressView.heightAnchor.constraint(equalToConstant: 48).isActive = true
if #available(iOS 11, *) {
progressView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 8).isActive = true
progressView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -8).isActive = true
} else {
progressView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8).isActive = true
progressView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8).isActive = true
}
progressView.contentView.addConstraint(NSLayoutConstraint(item: numberOfTasksLabel,
attribute: .centerX,
relatedBy: .equal,
toItem: progressView.contentView,
attribute: .centerX,
multiplier: 1,
constant: 0))
progressView.contentView.addConstraint(NSLayoutConstraint(item: numberOfTasksLabel,
attribute: .centerY,
relatedBy: .equal,
toItem: progressView.contentView,
attribute: .centerY,
multiplier: 1,
constant: 0))
progressView.contentView.addConstraint(NSLayoutConstraint(item: progressImageView,
attribute: .centerX,
relatedBy: .equal,
toItem: progressView.contentView,
attribute: .centerX,
multiplier: 1,
constant: 0))
progressView.contentView.addConstraint(NSLayoutConstraint(item: progressImageView,
attribute: .centerY,
relatedBy: .equal,
toItem: progressView.contentView,
attribute: .centerY,
multiplier: 1,
constant: 0))
let views = ["pv": progressView]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[pv(48)]",
options: .directionLeftToRight,
metrics: [:],
views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[pv(48)]-8-|",
options: .directionLeftToRight,
metrics: [:],
views: views))
rotateProgress()
updateProgress()
navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_catalog_close"), style: .plain, target: self, action: #selector(goBack))
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
fixedSpace.width = 20
self.setToolbarItems([backButton, fixedSpace, fwdButton], animated: true)
self.navigationController?.setToolbarHidden(false, animated: animated)
}
override func viewDidAppear(_ animated: Bool) {
@ -83,6 +83,11 @@ final class CatalogWebViewController: WebViewController {
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setToolbarHidden(true, animated: animated)
}
override func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
@ -95,16 +100,20 @@ final class CatalogWebViewController: WebViewController {
decisionHandler(.cancel);
}
override func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
override func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
if !statSent {
Statistics.logEvent("Bookmarks_Downloaded_Catalogue_open")
statSent = true
}
loadingIndicator.stopAnimating()
backButton.isEnabled = webView.canGoBack
fwdButton.isEnabled = webView.canGoForward
}
override func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
Statistics.logEvent("Bookmarks_Downloaded_Catalogue_error",
withParameters: [kStatError : kStatUnknown])
loadingIndicator.stopAnimating()
}
override func webView(_ webView: WKWebView,
@ -112,6 +121,7 @@ final class CatalogWebViewController: WebViewController {
withError error: Error) {
Statistics.logEvent("Bookmarks_Downloaded_Catalogue_error",
withParameters: [kStatError : kStatUnknown])
loadingIndicator.stopAnimating()
}
func processDeeplink(_ url: URL) {
@ -211,4 +221,12 @@ final class CatalogWebViewController: WebViewController {
progressImageView.layer.add(rotationAnimation, forKey:"rotationAnimation");
}
@objc private func onBack() {
back()
}
@objc private func onFwd() {
forward()
}
}

View file

@ -16,5 +16,7 @@
- (instancetype _Nullable)initWithAuthURL:(NSURL * _Nonnull)url
onSuccessAuth:(MWMStringBlock _Nullable)success
onFailure:(MWMVoidBlock _Nullable)failure;
- (void)forward;
- (void)back;
@end

View file

@ -7,6 +7,7 @@
@property(copy, nonatomic) MWMVoidBlock onFailure;
@property(copy, nonatomic) MWMStringBlock onSuccess;
@property(nonatomic) BOOL authorized;
@property(nonatomic) WKWebView * webView;
@end
@ -59,12 +60,12 @@
UIView * view = self.view;
view.backgroundColor = UIColor.whiteColor;
WKWebView * webView = [[WKWebView alloc] initWithFrame:{}];
webView.navigationDelegate = self;
[view addSubview:webView];
self.webView = [[WKWebView alloc] initWithFrame:{}];
self.webView.navigationDelegate = self;
[view addSubview:self.webView];
webView.translatesAutoresizingMaskIntoConstraints = NO;
webView.autoresizesSubviews = YES;
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
self.webView.autoresizesSubviews = YES;
NSLayoutYAxisAnchor * topAnchor = view.topAnchor;
NSLayoutYAxisAnchor * bottomAnchor = view.bottomAnchor;
NSLayoutXAxisAnchor * leadingAnchor = view.leadingAnchor;
@ -78,17 +79,17 @@
trailingAnchor = safeAreaLayoutGuide.trailingAnchor;
}
[webView.topAnchor constraintEqualToAnchor:topAnchor].active = YES;
[webView.bottomAnchor constraintEqualToAnchor:bottomAnchor].active = YES;
[webView.leadingAnchor constraintEqualToAnchor:leadingAnchor].active = YES;
[webView.trailingAnchor constraintEqualToAnchor:trailingAnchor].active = YES;
[self.webView.topAnchor constraintEqualToAnchor:topAnchor].active = YES;
[self.webView.bottomAnchor constraintEqualToAnchor:bottomAnchor].active = YES;
[self.webView.leadingAnchor constraintEqualToAnchor:leadingAnchor].active = YES;
[self.webView.trailingAnchor constraintEqualToAnchor:trailingAnchor].active = YES;
webView.backgroundColor = UIColor.whiteColor;
self.webView.backgroundColor = UIColor.whiteColor;
if (self.m_htmlText)
[webView loadHTMLString:self.m_htmlText baseURL:self.m_url];
[self.webView loadHTMLString:self.m_htmlText baseURL:self.m_url];
else
[webView loadRequest:[NSURLRequest requestWithURL:self.m_url]];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.m_url]];
}
- (void)viewDidDisappear:(BOOL)animated
@ -136,6 +137,16 @@
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)forward
{
[self.webView goForward];
}
- (void)back
{
[self.webView goBack];
}
#if DEBUG
- (void)webView:(WKWebView *)webView
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

View file

@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_catalog_back.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_catalog_back@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_catalog_back@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 293 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 945 B

View file

@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_catalog_close.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_catalog_close@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_catalog_close@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 397 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 B

View file

@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_catalog_fwd.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_catalog_fwd@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_catalog_fwd@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 889 B