Skip to content

Commit e396510

Browse files
committed
remove tap handler, renames and optimisations in datadetection
1 parent 7ab2eda commit e396510

File tree

4 files changed

+21
-145
lines changed

4 files changed

+21
-145
lines changed

Components/Sources/Common/Models/Styles/DataDetection.swift renamed to Components/Sources/Common/Models/Styles/DataDetectionStyle.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// DataDetection.swift
2+
// DataDetectionStyle.swift
33
// ReactiveDataDisplayManager
44
//
55
// Created by Антон Голубейков on 13.07.2023.
@@ -8,29 +8,29 @@
88
import Foundation
99
import UIKit
1010

11-
public struct DataDetection: Equatable {
11+
public struct DataDetectionStyle: Equatable {
1212

1313
// MARK: - Nested types
1414

15-
public typealias DataDetectionHandler = (String) -> Void
15+
public typealias Handler = (URL) -> Void
1616

1717
// MARK: - Properties
1818

1919
public let id: AnyHashable
2020
public let linkTextAttributes: [NSAttributedString.Key: Any]
21-
public var dataDetectionHandler: DataDetectionHandler?
21+
public var handler: Handler?
2222
public var dataDetectorTypes: UIDataDetectorTypes = []
2323

24-
public init(id: AnyHashable, linkTextAttributes: [NSAttributedString.Key: Any], dataDetectionHandler: @escaping DataDetectionHandler, dataDetectorTypes: UIDataDetectorTypes) {
24+
public init(id: AnyHashable, linkTextAttributes: [NSAttributedString.Key: Any], handler: @escaping Handler, dataDetectorTypes: UIDataDetectorTypes) {
2525
self.id = id
2626
self.linkTextAttributes = linkTextAttributes
27-
self.dataDetectionHandler = dataDetectionHandler
27+
self.handler = handler
2828
self.dataDetectorTypes = dataDetectorTypes
2929
}
3030

3131
// MARK: - Equatable
3232

33-
public static func == (lhs: DataDetection, rhs: DataDetection) -> Bool {
33+
public static func == (lhs: DataDetectionStyle, rhs: DataDetectionStyle) -> Bool {
3434
lhs.id == rhs.id &&
3535
areDictionariesEqual(lhs.linkTextAttributes, rhs.linkTextAttributes) &&
3636
lhs.dataDetectorTypes == rhs.dataDetectorTypes
@@ -40,7 +40,7 @@ public struct DataDetection: Equatable {
4040

4141
// MARK: - Private extension
4242

43-
private extension DataDetection {
43+
private extension DataDetectionStyle {
4444

4545
private static func areDictionariesEqual(_ lhs: [NSAttributedString.Key: Any]?, _ rhs: [NSAttributedString.Key: Any]?) -> Bool {
4646
guard let lhs = lhs, let rhs = rhs else {

Components/Sources/Common/Models/Styles/TapHandler.swift

Lines changed: 0 additions & 35 deletions
This file was deleted.

Components/Sources/Common/Views/MessageView.swift

Lines changed: 9 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,10 @@ import ReactiveDataDisplayManager
1111
/// Base view to implement label within cell
1212
public 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

347265
extension 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

Example/ReactiveDataDisplayManager/Table/ComponentsOverviewTableViewController/ComponentsOverviewTableViewController.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ final class ComponentsOverviewTableViewController: UIViewController {
111111
topController.present(alertController, animated: true, completion: nil)
112112
}
113113
}
114-
property.tapHandler(TapHandler(id: "Handling tap on message",
115-
textStyle: .init(color: .white.withAlphaComponent(0.5),
116-
font: .systemFont(ofSize: 16, weight: .regular)),
117-
backgroundStyle: .solid(backgorundColor?.withAlphaComponent(0.5)),
118-
tapAction: tapAction))
119114

120115
}
121116

@@ -131,14 +126,12 @@ final class ComponentsOverviewTableViewController: UIViewController {
131126
],
132127
borderWidth: 1,
133128
borderColor: UIColor.black.cgColor)
134-
let dataDetectionHandler: DataDetection.DataDetectionHandler = { urlString in
135-
if let url = URL(string: urlString) {
136-
UIApplication.shared.open(url, options: [:], completionHandler: nil)
137-
}
129+
let dataDetectionHandler: DataDetectionStyle.Handler = { url in
130+
UIApplication.shared.open(url, options: [:], completionHandler: nil)
138131
}
139-
private lazy var dataDetection = DataDetection(id: "Highlighting links and open them by tap",
132+
private lazy var dataDetection = DataDetectionStyle(id: "Basic handling of links using UIApplication",
140133
linkTextAttributes: [.foregroundColor: UIColor.blue],
141-
dataDetectionHandler: dataDetectionHandler,
134+
handler: dataDetectionHandler,
142135
dataDetectorTypes: [.link])
143136
private lazy var recievedMessageModel: MessageView.Model = .build { property in
144137
property.border(recievedMessageBorderStyle)

0 commit comments

Comments
 (0)