Skip to content

Commit

Permalink
Use Cell
Browse files Browse the repository at this point in the history
  • Loading branch information
onmyway133 committed Feb 9, 2020
1 parent 6edc7aa commit cb51cac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
13 changes: 6 additions & 7 deletions Micro.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
D29CB47A23EF6B960067685A /* Item.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29CB47723EF6B920067685A /* Item.swift */; };
D29CB47A23EF6B960067685A /* Cell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29CB47723EF6B920067685A /* Cell.swift */; };
D29CB47C23EF6BA20067685A /* DataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29CB47B23EF6BA20067685A /* DataSource.swift */; };
D29CB48023EF6BC90067685A /* State.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29CB47F23EF6BC90067685A /* State.swift */; };
D29CB48223EF6C020067685A /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29CB48123EF6C020067685A /* Extensions.swift */; };
Expand Down Expand Up @@ -78,7 +78,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
D29CB47723EF6B920067685A /* Item.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Item.swift; sourceTree = "<group>"; };
D29CB47723EF6B920067685A /* Cell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cell.swift; sourceTree = "<group>"; };
D29CB47B23EF6BA20067685A /* DataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataSource.swift; sourceTree = "<group>"; };
D29CB47F23EF6BC90067685A /* State.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; };
D29CB48123EF6C020067685A /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -211,7 +211,7 @@
name = Products;
sourceTree = BUILT_PRODUCTS_DIR;
};
OBJ_5 /* */ = {
OBJ_5 = {
isa = PBXGroup;
children = (
OBJ_6 /* Package.swift */,
Expand All @@ -221,7 +221,6 @@
OBJ_30 /* Products */,
OBJ_34 /* DerivedData */,
);
name = "";
sourceTree = "<group>";
};
OBJ_7 /* Sources */ = {
Expand All @@ -235,7 +234,7 @@
OBJ_8 /* Micro */ = {
isa = PBXGroup;
children = (
D29CB47723EF6B920067685A /* Item.swift */,
D29CB47723EF6B920067685A /* Cell.swift */,
D29CB47B23EF6BA20067685A /* DataSource.swift */,
D29CB47F23EF6BC90067685A /* State.swift */,
D29CB48123EF6C020067685A /* Extensions.swift */,
Expand Down Expand Up @@ -344,7 +343,7 @@
knownRegions = (
en,
);
mainGroup = OBJ_5 /* */;
mainGroup = OBJ_5;
productRefGroup = OBJ_30 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -393,7 +392,7 @@
D29CB48623EFD9D60067685A /* Reloader.swift in Sources */,
D29CB47C23EF6BA20067685A /* DataSource.swift in Sources */,
D29CB48223EF6C020067685A /* Extensions.swift in Sources */,
D29CB47A23EF6B960067685A /* Item.swift in Sources */,
D29CB47A23EF6B960067685A /* Cell.swift in Sources */,
D29CB48023EF6BC90067685A /* State.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Just declare a `State` with SwiftUI style `forEach` and Micro will reload with a

```swift
dataSource.state = forEach(models: blogs) { blog in
Item<BlogCell>() { context, cell in
Cell<BlogCell>() { context, cell in
cell.nameLabel.text = blog.name
}
.onSelect { context in
Expand All @@ -34,7 +34,7 @@ Features

### Complex model with multiple cell types

You can declare different `Item` in `forEach` with different kinds of cell.
You can declare different `Cell` in `forEach` with different kinds of cell.

```swift
struct Movie: DiffAware {
Expand Down Expand Up @@ -76,7 +76,7 @@ let movies: [Movie] = [
dataSource.state = forEach(models: movies) { movie in
switch movie.kind {
case .show(let name):
return Item<MovieCell>() { context, cell in
return Cell<MovieCell>() { context, cell in
cell.nameLabel.text = name
}
.onSelect { _ in
Expand All @@ -95,9 +95,9 @@ dataSource.state = forEach(models: movies) { movie in
CGSize(width: context.collectionView.frame.size.width, height: 40)
}
case .loading:
return Item<LoadingCell>() { _, _ in }
return Cell<LoadingCell>()
case .ad:
return Item<AdCell>() { _, _ in }
return Cell<AdCell>()
}
}
```
Expand Down
18 changes: 9 additions & 9 deletions Sources/Micro/Item.swift → Sources/Micro/Cell.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Item.swift
// Cell.swift
// DeepDiff
//
// Created by khoa on 08/02/2020.
Expand All @@ -11,13 +11,13 @@ public protocol ObserverOwner {
var observer: Observer { get }
}

public struct Item<Cell: UICollectionViewCell>: ObserverOwner {
public struct Cell<C: UICollectionViewCell>: ObserverOwner {
public let observer = Observer()

public init(_ closure: @escaping (Context, Cell) -> Void) {
public init(_ closure: @escaping (Context, C) -> Void = { _, _ in }) {
observer.onConfigure = { context, dataSource in
dataSource.registerIfNeeded(collectionView: context.collectionView, cellType: Cell.self)
if let cell: Cell = context.collectionView.dequeue(for: context.indexPath) {
dataSource.registerIfNeeded(collectionView: context.collectionView, cellType: C.self)
if let cell: C = context.collectionView.dequeue(for: context.indexPath) {
closure(context, cell)
return cell
} else {
Expand Down Expand Up @@ -51,18 +51,18 @@ public struct Item<Cell: UICollectionViewCell>: ObserverOwner {
return self
}

func onWillDisplay(_ closure: @escaping (Context, Cell) -> Void) -> Self {
func onWillDisplay(_ closure: @escaping (Context, C) -> Void) -> Self {
observer.onWillDisplay = { context, cell in
if let cell = cell as? Cell {
if let cell = cell as? C {
closure(context, cell)
}
}
return self
}

func onDidEndDisplay(_ closure: @escaping (Context, Cell) -> Void) -> Self {
func onDidEndDisplay(_ closure: @escaping (Context, C) -> Void) -> Self {
observer.onDidEndDisplay = { context, cell in
if let cell = cell as? Cell {
if let cell = cell as? C {
closure(context, cell)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/MicroTests/MicroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class MicroTests: XCTestCase {
dataSource.state = forEach(models: movies) { movie in
switch movie.kind {
case .show(let name):
return Item<MovieCell>() { context, cell in
return Cell<MovieCell>() { context, cell in
cell.nameLabel.text = name
}
.onSelect { _ in
Expand All @@ -63,9 +63,9 @@ final class MicroTests: XCTestCase {
CGSize(width: context.collectionView.frame.size.width, height: 40)
}
case .loading:
return Item<LoadingCell>() { _, _ in }
return Cell<LoadingCell>() { _, _ in }
case .ad:
return Item<AdCell>() { _, _ in }
return Cell<AdCell>() { _, _ in }
}
}
}
Expand Down

0 comments on commit cb51cac

Please sign in to comment.