Skip to content

Instantly share code, notes, and snippets.

@rajuashok
Created June 10, 2020 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajuashok/32c2cba192832f67947c9224a1e48a15 to your computer and use it in GitHub Desktop.
Save rajuashok/32c2cba192832f67947c9224a1e48a15 to your computer and use it in GitHub Desktop.
//
// ActionFooterView.swift
// Alpha
//
// Created by Ian Mendiola on 5/21/20.
// Copyright © 2020 Open Advisers LLC. All rights reserved.
//
import Foundation
import UIKit
protocol ActionFooterViewDelegate {
func didTapActionFooter(_ actionFooter: ActionFooterView)
}
class ActionFooterView: UIView {
let button = DefaultButton(style: .large, shape: .circle)
private let labelValueView = DefaultLabelValueListView()
private var labelValueHeightConstraint: NSLayoutConstraint!
private var labelValueWidthConstraint: NSLayoutConstraint!
private var buttonWidthConstraint: NSLayoutConstraint!
private var buttonHalfWidthConstraint: NSLayoutConstraint!
private var alertLabel = UILabel()
var delegate: ActionFooterViewDelegate?
var label: String? {
didSet {
updateWidthConstraints()
labelValueView.labels = [label]
labelValueView.invalidateIntrinsicContentSize()
}
}
var value: String? {
didSet {
updateWidthConstraints()
labelValueView.values = [value]
labelValueView.invalidateIntrinsicContentSize()
}
}
var actionTitle: String? {
didSet {
button.setTitle(actionTitle, for: .normal)
}
}
var isEnabled: Bool? {
didSet {
button.isEnabled = isEnabled ?? true
}
}
var alert: String? {
didSet {
print("Setting alert")
alertLabel.text = alert
print(alert)
if let _ = alert {
addSubview(alertLabel)
labelValueView.removeFromSuperview()
button.removeFromSuperview()
backgroundColor = UIColor.Alpha.lighterAbyssBlue
disableAutoResizingMaskTranslationForSubviews()
setupSubviews()
} else {
alertLabel.removeFromSuperview()
addSubview(labelValueView)
addSubview(button)
disableAutoResizingMaskTranslationForSubviews()
setupSubviews()
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
layoutMargins = .defaultInsets
backgroundColor = UIColor.Alpha.appBackground
alertLabel.numberOfLines = 0
alertLabel.font = .font(style: .regular, size: .medium)
alertLabel.textColor = UIColor.Alpha.secondaryLabel
button.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
addSubview(button)
labelValueView.direction = .vertical
labelValueView.labelFont = .font(style: .regular, size: .medium)
labelValueView.valueFont = .font(style: .regular, size: .medium)
addSubview(labelValueView)
disableAutoResizingMaskTranslationForSubviews()
setupSubviews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
print("SET INTRINSICE SIZE")
print("INTRINSIC: \(alertLabel.intrinsicContentSize)");
layoutIfNeeded()
print("INTRINSIC: \(alertLabel.intrinsicContentSize)");
var height: CGFloat = 75
if (alertLabel.isDescendant(of: self)) {
height = alertLabel.intrinsicContentSize.height
}
return .init(width: UIView.noIntrinsicMetric, height: height)
}
private func updateWidthConstraints() {
// Activate constraints that layout label views and action button to be
// approximately half the screen if label/value has been provided
let isHalfActive = labelValueView.labels != nil || labelValueView.values != nil
labelValueWidthConstraint.isActive = isHalfActive
buttonWidthConstraint.isActive = !isHalfActive
buttonHalfWidthConstraint.isActive = isHalfActive
}
private func setupSubviews() {
removeConstraints(self.constraints)
print("SETUPSUBVIEWS")
let margins = layoutMarginsGuide
var constraints = [NSLayoutConstraint]()
if alertLabel.isDescendant(of: self) {
constraints.append(contentsOf: [
alertLabel.topAnchor.constraint(equalTo: margins.topAnchor),
// alertLabel.leftAnchor.constraint(equalTo: leftAnchor,
// constant: .mediumInterimSpacing),
// alertLabel.rightAnchor.constraint(equalTo: rightAnchor,
// constant: -.mediumInterimSpacing),
alertLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
alertLabel.widthAnchor.constraint(equalTo: widthAnchor)
])
} else {
// Constraint that allows both action and label value to be layed out
labelValueWidthConstraint = labelValueView.widthAnchor.constraint(equalTo: margins.widthAnchor, multiplier: 0.49)
buttonHalfWidthConstraint = button.widthAnchor.constraint(equalTo: margins.widthAnchor, multiplier: 0.49)
buttonWidthConstraint = button.widthAnchor.constraint(equalTo: margins.widthAnchor)
constraints.append(contentsOf: [
labelValueView.topAnchor.constraint(equalTo:
margins.topAnchor),
labelValueView.leftAnchor.constraint(equalTo: margins.leftAnchor),
button.topAnchor.constraint(equalTo: margins.topAnchor),
button.rightAnchor.constraint(equalTo: margins.rightAnchor),
buttonWidthConstraint,
button.widthAnchor.constraint(lessThanOrEqualTo: margins.widthAnchor),
button.centerYAnchor.constraint(equalTo: labelValueView.centerYAnchor),
])
}
print("ACTIVATE")
NSLayoutConstraint.activate(constraints)
print("ACTIV: \(alertLabel.intrinsicContentSize)")
}
@objc private func didTap(_ button: UIButton) {
delegate?.didTapActionFooter(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment