Skip to content

Commit

Permalink
add reload method and hold(value:) modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Mar 15, 2023
1 parent 020fc0e commit 62186de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Sources/UIComponent/Core/Model/Component/Component+Modifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,24 @@ extension Component {
VisibleBoundsObserverComponent(child: self, onVisibleBoundsChanged: callback)
}
}

extension Component {
/// A block that can be captured inside `layout` or `build` method to trigger a reload on the linked componentView
public var reload: () -> Void {
guard let componentView = currentComponentView() else {
assertionFailure("reloadComponent should only be captured within `layout` or `build` method")
return {}
}
return { [weak componentView] in
componentView?.reloadData()
}
}
}

extension Component {
/// Hold any value while the Component is displayed
/// - Parameter value: value to be holding
public func hold(value: Any) -> Component {
HoldValueComponent(child: self, value: value)
}
}
21 changes: 21 additions & 0 deletions Sources/UIComponent/Core/Util/HoldValueComponent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// File.swift
//
//
// Created by Luke Zhao on 3/14/23.
//

import Foundation

struct HoldValueComponent: Component {
let child: Component
let value: Any
func layout(_ constraint: Constraint) -> RenderNode {
HoldValueRenderNode(child: child.layout(constraint), value: value)
}
}

struct HoldValueRenderNode: WrapperRenderNode {
let child: RenderNode
let value: Any?
}
26 changes: 26 additions & 0 deletions Sources/UIComponent/Core/Util/WrapperRenderNode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// File.swift
//
//
// Created by Luke Zhao on 3/14/23.
//

import Foundation

public protocol WrapperRenderNode: RenderNode {
var child: RenderNode { get }
}

public extension WrapperRenderNode {
var size: CGSize {
child.size
}

var positions: [CGPoint] {
[.zero]
}

var children: [RenderNode] {
[child]
}
}

0 comments on commit 62186de

Please sign in to comment.