created a TableFooterView to be used and refactored ViewController

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2025-01-20 18:37:11 -06:00
parent aced710388
commit a0adea163f
2 changed files with 59 additions and 5 deletions

View File

@ -14,6 +14,7 @@ class EmployeesViewController: UIViewController {
private let modeSegmentedControl = UISegmentedControl(items: EmployeeServiceMode.allCases.map{ $0.rawValue } ) private let modeSegmentedControl = UISegmentedControl(items: EmployeeServiceMode.allCases.map{ $0.rawValue } )
private let viewModel = EmployeesViewModel() private let viewModel = EmployeesViewModel()
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private var footerView: TableFooterView?
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -89,10 +90,12 @@ class EmployeesViewController: UIViewController {
} }
if let message, !viewModel.isLoading { if let message, !viewModel.isLoading {
let footerView = UILabel() // Lazy initialize footerView if needed
footerView.font = .systemFont(ofSize: 17, weight: .medium) if footerView == nil {
footerView.text = message footerView = TableFooterView(message: message)
footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 150) } else { // Update the message
footerView?.update(message: message)
}
tableView.tableFooterView = footerView tableView.tableFooterView = footerView
} else { } else {
tableView.tableFooterView = nil tableView.tableFooterView = nil

View File

@ -0,0 +1,51 @@
//
// EmptyStateFooterView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 1/20/25.
//
import UIKit
public class TableFooterView: UIView {
/// Label used to show the message
private let messageLabel: UILabel = {
let label = UILabel()
label.textColor = .gray
label.textAlignment = .center
label.font = UIFont.preferredFont(forTextStyle: .body) // Use a text style for Dynamic Type
label.adjustsFontForContentSizeCategory = true // Enable Dynamic Type adjustments
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
init(message: String) {
super.init(frame: .zero)
setupUI()
update(message: message)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// Setup the UI
private func setupUI() {
addSubview(messageLabel)
NSLayoutConstraint.activate([
messageLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
messageLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
messageLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16)
])
}
/// Updates the Current Message
/// - Parameter message: message to show
public func update(message: String) {
messageLabel.text = message
}
}