Skip to content

Instantly share code, notes, and snippets.

@jacobsapps
Created May 25, 2022 08:27
Show Gist options
  • Save jacobsapps/5d2d4373b546763398d3e7973e53693f to your computer and use it in GitHub Desktop.
Save jacobsapps/5d2d4373b546763398d3e7973e53693f to your computer and use it in GitHub Desktop.
SkeletonableView
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