Skip to content

Commit 67314dc

Browse files
authored
Merge pull request #227 from surfstudio/feature/SPT-1474/view_wrappers_optimisation
Feature/spt 1474/view wrappers optimisation
2 parents f7e67bf + 671d578 commit 67314dc

File tree

12 files changed

+163
-90
lines changed

12 files changed

+163
-90
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// CollectionWrappedCell+RDDM.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 05.06.2023.
6+
//
7+
8+
import ReactiveDataDisplayManager
9+
import UIKit
10+
11+
public extension StaticDataDisplayWrapper where Base: UIView & ConfigurableItem {
12+
13+
func collectionGenerator(with model: Base.Model,
14+
and registerType: CellRegisterType = .nib) -> BaseCollectionCellGenerator<CollectionWrappedCell<Base>> {
15+
CollectionWrappedCell<Base>.rddm.baseGenerator(with: model,
16+
and: registerType)
17+
}
18+
19+
}

Components/Sources/Collection/CollectionWrappedCell.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ public final class CollectionWrappedCell<View: ConfigurableItem>: UICollectionVi
1717

1818
public let nestedView: View = .init(frame: .zero)
1919

20+
public var cachedInsets: UIEdgeInsets?
21+
22+
public var cachedAlignment: Alignment?
23+
2024
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Alignment.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 05.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
/// Alignment of `nestedView` inside `ViewWrapper`
11+
public enum Alignment: Equatable {
12+
13+
/// same as `TrailingLessThenOrEqual` and `Equal` for all other constraints
14+
case leading(UIEdgeInsets)
15+
/// same as `LeadingGreaterThenOrEqual` and `Equal` for all other constraints
16+
case trailing(UIEdgeInsets)
17+
/// same as `Equal` for all `[top, bottom, leading, trailing]` constraints
18+
case all(UIEdgeInsets)
19+
20+
}

Components/Sources/Common/Protocols/AlignmentProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import UIKit
99

1010
public protocol AlignmentProvider {
1111

12-
var alignment: NSTextAlignment { get set }
12+
var alignment: Alignment { get }
1313

1414
}

Components/Sources/Common/Protocols/InsetsProvider.swift

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

Components/Sources/Common/Utils/ViewWrapper.swift

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,49 @@ public protocol ViewWrapper: ConfigurableItem {
1212

1313
associatedtype NestedView: ConfigurableItem
1414

15+
/// Inner configurable view with content
1516
var nestedView: NestedView { get }
1617

18+
/// Previous value of `Alignment` applyed to `nestedView`
19+
var cachedAlignment: Alignment? { get set }
20+
1721
}
1822

23+
// MARK: - Common implementation
24+
1925
public extension ViewWrapper where Self: UIView {
2026

2127
func configure(with model: NestedView.Model) {
22-
let insets = (model as? InsetsProvider)?.edgeInsets ?? .zero
23-
let alignmentRule = (model as? AlignmentProvider)?.alignment ?? .center
28+
wrapNestedViewIfNeeded(with: model)
29+
nestedView.configure(with: model)
30+
}
31+
32+
}
2433

