From 0a3bd5c8348c67a3b47c95eab847c2932bf5f9c5 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Thu, 11 Apr 2019 16:58:53 -0400 Subject: [PATCH 1/2] Updated the string analysis of setText to account for scenarios where an HTML tag is passed as a delimiter. Reoriented the design of the tuple. --- .../Atoms/Views/LabelWithInternalButton.swift | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift index 3810294a..a827c40d 100644 --- a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift +++ b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift @@ -10,7 +10,7 @@ import MVMCore public typealias ActionBlock = () -> Void -private typealias ActionIndiciesTuple = (startIndex: String.Index?, endIndex: String.Index?, revisedText: String?) +private typealias ActionableStringTuple = (front: String?, middle: String?, end: String?) public typealias ActionObjectDelegate = (NSObjectProtocol & MVMCoreActionDelegateProtocol) public typealias ButtonObjectDelegate = (NSObjectProtocol & ButtonDelegateProtocol) public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProtocol & MVMCoreLoadDelegateProtocol & MVMCorePresentationDelegateProtocol & NSObjectProtocol @@ -381,37 +381,41 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt private func setText(_ text: String?, startTag: String?, endTag: String?) { - let actionRange: ActionIndiciesTuple = rangeOfText(text, startTag: startTag, endTag: endTag) + let actionableTuple: ActionableStringTuple = rangeOfText(text, startTag: startTag, endTag: endTag) - if let revisedText = actionRange.revisedText, let startBraceIndex = actionRange.startIndex, let endBraceIndex = actionRange.endIndex { - - frontText = String(revisedText[revisedText.startIndex.. ActionIndiciesTuple { + private func rangeOfText(_ text: String?, startTag: String?, endTag: String?) -> ActionableStringTuple { - var fullText = text ?? "" - var actionRange: ActionIndiciesTuple = (startIndex: nil, endIndex: nil, revisedText: nil) + var actionableTuple: ActionableStringTuple = (front: nil, middle: nil, end: nil) - if let leftBrace = startTag, let rightBrace = endTag, fullText.contains(Character(leftBrace)) && fullText.contains(Character(rightBrace)) { - actionRange.startIndex = fullText.firstIndex(of: Character(leftBrace)) - fullText = fullText.replacingOccurrences(of: leftBrace, with: "") + guard let text = text else { return actionableTuple } + + if let leftTag = startTag, text.contains(leftTag) { - actionRange.endIndex = fullText.firstIndex(of: Character(rightBrace)) - fullText = fullText.replacingOccurrences(of: rightBrace, with: "") + let firstHalf = text.components(separatedBy: leftTag) + actionableTuple.front = firstHalf.first + + if let rightTag = endTag, text.contains(rightTag) { + + let secondHalf = firstHalf[1].components(separatedBy: rightTag) + actionableTuple.middle = secondHalf[0] + actionableTuple.end = secondHalf[1] + } } - - actionRange.revisedText = fullText - return actionRange + + return actionableTuple } @objc public func setActionMap(_ actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?) { From db672f1c21a86636ed8d84439a771e4391682c26 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Fri, 12 Apr 2019 09:53:30 -0400 Subject: [PATCH 2/2] Trimming whitespace to prevent double spaces from occurring. --- MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift index a827c40d..42661311 100644 --- a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift +++ b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift @@ -384,9 +384,9 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt let actionableTuple: ActionableStringTuple = rangeOfText(text, startTag: startTag, endTag: endTag) if let front = actionableTuple.front, let middle = actionableTuple.middle, let end = actionableTuple.end { - frontText = front - actionText = middle - backText = end + frontText = front.trimmingCharacters(in: .whitespaces) + actionText = middle.trimmingCharacters(in: .whitespaces) + backText = end.trimmingCharacters(in: .whitespaces) self.text = getTextFromStringComponents() } else { frontText = text @@ -408,7 +408,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt actionableTuple.front = firstHalf.first if let rightTag = endTag, text.contains(rightTag) { - let secondHalf = firstHalf[1].components(separatedBy: rightTag) actionableTuple.middle = secondHalf[0] actionableTuple.end = secondHalf[1]