From 8ecccbcecb5b32ac4802e0abe7d988d22e0c93ce Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 27 Apr 2023 14:47:56 -0500 Subject: [PATCH] refactored this to use new attribute Signed-off-by: Matt Bruce --- .../Tooltip/TrailingTooltipLabel.swift | 80 ++++++++++++++----- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/VDS/Components/Tooltip/TrailingTooltipLabel.swift b/VDS/Components/Tooltip/TrailingTooltipLabel.swift index 206aa413..0d0f8e19 100644 --- a/VDS/Components/Tooltip/TrailingTooltipLabel.swift +++ b/VDS/Components/Tooltip/TrailingTooltipLabel.swift @@ -64,31 +64,67 @@ open class TrailingTooltipLabel: View, TooltipLaunchable { open override func updateView() { super.updateView() - - var attributes: [any LabelAttributeModel] = [] - if let labelAttributes { - attributes.append(contentsOf: labelAttributes) - } - - var updatedLabelText = labelText - - //add the tool tip - if let oldText = updatedLabelText, !tooltipTitle.isEmpty, !tooltipContent.isEmpty { - let tooltipUpdateText = "\(oldText) " //create a little space between the final character and tooltip image - let frame = CGRect(x: 0, y: tooltipYOffset, width: tooltipSize.dimensions.width, height: tooltipSize.dimensions.width) - let color = textColorConfiguration.getColor(self) - let tooltipAttribute = ImageLabelAttribute(location: tooltipUpdateText.count - 2, imageName: "info", frame: frame, tintColor: color) - let tooltipAction = ActionLabelAttribute(location: tooltipUpdateText.count - 3, length: 3, shouldUnderline: false, action: tooltipAction) - updatedLabelText = tooltipUpdateText - attributes.append(tooltipAttribute) - attributes.append(tooltipAction) - } - //set the titleLabel - label.text = updatedLabelText - label.attributes = attributes + + label.text = labelText label.textStyle = labelTextStyle label.textPosition = labelTextPosition label.surface = surface label.disabled = disabled + + //add tooltip + if let labelText, !labelText.isEmpty, !tooltipTitle.isEmpty, !tooltipContent.isEmpty { + //create the model + let model = Label.TooltipModel(surface: surface, + closeButtonText: tooltipCloseButtonText, + size: tooltipSize, + title: tooltipTitle, + content: tooltipContent) + + //add the model + label.addTooltip(model: model) + } + } +} + +extension Label { + public struct TooltipModel { + public var surface: Surface + public var closeButtonText: String + public var size: Tooltip.Size + public var title: String + public var content: String + + public init(surface: Surface = .light, closeButtonText: String = "Close", size: Tooltip.Size = .medium, title: String, content: String) { + self.surface = surface + self.closeButtonText = closeButtonText + self.size = size + 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, + size: model.size, + title: model.title, + content: model.content) + newAttributes.append(tooltip) + } + + if !newAttributes.isEmpty { + attributes = newAttributes + } } }