Skip to content

Commit

Permalink
- BottomButtonBar: adapt layout dynamically to size class, placing th…
Browse files Browse the repository at this point in the history
…e promptText above the buttons in any but regular horizontal class size and stretching the buttons to fill the space beneath it
  • Loading branch information
felix-schwarz committed Aug 29, 2023
1 parent 1932993 commit bf5a832
Showing 1 changed file with 69 additions and 18 deletions.
87 changes: 69 additions & 18 deletions ownCloudAppShared/Client/User Interface/BottomButtonBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ open class BottomButtonBar: ThemeCSSView {
}
}

open var promptText: String?
open var hasCancelButton: Bool

var activityIndicator: UIActivityIndicatorView?
var showActivityIndicatorWhileModalActionRunning = true
open var modalActionRunning: Bool = false {
Expand Down Expand Up @@ -77,6 +80,7 @@ open class BottomButtonBar: ThemeCSSView {

public init(prompt: String? = nil, selectButtonTitle: String, cancelButtonTitle: String? = "Cancel".localized, hasCancelButton: Bool, selectAction: UIAction?, cancelAction: UIAction?) {
self.selectButtonTitle = selectButtonTitle
self.hasCancelButton = hasCancelButton

super.init()

Expand All @@ -93,9 +97,6 @@ open class BottomButtonBar: ThemeCSSView {
selectButton.setContentCompressionResistancePriority(.required, for: .vertical)
cancelButton.setContentCompressionResistancePriority(.required, for: .vertical)

var constraints: [NSLayoutConstraint] = []
var leadingButtonAnchor = selectButton.leadingAnchor

var selectButtonConfig = UIButton.Configuration.borderedProminent()
selectButtonConfig.title = selectButtonTitle
selectButtonConfig.cornerStyle = .large
Expand All @@ -114,42 +115,92 @@ open class BottomButtonBar: ThemeCSSView {
}

addSubview(cancelButton)

leadingButtonAnchor = cancelButton.leadingAnchor

constraints.append(contentsOf: [
cancelButton.trailingAnchor.constraint(equalTo: selectButton.leadingAnchor, constant: -15),
cancelButton.centerYAnchor.constraint(equalTo: selectButton.centerYAnchor)
])
}

promptText = prompt
promptLabel.text = prompt

addSubview(selectButton)
addSubview(promptLabel)
addSubview(bottomSeparatorLine)

constraints.append(contentsOf: [
promptLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20),
promptLabel.trailingAnchor.constraint(lessThanOrEqualTo: leadingButtonAnchor, constant: -20),
promptLabel.centerYAnchor.constraint(equalTo: selectButton.centerYAnchor),
updateLayout()
}

var barConstraints: [NSLayoutConstraint]? {
willSet {
if let barConstraints {
NSLayoutConstraint.deactivate(barConstraints)
}
}
didSet {
if let barConstraints {
NSLayoutConstraint.activate(barConstraints)
}
}
}

func updateLayout() {
var constraints: [NSLayoutConstraint] = []
let isHorizontalLayout = (traitCollection.horizontalSizeClass == .regular) || (promptText == nil)

selectButton.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -20),
selectButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 20),
selectButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -20),
if isHorizontalLayout {
let leadingButtonAnchor = hasCancelButton ? cancelButton.leadingAnchor : selectButton.leadingAnchor

constraints.append(contentsOf: [
promptLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20),
promptLabel.trailingAnchor.constraint(lessThanOrEqualTo: leadingButtonAnchor, constant: -20),
promptLabel.centerYAnchor.constraint(equalTo: selectButton.centerYAnchor),

selectButton.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -20),
selectButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 20),
selectButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -20)
])
} else {
constraints.append(contentsOf: [
promptLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20),
promptLabel.trailingAnchor.constraint(lessThanOrEqualTo: safeAreaLayoutGuide.trailingAnchor, constant: -20),
promptLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 10),

selectButton.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20).with(priority: .defaultHigh),
selectButton.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -20),
selectButton.topAnchor.constraint(equalTo: promptLabel.bottomAnchor, constant: 10),
selectButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -20)
])

if hasCancelButton {
constraints.append(
cancelButton.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20)
)
}
}

if hasCancelButton {
constraints.append(contentsOf: [
cancelButton.trailingAnchor.constraint(equalTo: selectButton.leadingAnchor, constant: -15),
cancelButton.centerYAnchor.constraint(equalTo: selectButton.centerYAnchor)
])
}

constraints.append(contentsOf: [
bottomSeparatorLine.leftAnchor.constraint(equalTo: leftAnchor),
bottomSeparatorLine.rightAnchor.constraint(equalTo: rightAnchor),
bottomSeparatorLine.topAnchor.constraint(equalTo: topAnchor),
bottomSeparatorLine.heightAnchor.constraint(equalToConstant: 1)
])

NSLayoutConstraint.activate(constraints)
barConstraints = constraints
}

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

open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

updateLayout()
}
}

extension ThemeCSSSelector {
Expand Down

0 comments on commit bf5a832

Please sign in to comment.