From de7d906d4e5d8fc6db86f24d58389b3c347b5f80 Mon Sep 17 00:00:00 2001 From: Viktor Starovoitov Date: Wed, 31 Jul 2024 18:34:32 +0300 Subject: [PATCH] Fix for #36 Two SkeletonForEach in a LazyVGrid causes duplicate IDs --- .../SkeletonUI/Extensions/View+SkeletonModifier.swift | 10 ++++++++++ Sources/SkeletonUI/Skeleton/SkeletonForEach.swift | 1 + 2 files changed, 11 insertions(+) diff --git a/Sources/SkeletonUI/Extensions/View+SkeletonModifier.swift b/Sources/SkeletonUI/Extensions/View+SkeletonModifier.swift index 4b47204..8ec1421 100644 --- a/Sources/SkeletonUI/Extensions/View+SkeletonModifier.swift +++ b/Sources/SkeletonUI/Extensions/View+SkeletonModifier.swift @@ -31,3 +31,13 @@ public extension View { .animation(transition.animation, value: loading) } } + +extension View { + @ViewBuilder func `if`(_ condition: Bool, transform: (Self) -> T) -> some View where T : View { + if condition { + transform(self) + } else { + self + } + } +} diff --git a/Sources/SkeletonUI/Skeleton/SkeletonForEach.swift b/Sources/SkeletonUI/Skeleton/SkeletonForEach.swift index 7e5d3d8..bd569cd 100644 --- a/Sources/SkeletonUI/Skeleton/SkeletonForEach.swift +++ b/Sources/SkeletonUI/Skeleton/SkeletonForEach.swift @@ -15,6 +15,7 @@ public struct SkeletonForEach: View where Data: RandomAccessColle public var body: some View { ForEach(0 ..< (data.isEmpty ? quantity : data.count), id: \.self) { index in self.content(self.data.isEmpty, self.data.isEmpty ? nil : self.data.map { $0 }[index]) + .if(self.data.isEmpty) { $0.id(UUID()) } } } }