Skip to content

Commit 98faef2

Browse files
committed
improve StackCellGenerator to support nib based views
1 parent dc73d68 commit 98faef2

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Source/Stack/Generators/BaseStackCellGenerator.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,60 @@
55
// Created by Никита Коробейников on 06.09.2021.
66
//
77

8+
import UIKit
9+
810
open 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
}

Source/Stack/View+RDDM.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
public 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
}

0 commit comments

Comments
 (0)