Skip to content

Commit

Permalink
fix animateUpdate overriding default animator
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Oct 20, 2022
1 parent 7d278f2 commit 9cccb92
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
22 changes: 10 additions & 12 deletions Sources/UIComponent/Animators/WrapperAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,39 @@ import UIKit

public class WrapperAnimator: Animator {
public var content: Animator?
public var passthrough: Bool = false
public var passthroughUpdate: Bool = false
public var insertBlock: ((ComponentDisplayableView, UIView, CGRect) -> Void)?
public var updateBlock: ((ComponentDisplayableView, UIView, CGRect) -> Void)?
public var deleteBlock: ((ComponentDisplayableView, UIView, () -> Void) -> Void)?

public override func shift(componentView: ComponentDisplayableView, delta: CGPoint, view: UIView) {
(content ?? componentView.animator).shift(componentView: componentView, delta: delta, view: view)
}

public override func update(componentView: ComponentDisplayableView, view: UIView, frame: CGRect) {
if let updateBlock {
updateBlock(componentView, view, frame)
if passthrough, let content = content {
content.update(componentView: componentView, view: view, frame: frame)
if passthroughUpdate {
(content ?? componentView.animator).update(componentView: componentView, view: view, frame: frame)
}
} else if let content {
content.update(componentView: componentView, view: view, frame: frame)
} else {
componentView.animator.update(componentView: componentView, view: view, frame: frame)
(content ?? componentView.animator).update(componentView: componentView, view: view, frame: frame)
}
}

public override func insert(componentView: ComponentDisplayableView, view: UIView, frame: CGRect) {
if let insertBlock {
insertBlock(componentView, view, frame)
} else if let content {
content.insert(componentView: componentView, view: view, frame: frame)
} else {
componentView.animator.insert(componentView: componentView, view: view, frame: frame)
(content ?? componentView.animator).insert(componentView: componentView, view: view, frame: frame)
}
}

public override func delete(componentView: ComponentDisplayableView, view: UIView, completion: @escaping () -> Void) {
if let deleteBlock {
deleteBlock(componentView, view, completion)
} else if let content {
content.delete(componentView: componentView, view: view, completion: completion)
} else {
componentView.animator.delete(componentView: componentView, view: view, completion: completion)
(content ?? componentView.animator).delete(componentView: componentView, view: view, completion: completion)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public class ComponentEngine {
if updateViews {
// view was on screen before reload, need to update the view.
viewData.renderNode._updateView(view)
(viewData.animator ?? animator).shift(componentView: componentView, delta: contentOffsetDelta, view: view)
animator.shift(componentView: componentView, delta: contentOffsetDelta, view: view)
}
} else {
view = viewData.renderNode._makeView() as! UIView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ extension ViewComponent {
$0.animateUpdate(passthrough: passthrough, updateBlock)
}
}
public func animateInsert(passthrough: Bool = false, _ insertBlock: @escaping ((ComponentDisplayableView, UIView, CGRect) -> Void)) -> ViewAnimatorWrapperComponent<Self> {
ViewModifierComponent(content: self) {
$0.animateInsert(passthrough: passthrough, insertBlock)
}
}
public func animateDelete(passthrough: Bool = false, _ deleteBlock: @escaping ((ComponentDisplayableView, UIView, () -> Void) -> Void)) -> ViewAnimatorWrapperComponent<Self> {
ViewModifierComponent(content: self) {
$0.animateDelete(passthrough: passthrough, deleteBlock)
}
}
}

extension ViewComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ extension ViewRenderNode {

public struct ViewAnimatorWrapperRenderNode<Content: ViewRenderNode>: ViewRenderNodeWrapper {
public let content: Content
var passthrough: Bool
var passthroughUpdate: Bool
var insertBlock: ((ComponentDisplayableView, UIView, CGRect) -> Void)?
var updateBlock: ((ComponentDisplayableView, UIView, CGRect) -> Void)?
var deleteBlock: ((ComponentDisplayableView, UIView, () -> Void) -> Void)?
public var animator: Animator? {
let wrapper = WrapperAnimator()
wrapper.content = content.animator
wrapper.passthrough = passthrough
wrapper.passthroughUpdate = passthroughUpdate
wrapper.insertBlock = insertBlock
wrapper.deleteBlock = deleteBlock
wrapper.updateBlock = updateBlock
Expand All @@ -110,12 +110,6 @@ public struct ViewAnimatorWrapperRenderNode<Content: ViewRenderNode>: ViewRender

extension ViewRenderNode {
func animateUpdate(passthrough: Bool = false, _ updateBlock: @escaping ((ComponentDisplayableView, UIView, CGRect) -> Void)) -> ViewAnimatorWrapperRenderNode<Self> {
ViewAnimatorWrapperRenderNode(content: self, passthrough: passthrough, updateBlock: updateBlock)
}
func animateInsert(passthrough: Bool = false, _ insertBlock: @escaping ((ComponentDisplayableView, UIView, CGRect) -> Void)) -> ViewAnimatorWrapperRenderNode<Self> {
ViewAnimatorWrapperRenderNode(content: self, passthrough: passthrough, insertBlock: insertBlock)
}
func animateDelete(passthrough: Bool = false, _ deleteBlock: @escaping ((ComponentDisplayableView, UIView, () -> Void) -> Void)) -> ViewAnimatorWrapperRenderNode<Self> {
ViewAnimatorWrapperRenderNode(content: self, passthrough: passthrough, deleteBlock: deleteBlock)
ViewAnimatorWrapperRenderNode(content: self, passthroughUpdate: passthrough, updateBlock: updateBlock)
}
}

0 comments on commit 9cccb92

Please sign in to comment.