25-
switch alignmentRule {
26-
case .right:
34+
// MARK: - Private
35+
36+
private extension ViewWrapper where Self: UIView {
37+
38+
/// Adding nestedView as subview only if constraint specific parameters were changed
39+
func wrapNestedViewIfNeeded(with model: NestedView.Model) {
40+
let alignment = (model as? AlignmentProvider)?.alignment
41+
42+
guard alignment != cachedAlignment else {
43+
return
44+
}
45+
46+
nestedView.removeFromSuperview()
47+
switch alignment {
48+
case .trailing(let insets):
2749
wrapWithLeadingGreaterThenOrEqualRule(subview: nestedView, with: insets)
28-
case .left:
50+
case .leading(let insets):
2951
wrapWithTrailingLessThenOrEqualRule(subview: nestedView, with: insets)
30-
default:
52+
case .all(let insets):
3153
wrap(subview: nestedView, with: insets)
54+
case .none:
55+
break
3256
}
33-
nestedView.configure(with: model)
57+
cachedAlignment = alignment
3458
}
3559

3660
}

Components/Sources/Common/Views/LabelView.swift

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public class LabelView: UIView {
1313

1414
// MARK: - Private properties
1515

16-
private var heightConstraint: NSLayoutConstraint?
17-
private var textView = UILabel(frame: .zero)
16+
private var label: UILabel = .init(frame: .zero)
1817

1918
}
2019

@@ -24,7 +23,7 @@ extension LabelView: ConfigurableItem {
2423

2524
// MARK: - Model
2625

27-
public struct Model: InsetsProvider, AlignmentProvider {
26+
public struct Model: AlignmentProvider {
2827

2928
// MARK: - Nested types
3029

@@ -63,42 +62,38 @@ extension LabelView: ConfigurableItem {
6362
public let text: TextType
6463
public let style: TextStyle
6564
public let layout: TextLayout
66-
public var edgeInsets: UIEdgeInsets
67-
public var labelClass: UILabel.Type
68-
public var alignment: NSTextAlignment
65+
public let alignment: Alignment
66+
public let textAlignment: NSTextAlignment
6967

7068
// MARK: - Initialization
7169

7270
public init(text: TextType,
7371
style: TextStyle,
7472
layout: TextLayout,
75-
alignment: NSTextAlignment,
76-
edgeInsets: UIEdgeInsets,
77-
labelClass: UILabel.Type = UILabel.self) {
73+
textAlignment: NSTextAlignment,
74+
viewAlignment: Alignment = .all(.zero)) {
7875
self.text = text
7976
self.style = style
8077
self.layout = layout
81-
self.alignment = alignment
82-
self.edgeInsets = edgeInsets
83-
self.labelClass = labelClass
78+
self.textAlignment = textAlignment
79+
self.alignment = viewAlignment
8480
}
8581

8682
}
8783

8884
// MARK: - Methods
8985

9086
public func configure(with model: Model) {
91-
textView = model.labelClass.init()
9287
configureConstraints()
9388

9489
self.backgroundColor = .clear
95-
textView.backgroundColor = .clear
96-
textView.textColor = model.style.color
97-
textView.font = model.style.font
90+
label.backgroundColor = .clear
91+
label.textColor = model.style.color
92+
label.font = model.style.font
9893

99-
textView.textAlignment = model.alignment
100-
textView.lineBreakMode = model.layout.lineBreakMode
101-
textView.numberOfLines = model.layout.numberOfLines
94+
label.textAlignment = model.textAlignment
95+
label.lineBreakMode = model.layout.lineBreakMode
96+
label.numberOfLines = model.layout.numberOfLines
10297

10398
configureText(with: model.text)
10499

@@ -112,27 +107,16 @@ extension LabelView: ConfigurableItem {
112107
private extension LabelView {
113108

114109
func configureConstraints() {
115-
wrap(subview: textView, with: .zero)
110+
wrap(subview: label, with: .zero)
116111
}
117112

118113
func configureText(with text: Model.TextType) {
119114
switch text {
120115
case .string(let text):
121-
textView.text = text
116+
label.text = text
122117
case .attributedString(let attrubutedText):
123-
textView.attributedText = attrubutedText
118+
label.attributedText = attrubutedText
124119
}
125120
}
126121

127122
}
128-
129-
// MARK: - LabelView.Model Equatable
130-
131-
extension LabelView.Model: Equatable {
132-
133-
public static func == (lhs: LabelView.Model, rhs: LabelView.Model) -> Bool {
134-
return lhs.style == rhs.style && lhs.layout == rhs.layout && lhs.text == rhs.text &&
135-
lhs.edgeInsets == rhs.edgeInsets && lhs.labelClass == rhs.labelClass
136-
}
137-
138-
}

Components/Sources/Common/Views/MessageView.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension MessageView: ConfigurableItem {
2323

2424
// MARK: - Model
2525

26-
public struct Model: Equatable, InsetsProvider, AlignmentProvider {
26+
public struct Model: Equatable, AlignmentProvider {
2727

2828
// MARK: - Nested types
2929

@@ -70,23 +70,23 @@ extension MessageView: ConfigurableItem {
7070

7171
public let text: TextType
7272
public let style: MessageStyle
73-
public var alignment: NSTextAlignment
74-
public var edgeInsets: UIEdgeInsets
73+
public var textAlignment: NSTextAlignment
74+
public var alignment: Alignment
7575
public let internalEdgeInsets: UIEdgeInsets
7676
public let borderStyle: BorderStyle
7777

7878
// MARK: - Initialization
7979

8080
public init(text: TextType,
8181
style: MessageStyle,
82-
alignment: NSTextAlignment,
83-
externalEdgeInsets: UIEdgeInsets,
82+
textAlignment: NSTextAlignment,
83+
viewAlignment: Alignment,
8484
internalEdgeInsets: UIEdgeInsets,
8585
borderStyle: BorderStyle) {
8686
self.text = text
8787
self.style = style
88-
self.alignment = alignment
89-
self.edgeInsets = externalEdgeInsets
88+
self.textAlignment = textAlignment
89+
self.alignment = viewAlignment
9090
self.internalEdgeInsets = internalEdgeInsets
9191
self.borderStyle = borderStyle
9292
}
@@ -104,7 +104,7 @@ extension MessageView: ConfigurableItem {
104104
configureTextView(textView, with: model)
105105
textView.textColor = model.style.textColor
106106
textView.font = model.style.font
107-
textView.textAlignment = model.alignment
107+
textView.textAlignment = model.textAlignment
108108

109109
wrap(subview: textView, with: model.internalEdgeInsets)
110110

Components/Sources/Common/Views/SeparatorView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SeparatorView: UIView {
3333

3434
extension SeparatorView: ConfigurableItem {
3535

36-
public struct Model: Equatable, InsetsProvider {
36+
public struct Model: Equatable {
3737
public let height: CGFloat
3838
public let color: UIColor?
3939
public var edgeInsets: UIEdgeInsets
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// TableWrappedCell+RDDM.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 05.06.2023.
6+
//
7+
8+
import ReactiveDataDisplayManager
9+
import UIKit
10+
11+
public extension StaticDataDisplayWrapper where Base: UIView & ConfigurableItem {
12+
13+
func tableGenerator(with model: Base.Model,
14+
and registerType: CellRegisterType = .nib) -> BaseCellGenerator<TableWrappedCell<Base>> {
15+
TableWrappedCell<Base>.rddm.baseGenerator(with: model,
16+
and: registerType)
17+
}
18+
19+
}

0 commit comments

Comments
 (0)