Skip to content

Commit df6434e

Browse files
committed
SPT-1469 optimize FoldableItem, remove CollectionFoldableItem
1 parent 30224c1 commit df6434e

File tree

12 files changed

+205
-214
lines changed

12 files changed

+205
-214
lines changed

Example/ReactiveDataDisplayManager/Collection/FoldableCollectionViewController/FoldableCollectionViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ private extension FoldableCollectionViewController {
7979
let child3 = makeRegularCellWithTitlesGenerators(count: 2)
8080

8181
// Create foldable generators
82-
let folder1 = makeFoldableCellGenerator(color: .lightGray, expanded: false)
83-
let folder2 = makeFoldableCellGenerator(color: .lightGray, expanded: false)
84-
let folder3 = makeFoldableCellGenerator(color: .lightGray, expanded: true)
82+
var folder1 = makeFoldableCellGenerator(color: .lightGray, expanded: false)
83+
var folder2 = makeFoldableCellGenerator(color: .lightGray, expanded: false)
84+
var folder3 = makeFoldableCellGenerator(color: .lightGray, expanded: true)
8585

8686
// Configure relationship
8787
folder3.children = child3
@@ -96,7 +96,7 @@ private extension FoldableCollectionViewController {
9696
adapter => .reload
9797
}
9898

99-
func makeFoldableCellGenerator(color: UIColor, expanded: Bool) -> CollectionCellGenerator & CollectionFoldableItem {
99+
func makeFoldableCellGenerator(color: UIColor, expanded: Bool) -> CollectionCellGenerator & CollectionChildrenHolder {
100100
// Create foldable generator
101101
let generator = FoldableCollectionViewCell.rddm.foldableGenerator(with: .init(color: color))
102102

@@ -130,7 +130,7 @@ private extension FoldableCollectionViewController {
130130
}
131131

132132
func getVisibleGenerators(for generator: CollectionCellGenerator) -> [CollectionCellGenerator] {
133-
if let foldableItem = generator as? CollectionFoldableItem, foldableItem.isExpanded {
133+
if let foldableItem = generator as? FoldableItem & CollectionChildrenHolder, foldableItem.isExpanded {
134134
return foldableItem.children
135135
.map { getVisibleGenerators(for: $0) }
136136
.reduce([generator], +)

ReactiveDataDisplayManagerTests/Mocks/AutoMockable.generated.swift

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,7 @@ class CollectionCellGeneratorMock: CollectionCellGenerator {
9191
}
9292

9393
}
94-
class CollectionFoldableItemMock: CollectionFoldableItem {
95-
var didFoldEvent: Event<Bool> {
96-
get { return underlyingDidFoldEvent }
97-
set(value) { underlyingDidFoldEvent = value }
98-
}
99-
var underlyingDidFoldEvent: Event<Bool>!
100-
var isExpanded: Bool {
101-
get { return underlyingIsExpanded }
102-
set(value) { underlyingIsExpanded = value }
103-
}
104-
var underlyingIsExpanded: Bool!
105-
var children: [CollectionCellGenerator] = []
106-
var labelStrategy: AccessibilityStringStrategy {
107-
get { return underlyingLabelStrategy }
108-
set(value) { underlyingLabelStrategy = value }
109-
}
110-
var underlyingLabelStrategy: AccessibilityStringStrategy!
111-
var valueStrategy: AccessibilityStringStrategy {
112-
get { return underlyingValueStrategy }
113-
set(value) { underlyingValueStrategy = value }
114-
}
115-
var underlyingValueStrategy: AccessibilityStringStrategy!
116-
var traitsStrategy: AccessibilityTraitsStrategy {
117-
get { return underlyingTraitsStrategy }
118-
set(value) { underlyingTraitsStrategy = value }
119-
}
120-
var underlyingTraitsStrategy: AccessibilityTraitsStrategy!
121-
var isAccessibilityIgnored: Bool {
122-
get { return underlyingIsAccessibilityIgnored }
123-
set(value) { underlyingIsAccessibilityIgnored = value }
124-
}
125-
var underlyingIsAccessibilityIgnored: Bool!
12694

127-
}
12895
class CollectionFooterGeneratorMock: CollectionFooterGenerator {
12996
var identifier: UICollectionReusableView.Type {
13097
get { return underlyingIdentifier }

Source/Collection/CollectionCell+RDDM.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public extension StaticDataDisplayWrapper where Base: UICollectionViewCell & Cal
2828

2929
public extension StaticDataDisplayWrapper where Base: UICollectionViewCell & ConfigurableItem & FoldableStateHolder {
3030

31-
func foldableGenerator(with model: Base.Model, and registerType: RegistrationType = .nib) -> FoldableCollectionCellGenerator<Base> {
31+
func foldableGenerator(with model: Base.Model, and registerType: RegistrationType = .nib) -> FoldableCellGenerator<Base> {
3232
.init(with: model, registerType: registerType)
3333
}
3434

Source/Collection/Generators/FoldableCollectionCellGenerator.swift

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

Source/Collection/Plugins/CollectionPluginsChecker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class CollectionPluginsChecker {
3131
func checkPlugins() {
3232
sections.flatMap(\.generators).forEach {
3333
checkPlugin(for: $0 as? SelectableItem)
34-
checkPlugin(for: $0 as? CollectionFoldableItem, pluginName: CollectionFoldablePlugin.pluginName)
34+
checkPlugin(for: $0 as? FoldableItem, pluginName: CollectionFoldablePlugin.pluginName)
3535
}
3636
}
3737

Source/Collection/Plugins/PluginAction/CollectionFoldablePlugin.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55
// Created by Vadim Tikhonov on 11.02.2021.
66
//
77

8-
/// Plugin to support `CollectionFoldableItem`
8+
/// Plugin to support `FoldableItem` in `UICollectionView`
99
///
1010
/// Allow expand or collapse child cells
1111
public class CollectionFoldablePlugin: BaseCollectionPlugin<CollectionEvent> {
1212

13+
// MARK: - Nested types
14+
15+
public typealias GeneratorType = FoldableItem & CollectionChildrenHolder & FoldableStateToggling
16+
17+
// MARK: - Plugin body
18+
1319
public override func process(event: CollectionEvent, with manager: BaseCollectionManager?) {
1420

1521
switch event {
1622
case .didSelect(let indexPath):
1723
guard
1824
let generator = manager?.sections[indexPath.section].generators[indexPath.row],
19-
let foldable = generator as? CollectionFoldableItem
25+
let foldable = generator as? GeneratorType
2026
else {
2127
return
2228
}
@@ -35,7 +41,7 @@ public class CollectionFoldablePlugin: BaseCollectionPlugin<CollectionEvent> {
3541
}
3642
}
3743

38-
foldable.isExpanded = !foldable.isExpanded
44+
foldable.toggleEpanded()
3945
foldable.didFoldEvent.invoke(with: (foldable.isExpanded))
4046
default:
4147
break
@@ -49,7 +55,7 @@ public class CollectionFoldablePlugin: BaseCollectionPlugin<CollectionEvent> {
4955
private extension CollectionFoldablePlugin {
5056

5157
func getVisibleGenerators(for generator: CollectionCellGenerator) -> [CollectionCellGenerator] {
52-
if let foldableItem = generator as? CollectionFoldableItem, foldableItem.isExpanded {
58+
if let foldableItem = generator as? GeneratorType, foldableItem.isExpanded {
5359
return foldableItem.children
5460
.map { getVisibleGenerators(for: $0) }
5561
.reduce([generator], +)
@@ -64,7 +70,7 @@ private extension CollectionFoldablePlugin {
6470

6571
public extension BaseCollectionPlugin {
6672

67-
/// Plugin to support `CollectionFoldableItem`
73+
/// Plugin to support `FoldableItem` in `UICollectionView`
6874
///
6975
/// Allow expand or collapse child cells
7076
static func foldable() -> BaseCollectionPlugin<CollectionEvent> {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CollectionFoldableItem.swift
2+
// CollectionChildrenHolder.swift
33
// ReactiveDataDisplayManager
44
//
55
// Created by Vadim Tikhonov on 11.02.2021.
@@ -9,13 +9,13 @@
99
import UIKit
1010

1111
// sourcery: AutoMockable
12-
public protocol CollectionFoldableItem: AnyObject, AccessibilityStrategyProvider {
13-
var didFoldEvent: Event<Bool> { get }
14-
var isExpanded: Bool { get set }
12+
public protocol CollectionChildrenHolder: AccessibilityStrategyProvider {
1513
var children: [CollectionCellGenerator] { get set }
1614
}
1715

18-
public extension CollectionFoldableItem {
16+
// MARK: - Defaults
17+
18+
public extension CollectionChildrenHolder {
1919
var labelStrategy: AccessibilityStringStrategy { .ignored }
2020
var traitsStrategy: AccessibilityTraitsStrategy { children.isEmpty ? .ignored : .just(.button) }
2121
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//
2+
// FoldableCellGenerator.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by korshunov on 23.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
/// A generator that can insert and remove child generators by tap
11+
open class FoldableCellGenerator<Cell: ConfigurableItem & FoldableStateHolder>: BaseCellGenerator<Cell>, FoldableItem, FoldableStateToggling {
12+
13+
// MARK: - Private
14+
15+
private var tableAnimation: TableChildrenAnimationGroup = (.left, .top)
16+
private var tableChildren: [TableCellGenerator] = []
17+
18+
private var collectionChildren: [CollectionCellGenerator] = []
19+
20+
// MARK: - Public
21+
22+
/// Event on changed `isExpanded` state
23+
public var didFoldEvent = Event<Bool>()
24+
25+
/// Folded/unfolded state
26+
public var isExpanded = false
27+
28+
// MARK: - BaseCellGenerator
29+
30+
public override func configure(cell: Cell, with model: Cell.Model) {
31+
super.configure(cell: cell, with: model)
32+
33+
cell.setExpanded(isExpanded)
34+
35+
didFoldEvent.addListner(with: "rddm.foldable-on-dequeue") { [weak cell] isExpanded in
36+
cell?.setExpanded(isExpanded)
37+
}
38+
}
39+
40+
// MARK: - FoldableStateToggling
41+
42+
public func toggleEpanded() {
43+
isExpanded.toggle()
44+
}
45+
46+
}
47+
48+
// MARK: - TableChildrenHolder
49+
50+
extension FoldableCellGenerator: TableChildrenHolder where Cell: UITableViewCell {
51+
52+
public var children: [TableCellGenerator] {
53+
get {
54+
tableChildren
55+
}
56+
set {
57+
tableChildren = newValue
58+
}
59+
}
60+
61+
public var animation: TableChildrenAnimationGroup {
62+
get {
63+
tableAnimation
64+
}
65+
set {
66+
tableAnimation = newValue
67+
}
68+
}
69+
70+
}
71+
72+
// MARK: - CollectionChildrenHolder
73+
74+
extension FoldableCellGenerator: CollectionChildrenHolder where Cell: UICollectionViewCell {
75+
76+
public var children: [CollectionCellGenerator] {
77+
get {
78+
collectionChildren
79+
}
80+
set {
81+
collectionChildren = newValue
82+
}
83+
}
84+
85+
}
86+
87+
// MARK: - Decorations
88+
89+
public extension FoldableCellGenerator {
90+
91+
/// - Parameter isExpanded: folded/unfolded state
92+
func isExpanded(_ isExpanded: Bool) -> Self {
93+
self.isExpanded = isExpanded
94+
return self
95+
}
96+
97+
/// - Parameter closure: handler closure for folded/unfolded events
98+
func didFoldEvent(_ closure: @escaping (Bool) -> Void) -> Self {
99+
self.didFoldEvent.addListner(closure)
100+
return self
101+
}
102+
103+
}
104+
105+
public extension FoldableCellGenerator where Cell: UITableViewCell {
106+
107+
/// - Parameter animation: animations for cells insertion and deletion
108+
func animation(_ animation: TableChildrenAnimationGroup) -> Self {
109+
self.animation = animation
110+
return self
111+
}
112+
113+
/// - Parameter children: array of child generators
114+
func children(_ children: [TableCellGenerator]) -> Self {
115+
self.children = children
116+
return self
117+
}
118+
119+
/// - Parameter content: resultBuilder based closure that returns an array of child generators
120+
func children(@GeneratorsBuilder<TableCellGenerator>_ content: @escaping (TableContext.Type) -> [TableCellGenerator]) -> Self {
121+
self.children = content(TableContext.self)
122+
return self
123+
}
124+
125+
}
126+
127+
public extension FoldableCellGenerator where Cell: UICollectionViewCell {
128+
129+
/// - Parameter children: array of child generators
130+
func children(_ children: [CollectionCellGenerator]) -> Self {
131+
self.children = children
132+
return self
133+
}
134+
135+
/// - Parameter content: resultBuilder based closure that returns an array of child generators
136+
func children(@GeneratorsBuilder<CollectionCellGenerator>_ content: @escaping (CollectionContext.Type) -> [CollectionCellGenerator]) -> Self {
137+
self.children = content(CollectionContext.self)
138+
return self
139+
}
140+
141+
}

0 commit comments

Comments
 (0)