Skip to content

Commit

Permalink
edits to label color variables definetions
Browse files Browse the repository at this point in the history
  • Loading branch information
noorhashem committed Jun 24, 2020
1 parent 911f098 commit 9c5da0a
Showing 1 changed file with 60 additions and 83 deletions.
143 changes: 60 additions & 83 deletions Extensions/Today/TodayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,62 @@ struct TodayStrings {
}

private struct TodayUX {
static let privateBrowsingColor = UIColor(rgb: 0xcf68ff)
static let backgroundHightlightColor = UIColor(white: 216.0/255.0, alpha: 44.0/255.0)
static let linkTextSize: CGFloat = 9.0
static let labelTextSize: CGFloat = 12.0
static let labelColor = UIColor(rgb: 0x242327)
static let subtitleLabelColor = UIColor(rgb: 0x38383C)
static let imageButtonTextSize: CGFloat = 13.0
static let copyLinkImageWidth: CGFloat = 20
static let margin: CGFloat = 8
static let buttonsHorizontalMarginPercentage: CGFloat = 0.1
static let privateSearchButtonColorBrightPurple = UIColor(red: 117.0/255.0, green: 41.0/255.0, blue: 167.0/255.0, alpha: 1.0)
static let privateSearchButtonColorDarkPurple = UIColor(red: 73.0/255.0, green: 46.0/255.0, blue: 133.0/255.0, alpha: 1.0)
static let privateSearchButtonColorFaintDarkPurple = UIColor(red: 56.0/255.0, green: 51.0/255.0, blue: 114.0/255.0, alpha: 1.0)
static let buttonStackViewSpacing: CGFloat = 30.0
static var labelColor: UIColor {
if #available(iOS 13, *) {
return UIColor(named: "widgetLabelColors") ?? UIColor(rgb: 0x242327)
} else {
return UIColor(rgb: 0x242327)
}
}
static var subtitleLabelColor: UIColor {
if #available(iOS 13, *) {
return UIColor(named: "subtitleLableColor") ?? UIColor(rgb: 0x38383C)
} else {
return UIColor(rgb: 0x38383C)
}
}
}

@objc (TodayViewController)
class TodayViewController: UIViewController, NCWidgetProviding {
var copiedURL: URL?

fileprivate lazy var newTabButton: ImageButtonWithLabel = {
let imageButton = ImageButtonWithLabel()
imageButton.addTarget(self, action: #selector(onPressNewTab), forControlEvents: .touchUpInside)
imageButton.label.text = TodayStrings.NewTabButtonLabel
let button = imageButton.button
button.setImage(UIImage(named: "search-button")?.withRenderingMode(.alwaysOriginal), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
let label = imageButton.label
if #available(iOS 13, *) {
label.textColor = UIColor(named: "widgetLabelColors")
label.tintColor = UIColor(named: "widgetLabelColors")
} else {
label.textColor = TodayUX.labelColor
label.tintColor = TodayUX.labelColor
}
label.textColor = TodayUX.labelColor
label.tintColor = TodayUX.labelColor
label.font = UIFont.systemFont(ofSize: TodayUX.imageButtonTextSize)
imageButton.sizeToFit()
return imageButton
}()

fileprivate lazy var newPrivateTabButton: ImageButtonWithLabel = {
let imageButton = ImageButtonWithLabel()
imageButton.addTarget(self, action: #selector(onPressNewPrivateTab), forControlEvents: .touchUpInside)
imageButton.label.text = TodayStrings.NewPrivateTabButtonLabel
let button = imageButton.button
button.setImage(UIImage(named: "private-search")?.withRenderingMode(.alwaysOriginal), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
let label = imageButton.label
if #available(iOS 13, *) {
label.textColor = UIColor(named: "widgetLabelColors")
label.tintColor = UIColor(named: "widgetLabelColors")
} else {
label.textColor = TodayUX.labelColor
label.tintColor = TodayUX.labelColor
}
label.textColor = TodayUX.labelColor
label.tintColor = TodayUX.labelColor
label.font = UIFont.systemFont(ofSize: TodayUX.imageButtonTextSize)
imageButton.sizeToFit()
return imageButton
}()

fileprivate lazy var openCopiedLinkButton: ButtonWithSublabel = {
let button = ButtonWithSublabel()
button.setTitle(TodayStrings.GoToCopiedLinkLabel, for: .normal)
Expand All @@ -86,20 +83,13 @@ class TodayViewController: UIViewController, NCWidgetProviding {
button.setImage(UIImage(named: "copy_link_icon")?.withRenderingMode(.alwaysOriginal), for: .normal)
button.label.font = UIFont.systemFont(ofSize: TodayUX.labelTextSize)
button.subtitleLabel.font = UIFont.systemFont(ofSize: TodayUX.linkTextSize)
if #available(iOS 13, *) {
button.label.textColor = UIColor(named: "widgetLabelColors")
button.label.tintColor = UIColor(named: "widgetLabelColors")
button.subtitleLabel.textColor = UIColor(named: "subtitleLableColor")
button.subtitleLabel.tintColor = UIColor(named: "subtitleLableColor")
} else {
button.label.textColor = TodayUX.labelColor
button.label.tintColor = TodayUX.labelColor
button.subtitleLabel.textColor = TodayUX.subtitleLabelColor
button.subtitleLabel.tintColor = TodayUX.subtitleLabelColor
}
button.label.textColor = TodayUX.labelColor
button.label.tintColor = TodayUX.labelColor
button.subtitleLabel.textColor = TodayUX.subtitleLabelColor
button.subtitleLabel.tintColor = TodayUX.subtitleLabelColor
return button
}()

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

fileprivate lazy var buttonStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.spacing = 30
stackView.spacing = TodayUX.buttonStackViewSpacing
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!
Expand All @@ -146,30 +136,30 @@ class TodayViewController: UIViewController, NCWidgetProviding {
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 @@ -184,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 @@ -213,49 +203,36 @@ extension UIButton {
func setBackgroundColor(_ color: UIColor, forState state: UIControl.State) {
let colorView = UIView(frame: CGRect(width: 1, height: 1))
colorView.backgroundColor = color

UIGraphicsBeginImageContext(colorView.bounds.size)
if let context = UIGraphicsGetCurrentContext() {
colorView.layer.render(in: context)
}
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

self.setBackgroundImage(colorImage, for: state)
}
}

extension UIButton {
func performGradient(colorOne: UIColor, colorTwo: UIColor, colorThree: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.frame
gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor, colorThree.cgColor]
gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.cornerRadius = self.frame.size.width/2
layer.masksToBounds = true
layer.insertSublayer(gradientLayer, below: self.imageView?.layer)
}
}

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)
Expand All @@ -264,18 +241,18 @@ class ImageButtonWithLabel: UIView {
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 @@ -284,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 @@ -326,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 9c5da0a

Please sign in to comment.