vds_ios/VDS/Components/Pagination/PaginationButton.swift
Matt Bruce 6bf3e19ebe added @objcMembers to all classes
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-07-25 10:53:26 -05:00

113 lines
3.9 KiB
Swift

//
// PaginationButton.swift
// VDS
//
// Created by Bandaru, Krishna Kishore on 05/03/24.
//
import UIKit
import VDSCoreTokens
///This is customised button for Pagination view
@objcMembers
@objc(PaginationButton)
open class PaginationButton: ButtonBase {
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
/// Type of the PaginationButton
private var type: Type = .next
/// Button tint color configuration
private let buttonTintColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite)
/// Button title color configuration
private let buttonTextColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite)
/// Button configuration for iOS 15+
@available(iOS 15.0, *)
private var buttonConfiguration: Button.Configuration {
var configuration = ButtonBase.Configuration.plain()
configuration.imagePadding = VDSLayout.space2X
configuration.imagePlacement = type == .next ? .trailing : .leading
configuration.titleAlignment = type == .next ? .trailing : .leading
configuration.contentInsets = .zero
return configuration
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
/// TextStyle used on the titleLabel.
open override var textStyle: TextStyle { TextStyle.boldBodySmall }
/// UIColor used on the titleLabel text.
open override var textColor: UIColor { buttonTextColorConfiguration.getColor(surface) }
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
init(type: Type) {
self.type = type
super.init()
}
required public init() {
super.init()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
/// Executed on initialization for this View.
open override func initialSetup() {
super.initialSetup()
if #available(iOS 15.0, *) {
configuration = buttonConfiguration
} else {
semanticContentAttribute = type == .next ? .forceRightToLeft : .forceLeftToRight
imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: VDSLayout.space2X)
}
contentHorizontalAlignment = type == .next ? .trailing : .leading
}
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
text = type.title
let color = buttonTintColorConfiguration.getColor(surface)
setImage(type.image(color), for: .normal)
tintColor = color
super.updateView()
}
}
extension PaginationButton {
//--------------------------------------------------
// MARK: - Enum to configure PaginationButton
//--------------------------------------------------
enum `Type` {
case previous, next
var title: String {
switch self {
case .next:
"Next"
case .previous:
"Previous"
}
}
private var imageSize: CGSize { Icon.Size.xsmall.dimensions }
///Image for the configuration type
func image(_ color: UIColor) -> UIImage? {
switch self {
case .previous:
UIImage.image(for: .paginationLeftArrow, color: color, renderingMode: .alwaysTemplate)?.resized(to: imageSize)
case .next:
UIImage.image(for: .paginationRightArrow, color: color, renderingMode: .alwaysTemplate)?.resized(to: imageSize)
}
}
}
}