Skip to content

Commit 079444c

Browse files
giiiitagiiiita
authored andcommitted
Use lastRowAtIndex for the last row
1 parent f31bd7a commit 079444c

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

Sources/QGrid/QGrid.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ public struct QGrid<Data, Content>: View
9696
self.rowAtIndex(row.id * self.cols,
9797
geometry: geometry)
9898
}
99+
99100
// Handle last row
100101
if (self.data.count % self.cols > 0) {
101-
self.rowAtIndex(self.rows * self.cols,
102-
geometry: geometry,
103-
isLastRow: true)
102+
self.lastRowAtIndex(self.rows * self.cols,
103+
geometry: geometry,
104+
lastRowContentCount: self.data.count % self.cols)
104105
}
105106
}
106107
}
@@ -112,17 +113,26 @@ public struct QGrid<Data, Content>: View
112113
// MARK: - `BODY BUILDER` 💪 FUNCTIONS
113114

114115
private func rowAtIndex(_ index: Int,
115-
geometry: GeometryProxy,
116-
isLastRow: Bool = false) -> some View {
116+
geometry: GeometryProxy) -> some View {
117117
HStack(spacing: self.hSpacing) {
118118
ForEach((0..<cols).map { QGridIndex(id: $0) }) { column in
119119
self.contentAtIndex(index + column.id)
120120
.frame(width: self.contentWidthForGeometry(geometry))
121-
// Dirty little hack to handle layouting of the last row gracefully :
122-
.opacity(!isLastRow || column.id < self.data.count % self.cols ? 1.0 : 0.0)
123121
}
124122
}
125123
}
124+
125+
private func lastRowAtIndex(_ index: Int,
126+
geometry: GeometryProxy,
127+
lastRowContentCount: Int) -> some View {
128+
HStack(spacing: self.hSpacing) {
129+
ForEach((0..<lastRowContentCount).map { QGridIndex(id: $0) }) { column in
130+
self.contentAtIndex(index + column.id)
131+
.frame(width: self.contentWidthForGeometry(geometry))
132+
}
133+
Spacer()
134+
}
135+
}
126136

127137
private func contentAtIndex(_ index: Int) -> Content {
128138
// (Addressing the workaround with transparent content in the last row) :

0 commit comments

Comments
 (0)