Skip to content

Commit

Permalink
New coordinator api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed Nov 3, 2019
1 parent 05af365 commit 4ee2bd4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repositoryURL": "https://github.com/alexdrone/CoreRender",
"state": {
"branch": "master",
"revision": "99de775c57b5dcee4be40fed3026aad053b5addd",
"revision": "05af365d9f8a70e76ca75a18acdcd60597c189aa",
"version": null
}
}
Expand Down
15 changes: 3 additions & 12 deletions Demo/CoreRenderDemo/DemoWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,13 @@ class CounterState: State {
var count: UInt = 0
}

class CounterCoordinator: Coordinator<NullProps, CounterState>, CoordinatorProtocol {
static let descriptor: AnyCoordinatorDescriptor =
CoordinatorDescriptor<CounterCoordinator, NullProps, CounterState>()

override init() {
super.init()
self.state = CounterState()
self.props = NullProps.null
}

class CounterCoordinator: Coordinator<CounterState, NullProps> {
func increase() {
self.state.count += 1
state.count += 1
body?.setNeedsLayout()
}
}


// MARK: - Constants

struct Const {
Expand Down
13 changes: 2 additions & 11 deletions Demo/CoreRenderDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@ class ViewCoordinator: UIViewController {
var hostingView: HostingView!
let context = Context()
var coordinator: CounterCoordinator {
let c = context.coordinator(CounterCoordinator.descriptor.toRef())
guard let _ = c as? CounterCoordinator else {
print(type(of: c))
let a = CounterCoordinator()
print(type(of: a))
print(c is CounterCoordinator)
print(c is CoordinatorProtocol)
print(c.state)
fatalError()
}
return coordinator
context.coordinator(makeCoordinatorDescriptor(CounterCoordinator.self).toRef())
as! CounterCoordinator
}

override func loadView() {
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The `withLayoutSpec` modifier allows to specify a custom configuration closure f
*Coordinators* are similar to Components in React/Render/Litho and Coordinators in SwiftUI.

```swift
class CounterCoordinator: Coordinator<NullProps, CounterState> {
class CounterCoordinator: Coordinator<CounterState, NullProps> {

func incrementCounter() {
self.state.count += 1 // Update the state.
Expand All @@ -74,13 +74,14 @@ class CounterViewCoordinator: UIViewController {
private var hostingView: HostingView {
view as! HostingView
}
private let coordinator: CounterCoordinator {
context.coordinator(ofType: CounterCoordinator.self, withKey: "counter")
var coordinator: CounterCoordinator {
context.coordinator(makeCoordinatorDescriptor(CounterCoordinator.self).toRef())
as! CounterCoordinator
}

func loadView() {
view = HostingView(context: context, options: [.useSafeAreaInsets]) { ctx in
makeCounter(ctx: ctx)
makeCounter(ctx: ctx, coordinator: coordinator)
}
}

Expand Down

0 comments on commit 4ee2bd4

Please sign in to comment.