File tree Expand file tree Collapse file tree 2 files changed +47
-3
lines changed Expand file tree Collapse file tree 2 files changed +47
-3
lines changed Original file line number Diff line number Diff line change 55// Created by Никита Коробейников on 06.09.2021.
66//
77
8+ import UIKit
9+
810open class BaseStackCellGenerator < View: ConfigurableItem > : StackCellGenerator , ViewBuilder {
911
1012 public let model : View . Model
1113
12- public init ( with model: View . Model ) {
14+ // MARK: - Private properties
15+
16+ private let registerType : CellRegisterType
17+
18+ public init ( with model: View . Model ,
19+ registerType: CellRegisterType = . class) {
1320 self . model = model
21+ self . registerType = registerType
1422 }
1523
1624 public func build( view: View ) {
1725 view. configure ( with: model)
1826 }
1927
28+ public func generate( stackView: UIStackView , index: Int ) -> UIView {
29+ let view = createView ( )
30+ self . build ( view: view)
31+ return view
32+ }
33+
34+ }
35+
36+ // MARK: - Private
37+
38+ private extension BaseStackCellGenerator {
39+
40+ func createView( ) -> ViewType {
41+ switch registerType {
42+ case . nib:
43+ return ViewType . loadFromNib ( bundle: ViewType . bundle ( ) ?? . main)
44+ case . class:
45+ return ViewType ( )
46+ }
47+ }
48+
49+ }
50+
51+ private extension ConfigurableItem {
52+
53+ static func loadFromNib< T: UIView > ( bundle: Bundle ) -> T {
54+ let nibName = String ( describing: self )
55+ let nib = UINib ( nibName: nibName, bundle: bundle)
56+
57+ guard let view = nib. instantiate ( withOwner: self , options: nil ) . first as? T else {
58+ return T ( )
59+ }
60+
61+ return view
62+ }
63+
2064}
Original file line number Diff line number Diff line change 77
88public extension StaticDataDisplayWrapper where Base: ConfigurableItem {
99
10- func baseStackGenerator( with model: Base . Model ) -> BaseStackCellGenerator < Base > {
11- . init( with: model)
10+ func baseStackGenerator( with model: Base . Model , and registerType : CellRegisterType = . class ) -> BaseStackCellGenerator < Base > {
11+ . init( with: model, registerType : registerType )
1212 }
1313
1414}
You can’t perform that action at this time.
0 commit comments