From 93f90bad474984d02eab8383387ec7ee27d5bf3a Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Tue, 7 May 2019 10:48:52 -0400 Subject: [PATCH] corrections made. --- MVMCoreUI/Atoms/Views/Label.swift | 108 +++--------------- .../Atoms/Views/LabelWithInternalButton.swift | 41 +------ 2 files changed, 20 insertions(+), 129 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index 15e5cf1b..ad8a2d2e 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -57,7 +57,6 @@ public typealias ActionBlock = () -> Void public struct ActionableClause { var location: Int? var length: Int? - var actionText: String? var actionBlock: ActionBlock? var range: NSRange { @@ -258,30 +257,19 @@ public typealias ActionBlock = () -> Void } case "actions": guard let actionLabel = label as? Label, - let text = actionLabel.text, let actions = attribute.optionalArrayForKey("actions") else { continue } for case let action as [String: Any] in actions { guard let actionLocation = action["location"] as? Int, - let actionLength = action["length"] as? Int, - let subStringRange = Range(NSRange(location: actionLocation, length: actionLength), in: text) + let actionLength = action["length"] as? Int else { continue } actionLabel.clauses.append(ActionableClause(location: actionLocation, length: actionLength, - actionText: String(text[subStringRange]), - actionBlock: { [weak delegate] in - var willPerform = true - - if let buttonDelegate = (delegate as? MVMCoreUIDelegateObject)?.buttonDelegate, - buttonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) { - willPerform = buttonDelegate.button?(actionLabel, shouldPerformActionWithMap: json, additionalData: additionalData) ?? false - } - - if willPerform { - MVMCoreActionHandler.shared()?.handleAction(with: json, additionalData: additionalData, delegateObject: delegate) - } })) + actionBlock: actionLabel.createActionBlockFrom(actionMap: json, + additionalData: additionalData, + delegate: delegate))) } default: continue @@ -291,6 +279,14 @@ public typealias ActionBlock = () -> Void } } + func createActionBlockFrom(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegate: DelegateObject?) -> ActionBlock { + return { [weak delegate] in + if (delegate as? MVMCoreUIDelegateObject)?.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true { + MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegate) + } + } + } + //------------------------------------------------------ // MARK: - Methods //------------------------------------------------------ @@ -385,9 +381,9 @@ extension Label { } } - // MARK: - Multi-Action Functionality extension Label { + /** Provides an actionable range of text. @@ -398,13 +394,8 @@ extension Label { */ @objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) { - guard let text = self.text, - let subStringRange = Range(range, in: text) - else { return } - clauses.append(ActionableClause(location: range.location, length: range.length, - actionText: String(text[subStringRange]), actionBlock: actionBlock)) } @@ -418,78 +409,13 @@ extension Label { - delegate: - additionalData: */ - @objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, delegate: DelegateObject?, additionalData: [AnyHashable: Any]?) { - - guard let text = self.text, - let subStringRange = Range(range, in: text) - else { return } + @objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegate: DelegateObject?) { clauses.append(ActionableClause(location: range.location, length: range.length, - actionText: String(text[subStringRange]), - actionBlock: { [weak self, weak delegate] in - var willPerform = true - - if let wSelf = self, let buttonDelegate = (delegate as? MVMCoreUIDelegateObject)?.buttonDelegate, - buttonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) { - willPerform = buttonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false - } - - if willPerform { - MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegate) - } })) - } - - /** - Provides an actionable range of text. - - Allows actionable range to be established by a particular substring of the containing label text. - - - Attention: This method expects text to be set first. Otherwise, it will do nothing. Do not use if actionText is not unique in the Label's text. - - Parameters: - - actionText: The actionable text contained witin the label's text. - - actionBlock: The code triggered when tapping the range of text. - */ - @objc public func addTappableLinkAttribute(actionText: String, actionBlock: @escaping ActionBlock) { - - guard let text = self.text else { return } - let string = text as NSString - let range = string.range(of: actionText) - - clauses.append(ActionableClause(location: range.location, - length: range.length, - actionText: actionText, - actionBlock: actionBlock)) - } - - /** - Provides an actionable range of text. - - - Attention: This method expects text to be set first. Otherwise, it will do nothing. Do not use if actionText is not unique in the Label's text. - - Parameters: - - actionText: The actionable text contained witin the label's text. - - actionMap: - - delegate: - - additionalData: - */ - @objc public func addTappableLinkAttribute(actionText: String, actionMap: [AnyHashable: Any]?, delegate: DelegateObject?, additionalData: [AnyHashable: Any]?) { - - guard let text = self.text else { return } - let string = text as NSString - let range = string.range(of: actionText) - - clauses.append(ActionableClause(location: range.location, length: range.length, actionText: actionText, - actionBlock: { [weak self, weak delegate] in - var willPerform = true - - if let wSelf = self, let buttonDelegate = (delegate as? MVMCoreUIDelegateObject)?.buttonDelegate, - buttonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) { - willPerform = buttonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false - } - - if willPerform { - MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegate) - } })) + actionBlock: createActionBlockFrom(actionMap: actionMap, + additionalData: additionalData, + delegate: delegate))) } @objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) { diff --git a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift index c5981e5e..dbb4cfc5 100644 --- a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift +++ b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift @@ -66,7 +66,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt } else { label?.clauses = [Label.ActionableClause(location: actionRange.location, length: actionRange.length, - actionText: actionText, actionBlock: newActionBlock)] } } @@ -166,19 +165,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt super.init(frame: .zero) setText(fullText, startTag: startTag, endTag: endTag) - - actionBlock = { [weak self, weak delegateObject] in - var performAction = true - - if let wSelf = self, let wButtonDelegate = (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate, - wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) { - performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false - } - - if performAction { - MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) - } - } + actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegate: delegateObject) } //------------------------------------------------------ @@ -213,18 +200,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt @objc public func setActionMap(_ actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { - actionBlock = { [weak self, weak delegateObject] in - var performAction = true - - if let wSelf = self, let wButtonDelegate = (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate, - wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) { - performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false - } - - if performAction { - MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) - } - } + actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegate: delegateObject) } //------------------------------------------------------ @@ -409,18 +385,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt actionText = actionMap?.optionalStringForKey(KeyTitle) backText = actionMap?.optionalStringForKey(KeyTitlePostfix) - actionBlock = { [weak self, weak delegateObject] in - var performAction = true - - if let wSelf = self, let wButtonDelegate = (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate, - wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) { - performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false - } - - if performAction { - MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) - } - } + actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegate: delegateObject) text = getTextFromStringComponents() setLabelAttributes()