From 81f2063aa00b0546defaa312cad47693663f38a8 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Fri, 27 Aug 2021 15:32:37 -0700 Subject: [PATCH] make UIComponent more generic --- .../Components/UserProfile.swift | 2 +- .../Core/ComponentView/ComponentEngine.swift | 2 +- .../ViewRenderNode/AnyViewRenderNode.swift | 4 ++-- .../Model/ViewRenderNode/ViewRenderNode.swift | 23 ++++++++++++++----- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Examples/UIComponentExample/Examples/Complex layout/Components/UserProfile.swift b/Examples/UIComponentExample/Examples/Complex layout/Components/UserProfile.swift index 5018462f..e228072e 100644 --- a/Examples/UIComponentExample/Examples/Complex layout/Components/UserProfile.swift +++ b/Examples/UIComponentExample/Examples/Complex layout/Components/UserProfile.swift @@ -4,7 +4,7 @@ import UIComponent import Kingfisher import UIKit.UIImageView -extension ViewComponent { +extension ViewComponent where R.View: UIView { fileprivate func shadowAvatar() -> ViewUpdateComponent { update { $0.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor diff --git a/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift b/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift index e5908fae..44d050a7 100644 --- a/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift +++ b/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift @@ -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 diff --git a/Sources/UIComponent/Core/Model/ViewRenderNode/AnyViewRenderNode.swift b/Sources/UIComponent/Core/Model/ViewRenderNode/AnyViewRenderNode.swift index d31d8196..8b13a1ad 100644 --- a/Sources/UIComponent/Core/Model/ViewRenderNode/AnyViewRenderNode.swift +++ b/Sources/UIComponent/Core/Model/ViewRenderNode/AnyViewRenderNode.swift @@ -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 { diff --git a/Sources/UIComponent/Core/Model/ViewRenderNode/ViewRenderNode.swift b/Sources/UIComponent/Core/Model/ViewRenderNode/ViewRenderNode.swift index 8cecd387..00f6796d 100644 --- a/Sources/UIComponent/Core/Model/ViewRenderNode/ViewRenderNode.swift +++ b/Sources/UIComponent/Core/Model/ViewRenderNode/ViewRenderNode.swift @@ -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) } @@ -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) - } }