Skip to content

Commit b841326

Browse files
committed
Add support for advacend usage to get the native UIView/NSView life cycle for AnimatedImage
1 parent 6dc0002 commit b841326

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ final class AnimatedImageModel : ObservableObject {
2020
@Published var progressBlock: ((Int, Int) -> Void)?
2121
}
2222

23+
// Coordinator Life Cycle Binding Object
24+
final class AnimatedImageCoordinator : ObservableObject {
25+
@Published var viewCreateBlock: ((PlatformView) -> Void)?
26+
@Published var viewUpdateBlock: ((PlatformView) -> Void)?
27+
}
28+
2329
// Layout Binding Object
2430
final class AnimatedImageLayout : ObservableObject {
2531
@Published var contentMode: ContentMode = .fill
@@ -58,6 +64,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
5864
@ObservedObject var imageModel = AnimatedImageModel()
5965
@ObservedObject var imageLayout = AnimatedImageLayout()
6066
@ObservedObject var imageConfiguration = AnimatedImageConfiguration()
67+
@ObservedObject var imageCoordinator = AnimatedImageCoordinator()
6168

6269
var url: URL?
6370
var placeholder: PlatformImage?
@@ -196,7 +203,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
196203
}
197204

198205
func makeView(context: PlatformViewRepresentableContext<AnimatedImage>) -> AnimatedImageViewWrapper {
199-
AnimatedImageViewWrapper()
206+
let view = AnimatedImageViewWrapper()
207+
if let viewCreateBlock = imageCoordinator.viewCreateBlock {
208+
viewCreateBlock(view)
209+
}
210+
return view
200211
}
201212

202213
func updateView(_ view: AnimatedImageViewWrapper, context: PlatformViewRepresentableContext<AnimatedImage>) {
@@ -238,6 +249,9 @@ public struct AnimatedImage : PlatformViewRepresentable {
238249

239250
configureView(view, context: context)
240251
layoutView(view, context: context)
252+
if let viewUpdateBlock = imageCoordinator.viewUpdateBlock {
253+
viewUpdateBlock(view)
254+
}
241255
}
242256

243257
static func dismantleView(_ view: AnimatedImageViewWrapper, coordinator: ()) {
@@ -554,6 +568,26 @@ extension AnimatedImage {
554568
}
555569
}
556570

571+
// View Coordinator Handler
572+
extension AnimatedImage {
573+
574+
/// Provide the action when view representable create the native view.
575+
/// - Parameter action: The action to perform. The first arg is the native view.
576+
/// - Returns: A view that triggers `action` when view representable create the native view.
577+
public func onViewCreate(perform action: ((PlatformView) -> Void)? = nil) -> AnimatedImage {
578+
imageCoordinator.viewCreateBlock = action
579+
return self
580+
}
581+
582+
/// Provide the action when view representable update the native view.
583+
/// - Parameter action: The action to perform. The first arg is the native view.
584+
/// - Returns: A view that triggers `action` when view representable update the native view.
585+
public func onViewUpdate(perform action: ((PlatformView) -> Void)? = nil) -> AnimatedImage {
586+
imageCoordinator.viewUpdateBlock = action
587+
return self
588+
}
589+
}
590+
557591
#if os(macOS) || os(iOS) || os(tvOS)
558592
// Web Image convenience
559593
extension AnimatedImage {

0 commit comments

Comments
 (0)