Skip to content

Commit a60c0f0

Browse files
committed
Add context in configure
1 parent b67992e commit a60c0f0

File tree

5 files changed

+8
-10
lines changed

5 files changed

+8
-10
lines changed

Sources/Micro/DataSource.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import UIKit
99

1010
open class DataSource: NSObject {
1111
private var cellRegister: Set<String> = Set()
12-
private var finalState: State = .init()
12+
internal var trueState: State = .init()
1313
public weak var collectionView: UICollectionView?
1414

1515
public init(collectionView: UICollectionView) {
@@ -18,7 +18,7 @@ open class DataSource: NSObject {
1818

1919
public var state: State {
2020
get {
21-
finalState
21+
trueState
2222
}
2323
set {
2424
reload(newState: newValue)
@@ -34,7 +34,6 @@ open class DataSource: NSObject {
3434
let request = Reloader.Request(
3535
dataSource: self,
3636
collectionView: collectionView,
37-
oldState: finalState,
3837
newState: newState,
3938
isAnimated: isAnimated,
4039
completion: completion ?? { _ in }

Sources/Micro/ForEach.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public func forEach<Model: DiffAware>(
1414
) -> State {
1515
let onReload: Reloader.Reload = { request in
1616
let reload: () -> Void = {
17-
let oldModels: [Model] = request.oldState.models.compactMap({ $0 as? Model })
17+
let oldModels: [Model] = request.dataSource.trueState.models.compactMap({ $0 as? Model })
1818

1919
if oldModels.isEmpty {
20-
request.dataSource.finalState = request.newState
20+
request.dataSource.trueState = request.newState
2121
request.collectionView.reloadData()
2222
request.completion(true)
2323
return
@@ -28,7 +28,7 @@ public func forEach<Model: DiffAware>(
2828
changes: changes,
2929
section: 0,
3030
updateData: {
31-
request.dataSource.finalState = request.newState
31+
request.dataSource.trueState = request.newState
3232
},
3333
completion: request.completion
3434
)

Sources/Micro/Item.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public protocol ObserverOwner {
1414
public struct Item<Cell: UICollectionViewCell>: ObserverOwner {
1515
public let observer = Observer()
1616

17-
public init(_ closure: @escaping (Cell) -> Void) {
17+
public init(_ closure: @escaping (Context, Cell) -> Void) {
1818
observer.onConfigure = { context, dataSource in
1919
dataSource.registerIfNeeded(collectionView: context.collectionView, cellType: Cell.self)
2020
if let cell: Cell = context.collectionView.dequeue(for: context.indexPath) {
21-
closure(cell)
21+
closure(context, cell)
2222
return cell
2323
} else {
2424
return nil

Sources/Micro/Reloader.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public struct Reloader {
1111
public struct Request {
1212
public let dataSource: DataSource
1313
public let collectionView: UICollectionView
14-
public let oldState: State
1514
public let newState: State
1615
public let isAnimated: Bool
1716
public let completion: Completion

Tests/MicroTests/MicroTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class MicroTests: XCTestCase {
3030
]
3131

3232
dataSource.state = forEach(models: persons) { person in
33-
Item<PersonCell>() { cell in
33+
Item<PersonCell>() { context, cell in
3434
cell.nameLabel.text = person.name
3535
}
3636
.onSelect { _ in

0 commit comments

Comments
 (0)