Created
May 25, 2022 08:27
-
-
Save jacobsapps/5d2d4373b546763398d3e7973e53693f to your computer and use it in GitHub Desktop.
SkeletonableView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct SkeletonableView<Content: View, Model: Collection>: View { | |
private var data: Model | |
private var placeholder: Model | |
@State private var timeoutRemaining: Int = 5 | |
private let content: (Model) -> Content | |
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() | |
public init(data: Model, | |
placeholder: Model, | |
@ViewBuilder content: @escaping (Model) -> Content) { | |
self.data = data | |
self.placeholder = placeholder | |
self.content = content | |
} | |
public var body: some View { | |
Group { | |
if data.isEmpty == true, timeoutRemaining > 0 { | |
content(placeholder) | |
.redacted(reason: .placeholder) | |
.allowsHitTesting(false) | |
} else { | |
content(data) | |
} | |
} | |
.onReceive(timer) { _ in | |
if timeoutRemaining != 0 { | |
withAnimation { | |
timeoutRemaining -= 1 | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment