From 9eb16bf15aef8b2719c56a7530d61e896f351a94 Mon Sep 17 00:00:00 2001 From: James J Porter Date: Mon, 30 Dec 2019 14:52:07 -0500 Subject: [PATCH] Fix grid ids (#65) * try to make item IDs work correctly * Add back tuple initializers --- Sources/Grid/Grid+Inits.swift | 83 ++++++++++++++++++++++++++++------- Sources/Grid/Grid.swift | 21 +++++---- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/Sources/Grid/Grid+Inits.swift b/Sources/Grid/Grid+Inits.swift index 073a661..d342339 100644 --- a/Sources/Grid/Grid+Inits.swift +++ b/Sources/Grid/Grid+Inits.swift @@ -2,48 +2,97 @@ import SwiftUI extension Grid { public init(_ data: Data, @ViewBuilder item: @escaping (Data.Element) -> Item) where Content == ForEach, Data : RandomAccessCollection, Item : View, Data.Element : Identifiable { - self.items = data.map({ AnyView(item($0)) }) + self.items = data.map { GridItem(view: AnyView(item($0)), id: AnyHashable($0.id)) } } public init(_ data: Data, id: KeyPath, @ViewBuilder item: @escaping (Data.Element) -> Item) where Content == ForEach, Data : RandomAccessCollection, ID : Hashable, Item : View { - self.items = data.map({ AnyView(item($0)) }) + self.items = data.map { GridItem(view: AnyView(item($0)), id: AnyHashable($0[keyPath: id])) } } public init(_ data: Range, @ViewBuilder item: @escaping (Int) -> Item) where Content == ForEach, Int, Item>, Item : View { - self.items = data.map({ AnyView(item($0)) }) + self.items = data.map { GridItem(view: AnyView(item($0)), id: AnyHashable($0)) } } } extension Grid { public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1))] } - + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2))] } - + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2), AnyView(content().value.3)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3))] } public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3, C4)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2), AnyView(content().value.3), AnyView(content().value.4)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3)), + GridItem(view: AnyView(content().value.4), id: AnyHashable(4))] } - + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3, C4, C5)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2), AnyView(content().value.3), AnyView(content().value.4), AnyView(content().value.5)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3)), + GridItem(view: AnyView(content().value.4), id: AnyHashable(4)), + GridItem(view: AnyView(content().value.5), id: AnyHashable(5))] } - + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3, C4, C5, C6)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2), AnyView(content().value.3), AnyView(content().value.4), AnyView(content().value.5), AnyView(content().value.6)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3)), + GridItem(view: AnyView(content().value.4), id: AnyHashable(4)), + GridItem(view: AnyView(content().value.5), id: AnyHashable(5)), + GridItem(view: AnyView(content().value.6), id: AnyHashable(6))] } - + + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3, C4, C5, C6, C7)> { + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3)), + GridItem(view: AnyView(content().value.4), id: AnyHashable(4)), + GridItem(view: AnyView(content().value.5), id: AnyHashable(5)), + GridItem(view: AnyView(content().value.6), id: AnyHashable(6)), + GridItem(view: AnyView(content().value.7), id: AnyHashable(7))] + } + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2), AnyView(content().value.3), AnyView(content().value.4), AnyView(content().value.5), AnyView(content().value.6), AnyView(content().value.7), AnyView(content().value.8)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3)), + GridItem(view: AnyView(content().value.4), id: AnyHashable(4)), + GridItem(view: AnyView(content().value.5), id: AnyHashable(5)), + GridItem(view: AnyView(content().value.6), id: AnyHashable(6)), + GridItem(view: AnyView(content().value.7), id: AnyHashable(7)), + GridItem(view: AnyView(content().value.8), id: AnyHashable(8))] } - + public init(@ViewBuilder content: () -> Content) where Content == TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)> { - self.items = [AnyView(content().value.0), AnyView(content().value.1), AnyView(content().value.2), AnyView(content().value.3), AnyView(content().value.4), AnyView(content().value.5), AnyView(content().value.6), AnyView(content().value.7), AnyView(content().value.8), AnyView(content().value.9)] + self.items = [GridItem(view: AnyView(content().value.0), id: AnyHashable(0)), + GridItem(view: AnyView(content().value.1), id: AnyHashable(1)), + GridItem(view: AnyView(content().value.2), id: AnyHashable(2)), + GridItem(view: AnyView(content().value.3), id: AnyHashable(3)), + GridItem(view: AnyView(content().value.4), id: AnyHashable(4)), + GridItem(view: AnyView(content().value.5), id: AnyHashable(5)), + GridItem(view: AnyView(content().value.6), id: AnyHashable(6)), + GridItem(view: AnyView(content().value.7), id: AnyHashable(7)), + GridItem(view: AnyView(content().value.8), id: AnyHashable(8)), + GridItem(view: AnyView(content().value.9), id: AnyHashable(9))] } } diff --git a/Sources/Grid/Grid.swift b/Sources/Grid/Grid.swift index a50f3ef..83ee59f 100644 --- a/Sources/Grid/Grid.swift +++ b/Sources/Grid/Grid.swift @@ -1,9 +1,14 @@ import SwiftUI +struct GridItem: Identifiable { + let view: AnyView + let id: AnyHashable +} + /// A view that arranges its children in a grid. public struct Grid: View where Content: View { @Environment(\.gridStyle) private var style - let items: [AnyView] + let items: [GridItem] @State private var itemsPreferences: [AnyHashable : GridItemPreferences] = [:] public var body: some View { @@ -26,15 +31,15 @@ public struct Grid: View where Content: View { private func grid(with geometry: GeometryProxy) -> some View { ScrollView(self.style.axis == .vertical ? .vertical : .horizontal) { ZStack(alignment: .topLeading) { - ForEach(0..