Skip to content
Merged
40 changes: 40 additions & 0 deletions WooCommerce/Classes/Authentication/WebAuth/LinkBehavior.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Foundation
import WebKit

enum LinkBehavior {
case all
case hostOnly(URL)
case urlOnly(URL)

func handle(navigationAction: WKNavigationAction, for webView: WKWebView) -> WKNavigationActionPolicy {

// We only want to apply this policy for links, not for all resource loads
guard navigationAction.navigationType == .linkActivated && navigationAction.request.url == navigationAction.request.mainDocumentURL else {
return .allow
}

// Should not happen, but future checks will not work if we can't check the URL
guard let navigationURL = navigationAction.request.url else {
return .allow
}

switch self {
case .all:
return .allow
case .hostOnly(let url):
if navigationAction.request.url?.host == url.host {
return .allow
} else {
UIApplication.shared.open(navigationURL)
return .cancel
}
case .urlOnly(let url):
if navigationAction.request.url?.absoluteString == url.absoluteString {
return .allow
} else {
UIApplication.shared.open(navigationURL)
return .cancel
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Foundation
import UIKit
import WordPressShared.WPFontManager

open class NavigationTitleView: UIView {
@objc public let titleLabel = UILabel(frame: defaultTitleFrame)
@objc public let subtitleLabel = UILabel(frame: defaultSubtitleFrame)


// MARK: - UIView's Methods
convenience init() {
self.init(frame: NavigationTitleView.defaultViewFrame)
}

@objc convenience init(title: String?, subtitle: String?) {
self.init()
titleLabel.text = title ?? String()
subtitleLabel.text = subtitle ?? String()
}

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

required public init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setupSubviews()
}


// MARK: - Helpers
fileprivate func setupSubviews() {
titleLabel.font = WPFontManager.systemSemiBoldFont(ofSize: NavigationTitleView.defaultTitleFontSize)
titleLabel.textColor = .white
titleLabel.textAlignment = .center
titleLabel.backgroundColor = .clear
titleLabel.autoresizingMask = .flexibleWidth

subtitleLabel.font = WPFontManager.systemRegularFont(ofSize: NavigationTitleView.defaultSubtitleFontSize)
subtitleLabel.textColor = .white
subtitleLabel.textAlignment = .center
subtitleLabel.backgroundColor = .clear
subtitleLabel.autoresizingMask = .flexibleWidth

backgroundColor = UIColor.clear
autoresizingMask = [.flexibleWidth, .flexibleBottomMargin, .flexibleTopMargin]
clipsToBounds = true

addSubview(titleLabel)
addSubview(subtitleLabel)
}

// MARK: - Static Constants
fileprivate static let defaultViewFrame = CGRect(x: 0.0, y: 0.0, width: 200.0, height: 35.0)

fileprivate static let defaultTitleFontSize = CGFloat(15)
fileprivate static let defaultTitleFrame = CGRect(x: 0.0, y: 0.0, width: 200.0, height: 19.0)

fileprivate static let defaultSubtitleFontSize = CGFloat(10)
fileprivate static let defaultSubtitleFrame = CGRect(x: 0.0, y: 19.0, width: 200.0, height: 16.0)
}
Loading