125 lines
3.3 KiB
Swift

//
// ErrorMessageView.swift
// Locations
//
// Created by Javier Cicchelli on 12/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import UIKit
class ErrorMessageView: UIView {
// MARK: Typealiases
typealias OnRetryClosure = () -> Void
// MARK: Properties
var onRetry: OnRetryClosure?
// MARK: Outlets
private lazy var stack: UIStackView = {
let stack = UIStackView()
stack.alignment = .center
stack.axis = .vertical
stack.distribution = .fill
stack.spacing = 32
stack.translatesAutoresizingMaskIntoConstraints = false
return stack
}()
private lazy var title = {
let title = UILabel()
title.font = .preferredFont(forTextStyle: .largeTitle)
title.numberOfLines = 0
title.lineBreakMode = .byWordWrapping
title.text = "Some error title goes in here..."
title.textAlignment = .center
title.translatesAutoresizingMaskIntoConstraints = false
return title
}()
private lazy var message = {
let message = UILabel()
message.font = .preferredFont(forTextStyle: .body)
message.lineBreakMode = .byWordWrapping
message.numberOfLines = 0
message.text = "Some long, descriptive, explanatory error message goes in here..."
message.textAlignment = .center
message.textColor = .secondaryLabel
message.translatesAutoresizingMaskIntoConstraints = false
return message
}()
private lazy var retry = {
let retry = UIButton()
retry.backgroundColor = .red
retry.translatesAutoresizingMaskIntoConstraints = false
retry.layer.borderColor = UIColor.red.cgColor
retry.layer.borderWidth = 1
retry.layer.cornerRadius = 5
retry.titleLabel?.font = .preferredFont(forTextStyle: .headline)
retry.addTarget(self, action: #selector(retryPressed), for: .touchUpInside)
retry.setTitle("Try again", for: .normal)
return retry
}()
// MARK: Initialisers
init() {
super.init(frame: .zero)
setupView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - Helpers
private extension ErrorMessageView {
// MARK: Functions
func setupView() {
stack.addArrangedSubview(title)
stack.addArrangedSubview(message)
stack.addArrangedSubview(retry)
stack.setCustomSpacing(160, after: message)
backgroundColor = .clear
translatesAutoresizingMaskIntoConstraints = false
addSubview(stack)
NSLayoutConstraint.activate([
retry.heightAnchor.constraint(equalToConstant: 44),
retry.leadingAnchor.constraint(equalTo: stack.leadingAnchor),
retry.trailingAnchor.constraint(equalTo: stack.trailingAnchor),
bottomAnchor.constraint(equalTo: stack.bottomAnchor),
leadingAnchor.constraint(equalTo: stack.leadingAnchor),
topAnchor.constraint(equalTo: stack.topAnchor),
trailingAnchor.constraint(equalTo: stack.trailingAnchor)
])
}
@objc func retryPressed() {
onRetry?()
}
}