From 7aba64dd4aaaa2ee8b70812178abccf7702631a8 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Thu, 5 Sep 2024 15:02:35 +0330 Subject: [PATCH 1/5] feat: make the horizontal spacing exactly as expected and remove the unintended horizontal paddings --- .../SwiftUIFlowLayout/SwiftUIFlowLayout.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift index eee356c..4c6335a 100644 --- a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift +++ b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift @@ -40,14 +40,23 @@ public struct FlowLayout: View } private func content(in g: GeometryProxy) -> some View { - var width = CGFloat.zero var height = CGFloat.zero + var width = CGFloat.zero { + didSet { + if width == 0 { + row = 0 + } else { + row += 1 + } + } + } + var row = 0 var lastHeight = CGFloat.zero let itemCount = items.count return ZStack(alignment: .topLeading) { ForEach(Array(items.enumerated()), id: \.offset) { index, item in viewMapping(item) - .padding([.horizontal, .vertical], itemSpacing) + .padding([.vertical], itemSpacing) .alignmentGuide(.leading, computeValue: { d in if (abs(width - d.width) > g.size.width) { width = 0 @@ -55,12 +64,13 @@ public struct FlowLayout: View } lastHeight = d.height let result = width + let extraWidth = -itemSpacing * CGFloat(row) if index == itemCount - 1 { width = 0 } else { width -= d.width } - return result + return result + extraWidth }) .alignmentGuide(.top, computeValue: { d in let result = height From cb5dda6b4192ba282a8d9d047fac0bcdb9f43341 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Thu, 5 Sep 2024 15:03:21 +0330 Subject: [PATCH 2/5] feat: make the vertical spacing exactly as expected and remove the unintended vertical paddings --- Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift index 4c6335a..28ee8fc 100644 --- a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift +++ b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift @@ -40,7 +40,6 @@ public struct FlowLayout: View } private func content(in g: GeometryProxy) -> some View { - var height = CGFloat.zero var width = CGFloat.zero { didSet { if width == 0 { @@ -51,12 +50,21 @@ public struct FlowLayout: View } } var row = 0 + var height = CGFloat.zero{ + didSet { + if height == 0 { + column = 0 + } else { + column += 1 + } + } + } + var column = 0 var lastHeight = CGFloat.zero let itemCount = items.count return ZStack(alignment: .topLeading) { ForEach(Array(items.enumerated()), id: \.offset) { index, item in viewMapping(item) - .padding([.vertical], itemSpacing) .alignmentGuide(.leading, computeValue: { d in if (abs(width - d.width) > g.size.width) { width = 0 @@ -74,10 +82,11 @@ public struct FlowLayout: View }) .alignmentGuide(.top, computeValue: { d in let result = height + let extraHeight = -itemSpacing * CGFloat(column) if index == itemCount - 1 { height = 0 } - return result + return result + extraHeight }) } } From d47ee2e5c466f0c5b0a898e4a9f568890608aab2 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Thu, 5 Sep 2024 15:03:52 +0330 Subject: [PATCH 3/5] feat: make the default spacing 8 to match the original author intention --- Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift index 28ee8fc..60e936b 100644 --- a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift +++ b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift @@ -1,6 +1,6 @@ import SwiftUI -public let flowLayoutDefaultItemSpacing: CGFloat = 4 +public let flowLayoutDefaultItemSpacing: CGFloat = 8 public struct FlowLayout: View { let mode: Mode From 398a7cd03a8e7acdf24001f62ececd848b4978ab Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Thu, 5 Sep 2024 15:06:41 +0330 Subject: [PATCH 4/5] docs: use better names for the local variable to show what is it holding --- Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift index 60e936b..b1c8ff3 100644 --- a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift +++ b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift @@ -71,22 +71,22 @@ public struct FlowLayout: View height -= lastHeight } lastHeight = d.height - let result = width + let currentWidth = width let extraWidth = -itemSpacing * CGFloat(row) if index == itemCount - 1 { width = 0 } else { width -= d.width } - return result + extraWidth + return currentWidth + extraWidth }) .alignmentGuide(.top, computeValue: { d in - let result = height + let currentHeight = height let extraHeight = -itemSpacing * CGFloat(column) if index == itemCount - 1 { height = 0 } - return result + extraHeight + return currentHeight + extraHeight }) } } From 95d0c4c49ff138cb649aaabcb04ce52fdb6ccc78 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Mon, 30 Sep 2024 16:15:29 +0330 Subject: [PATCH 5/5] fix: swap property names to match the value inside --- .../SwiftUIFlowLayout/SwiftUIFlowLayout.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift index b1c8ff3..f1011f0 100644 --- a/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift +++ b/Sources/SwiftUIFlowLayout/SwiftUIFlowLayout.swift @@ -43,23 +43,23 @@ public struct FlowLayout: View var width = CGFloat.zero { didSet { if width == 0 { - row = 0 + column = 0 } else { - row += 1 + column += 1 } } } - var row = 0 + var column = 0 var height = CGFloat.zero{ didSet { if height == 0 { - column = 0 + row = 0 } else { - column += 1 + row += 1 } } } - var column = 0 + var row = 0 var lastHeight = CGFloat.zero let itemCount = items.count return ZStack(alignment: .topLeading) { @@ -72,7 +72,7 @@ public struct FlowLayout: View } lastHeight = d.height let currentWidth = width - let extraWidth = -itemSpacing * CGFloat(row) + let extraWidth = -itemSpacing * CGFloat(column) if index == itemCount - 1 { width = 0 } else { @@ -82,7 +82,7 @@ public struct FlowLayout: View }) .alignmentGuide(.top, computeValue: { d in let currentHeight = height - let extraHeight = -itemSpacing * CGFloat(column) + let extraHeight = -itemSpacing * CGFloat(row) if index == itemCount - 1 { height = 0 }