@@ -11,37 +11,10 @@ import ReactiveDataDisplayManager
1111/// Base view to implement label within cell
1212public class MessageView : UIView {
1313
14- // MARK: - Aliases
15-
16- typealias TapGesture = UILongPressGestureRecognizer
17-
18- // MARK: - Constants
19-
20- private enum Constants {
21- static let minimumPressDuration : TimeInterval = 0.001
22- }
23-
24- // MARK: - Public initialization
25-
26- public override init ( frame: CGRect ) {
27- super. init ( frame: frame)
28-
29- let tapGesture = TapGesture ( target: self , action: #selector( handleTapGesture ( _: ) ) )
30- tapGesture. cancelsTouchesInView = false
31- tapGesture. minimumPressDuration = Constants . minimumPressDuration
32- addGestureRecognizer ( tapGesture)
33- }
34-
35- required init ? ( coder: NSCoder ) {
36- fatalError ( " init(coder:) has not been implemented " )
37- }
38-
3914 // MARK: - Private properties
4015
4116 private var textView = UITextView ( frame: . zero)
42- private var dataDetection : DataDetection ?
43- private var tapHandler : TapHandler ?
44- private var pressStateAction : ( ( TapGesture . State ) -> Void ) ?
17+ private var dataDetectionHandler : DataDetectionStyle . Handler ?
4518
4619}
4720
@@ -132,21 +105,13 @@ extension MessageView: ConfigurableItem {
132105 } )
133106 }
134107
135- public static func dataDetection( _ value: DataDetection ) -> Property {
108+ public static func dataDetection( _ value: DataDetectionStyle ) -> Property {
136109 . init( closure: { model in
137110 var model = model
138111 model. set ( dataDetection: value)
139112 return model
140113 } )
141114 }
142-
143- public static func tapHandler( _ value: TapHandler ? ) -> Property {
144- . init( closure: { model in
145- var model = model
146- model. set ( tapHandler: value)
147- return model
148- } )
149- }
150115
151116 /// To set it to **false**, dataDetection and tapHandler must be nil
152117 public static func selectable( _ selectable: Bool ) -> Property {
@@ -168,8 +133,7 @@ extension MessageView: ConfigurableItem {
168133 private( set) public var alignment : Alignment = . all( . zero)
169134 private( set) public var internalEdgeInsets : UIEdgeInsets = . zero
170135 private( set) public var borderStyle : BorderStyle ?
171- private( set) public var dataDetection : DataDetection ?
172- private( set) public var tapHandler : TapHandler ?
136+ private( set) public var dataDetection : DataDetectionStyle ?
173137 private( set) public var selectable : Bool = false
174138
175139 // MARK: - Mutation
@@ -206,14 +170,10 @@ extension MessageView: ConfigurableItem {
206170 self . borderStyle = border
207171 }
208172
209- mutating func set( dataDetection: DataDetection ) {
173+ mutating func set( dataDetection: DataDetectionStyle ) {
210174 self . dataDetection = dataDetection
211175 }
212176
213- mutating func set( tapHandler: TapHandler ? ) {
214- self . tapHandler = tapHandler
215- }
216-
217177 mutating func set( selectable: Bool ) {
218178 self . selectable = selectable
219179 }
@@ -266,12 +226,7 @@ private extension MessageView {
266226 textView. dataDetectorTypes = model. dataDetection? . dataDetectorTypes ?? [ ]
267227 textView. linkTextAttributes = model. dataDetection? . linkTextAttributes
268228 textView. delegate = self
269- dataDetection = model. dataDetection
270-
271- self . tapHandler = model. tapHandler
272- pressStateAction = { [ weak self] state in
273- self ? . handleTapGesture ( state, with: model)
274- }
229+ dataDetectionHandler = model. dataDetection? . handler
275230 }
276231
277232 func applyBackground( style: BackgroundStyle ) {
@@ -291,63 +246,26 @@ private extension MessageView {
291246 layer. maskedCorners = borderStyle. maskedCorners
292247 }
293248
294- func handleDataDetection( _ data: String ) {
295- guard let dataDetectionHandler = dataDetection? . dataDetectionHandler else {
296- return
297- }
298-
299- dataDetectionHandler ( data)
300- }
301-
302- @objc func handleTapGesture( _ gesture: TapGesture ) {
303- guard tapHandler? . tapAction != nil else {
304- return
305- }
306- pressStateAction ? ( gesture. state)
249+ func handleDataDetection( _ data: URL ) {
250+ dataDetectionHandler ? ( data)
307251 }
308252
309253 func setIsSelectablePropertyIfNeeded( for model: Model ) {
310- if model. dataDetection != nil || model . tapHandler != nil {
254+ if model. dataDetection != nil {
311255 textView. isSelectable = true
312256 } else {
313257 textView. isSelectable = model. selectable
314258 }
315259 }
316260
317- func handleTapGesture( _ state: TapGesture . State , with model: Model ) {
318- switch state {
319- case . began:
320- setPressState ( for: model)
321- case . cancelled, . failed:
322- setInitialState ( for: model)
323- case . ended:
324- setInitialState ( for: model)
325- tapHandler? . tapAction ? ( )
326- default :
327- break
328- }
329- }
330-
331- func setPressState( for model: Model ) {
332- textView. textColor = model. tapHandler? . textStyle? . color ?? model. textStyle. color
333- textView. font = model. tapHandler? . textStyle? . font ?? model. textStyle. font
334- applyBackground ( style: model. tapHandler? . backgroundStyle ?? model. backgroundStyle)
335- }
336-
337- func setInitialState( for model: Model ) {
338- textView. textColor = model. textStyle. color
339- textView. font = model. textStyle. font
340- applyBackground ( style: model. backgroundStyle)
341- }
342-
343261}
344262
345263// MARK: - UITextViewDelegate
346264
347265extension MessageView : UITextViewDelegate {
348266
349267 public func textView( _ textView: UITextView , shouldInteractWith URL: URL , in characterRange: NSRange , interaction: UITextItemInteraction ) -> Bool {
350- handleDataDetection ( URL . absoluteString )
268+ handleDataDetection ( URL)
351269 return false
352270 }
353271
0 commit comments