vds_ios/VDS/Components/Tooltip/TrailingTooltipLabel.swift
Matt Bruce e82a94304c refactored to remove combine for local updater, replaced with setNeedsUpdate
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-05-08 11:51:10 -05:00

120 lines
4.0 KiB
Swift

//
// TrailingTooltipLabel.swift
// VDS
//
// Created by Matt Bruce on 4/14/23.
//
import Foundation
import UIKit
import Combine
@objc(VDSTrailingTooltipLabel)
open class TrailingTooltipLabel: View, TooltipLaunchable {
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private let tooltipAction = PassthroughSubject<Void, Never>()
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
open var label = Label()
open var labelText: String? { didSet { setNeedsUpdate() }}
open var labelAttributes: [any LabelAttributeModel]? { didSet { setNeedsUpdate() } }
open var labelTextStyle: TextStyle = .defaultStyle { didSet { setNeedsUpdate() } }
open var labelTextPosition: TextPosition = .left { didSet { setNeedsUpdate() } }
public lazy var textColorConfiguration: AnyColorable = {
label.textColorConfiguration
}() { didSet { setNeedsUpdate() }}
open var tooltipCloseButtonText: String = "Close" { didSet { setNeedsUpdate() } }
open var tooltipTitle: String = "" { didSet { setNeedsUpdate() } }
open var tooltipContent: String = "" { didSet { setNeedsUpdate() } }
open var tooltipYOffset: CGFloat = 0 { didSet { setNeedsUpdate() } }
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open override func setup() {
super.setup()
addSubview(label)
label.pinToSuperView()
//create the tooltip click event
tooltipAction.sink { [weak self] in
guard let self else { return }
self.presentTooltip(surface: self.surface,
title: self.tooltipTitle,
content: self.tooltipContent,
closeButtonText: self.tooltipCloseButtonText)
}.store(in: &subscribers)
}
open override func updateView() {
super.updateView()
label.text = labelText
label.textStyle = labelTextStyle
label.textPosition = labelTextPosition
label.attributes = labelAttributes
label.surface = surface
label.disabled = disabled
//add tooltip
if let labelText, !labelText.isEmpty, !tooltipTitle.isEmpty, !tooltipContent.isEmpty {
label.addTooltip(model: .init(surface: surface, closeButtonText: tooltipCloseButtonText, title: tooltipTitle, content: tooltipContent))
}
}
}
extension Label {
public struct TooltipModel {
public var surface: Surface
public var closeButtonText: String
public var title: String
public var content: String
public init(surface: Surface = .light, closeButtonText: String = "Close", title: String, content: String) {
self.surface = surface
self.closeButtonText = closeButtonText
self.title = title
self.content = content
}
}
public func addTooltip(model: TooltipModel) {
var newAttributes: [any LabelAttributeModel] = []
if let attributes {
attributes.forEach { attribute in
if type(of: attribute) != TooltipLabelAttribute.self {
newAttributes.append(attribute)
}
}
}
if let text = text, !text.isEmpty {
let tooltip = TooltipLabelAttribute(surface: surface,
closeButtonText: model.closeButtonText,
title: model.title,
content: model.content)
newAttributes.append(tooltip)
}
if !newAttributes.isEmpty {
attributes = newAttributes
}
}
}