Skip to content

Commit

Permalink
code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
noorhashem committed Jun 24, 2020
1 parent 9c5da0a commit a552d58
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions Extensions/Today/TodayViewController.swift
Expand Up @@ -58,7 +58,7 @@ class TodayViewController: UIViewController, NCWidgetProviding {
imageButton.sizeToFit()
return imageButton
}()

fileprivate lazy var newPrivateTabButton: ImageButtonWithLabel = {
let imageButton = ImageButtonWithLabel()
imageButton.addTarget(self, action: #selector(onPressNewPrivateTab), forControlEvents: .touchUpInside)
Expand All @@ -72,7 +72,7 @@ class TodayViewController: UIViewController, NCWidgetProviding {
imageButton.sizeToFit()
return imageButton
}()

fileprivate lazy var openCopiedLinkButton: ButtonWithSublabel = {
let button = ButtonWithSublabel()
button.setTitle(TodayStrings.GoToCopiedLinkLabel, for: .normal)
Expand All @@ -89,7 +89,7 @@ class TodayViewController: UIViewController, NCWidgetProviding {
button.subtitleLabel.tintColor = TodayUX.subtitleLabelColor
return button
}()

fileprivate lazy var widgetStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
Expand All @@ -98,7 +98,7 @@ class TodayViewController: UIViewController, NCWidgetProviding {
stackView.distribution = UIStackView.Distribution.fillProportionally
return stackView
}()

fileprivate lazy var buttonStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
Expand All @@ -107,59 +107,59 @@ class TodayViewController: UIViewController, NCWidgetProviding {
stackView.distribution = UIStackView.Distribution.fillEqually
return stackView
}()

fileprivate var scheme: String {
guard let string = Bundle.main.object(forInfoDictionaryKey: "MozInternalURLScheme") as? String else {
// Something went wrong/weird, but we should fallback to the public one.
return "firefox"
}
return string
}

override func viewDidLoad() {
super.viewDidLoad()
let widgetView: UIView!
self.extensionContext?.widgetLargestAvailableDisplayMode = .compact

let effectView: UIVisualEffectView

if #available(iOS 13, *) {
effectView = UIVisualEffectView(effect: UIVibrancyEffect.widgetEffect(forVibrancyStyle: .label))
} else {
effectView = UIVisualEffectView(effect: .none)
}

self.view.addSubview(effectView)
effectView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
widgetView = effectView.contentView
buttonStackView.addArrangedSubview(newTabButton)
buttonStackView.addArrangedSubview(newPrivateTabButton)

widgetStackView.addArrangedSubview(buttonStackView)
widgetStackView.addArrangedSubview(openCopiedLinkButton)

widgetView.addSubview(widgetStackView)
widgetStackView.snp.makeConstraints { make in
make.edges.equalTo(widgetView)
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateCopiedLink()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let edge = size.width * TodayUX.buttonsHorizontalMarginPercentage
buttonStackView.layoutMargins = UIEdgeInsets(top: 0, left: edge, bottom: 0, right: edge)
}

func widgetMarginInsets(forProposedMarginInsets defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return .zero
}

func updateCopiedLink() {
UIPasteboard.general.asyncURL().uponQueue(.main) { res in
if let copiedURL: URL? = res.successValue,
Expand All @@ -174,23 +174,23 @@ class TodayViewController: UIViewController, NCWidgetProviding {
}
}
}

// MARK: Button behaviour
@objc func onPressNewTab(_ view: UIView) {
openContainingApp("?private=false")
}

@objc func onPressNewPrivateTab(_ view: UIView) {
openContainingApp("?private=true")
}

fileprivate func openContainingApp(_ urlSuffix: String = "") {
let urlString = "\(scheme)://open-url\(urlSuffix)"
self.extensionContext?.open(URL(string: urlString)!) { success in
log.info("Extension opened containing app: \(success)")
}
}

@objc func onPressOpenClibpoard(_ view: UIView) {
if let url = copiedURL,
let encodedString = url.absoluteString.escape() {
Expand All @@ -216,43 +216,43 @@ extension UIButton {
}

class ImageButtonWithLabel: UIView {

lazy var button = UIButton()
lazy var label = UILabel()

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

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

func performLayout() {
addSubview(button)
addSubview(label)
button.imageView?.contentMode = .scaleAspectFit

button.snp.makeConstraints { make in
make.centerX.equalTo(self)
make.top.equalTo(self.safeAreaLayoutGuide).offset(5)
make.right.greaterThanOrEqualTo(self.safeAreaLayoutGuide).offset(40)
make.left.greaterThanOrEqualTo(self.safeAreaLayoutGuide).inset(40)
make.height.greaterThanOrEqualTo(60)
}

label.snp.makeConstraints { make in
make.top.equalTo(button.snp.bottom).offset(10)
make.leading.trailing.bottom.equalTo(self)
make.height.equalTo(10)
}

label.numberOfLines = 1
label.lineBreakMode = .byWordWrapping
label.textAlignment = .center
}

func addTarget(_ target: AnyObject?, action: Selector, forControlEvents events: UIControl.Event) {
button.addTarget(target, action: action, for: events)
}
Expand All @@ -261,40 +261,40 @@ class ImageButtonWithLabel: UIView {
class ButtonWithSublabel: UIButton {
lazy var subtitleLabel = UILabel()
lazy var label = UILabel()

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

convenience init() {
self.init(frame: .zero)
}

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

fileprivate func performLayout() {
let titleLabel = self.label
self.titleLabel?.removeFromSuperview()
addSubview(titleLabel)

let imageView = self.imageView!
let subtitleLabel = self.subtitleLabel
self.addSubview(subtitleLabel)

imageView.snp.makeConstraints { make in
make.centerY.left.equalTo(10)
make.width.equalTo(TodayUX.copyLinkImageWidth)
}

titleLabel.snp.makeConstraints { make in
make.left.equalTo(imageView.snp.right).offset(10)
make.trailing.top.equalTo(self)
make.height.greaterThanOrEqualTo(12)
}

subtitleLabel.lineBreakMode = .byTruncatingTail
subtitleLabel.snp.makeConstraints { make in
make.bottom.equalTo(self).inset(10)
Expand All @@ -303,7 +303,7 @@ class ButtonWithSublabel: UIButton {
make.height.greaterThanOrEqualTo(10)
}
}

override func setTitle(_ text: String?, for state: UIControl.State) {
self.label.text = text
super.setTitle(text, for: state)
Expand Down

0 comments on commit a552d58

Please sign in to comment.