Skip to content

Commit

Permalink
make UIComponent more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Aug 27, 2021
1 parent 1e7b7f1 commit 81f2063
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import UIComponent
import Kingfisher
import UIKit.UIImageView

extension ViewComponent {
extension ViewComponent where R.View: UIView {
fileprivate func shadowAvatar() -> ViewUpdateComponent<Self> {
update {
$0.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public class ComponentEngine {
viewData.renderNode._updateView(view)
}
} else {
view = viewData.renderNode._makeView()
view = viewData.renderNode._makeView() as! UIView
UIView.performWithoutAnimation {
view.bounds.size = frame.bounds.size
view.center = frame.center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public protocol AnyViewRenderNode: RenderNode {
var keyPath: String { get }
var animator: Animator? { get }
var reuseKey: String? { get }
func _makeView() -> UIView
func _updateView(_ view: UIView)
func _makeView() -> Any
func _updateView(_ view: Any)
}

public extension AnyViewRenderNode {
Expand Down
23 changes: 17 additions & 6 deletions Sources/UIComponent/Core/Model/ViewRenderNode/ViewRenderNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import UIKit

public protocol UIComponentRenderableView {
init()
}
extension UIView: UIComponentRenderableView {}

@dynamicMemberLookup
public protocol ViewRenderNode: AnyViewRenderNode {
associatedtype View: UIView
associatedtype View: UIComponentRenderableView
func makeView() -> View
func updateView(_ view: View)
}
Expand All @@ -14,15 +19,21 @@ public extension ViewRenderNode {
func makeView() -> View {
View()
}
func _makeView() -> Any {
return makeView()
}
// MARK: AnyViewRenderNode
func _makeView() -> UIView {
func _updateView(_ view: Any) {
guard let view = view as? View else { return }
return updateView(view)
}
}

public extension ViewRenderNode where View: UIView {
func _makeView() -> Any {
if let reuseKey = reuseKey {
return ReuseManager.shared.dequeue(identifier: reuseKey, makeView())
}
return makeView()
}
func _updateView(_ view: UIView) {
guard let view = view as? View else { return }
return updateView(view)
}
}

0 comments on commit 81f2063

Please sign in to comment.