Skip to content

Commit

Permalink
Merge pull request #41 from rzulkoski/bugfix/remove-line-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
dkk committed May 19, 2023
2 parents 44987fb + c553ea4 commit 6d87fab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 72 deletions.
58 changes: 47 additions & 11 deletions Sources/WrappingHStack/InternalWrappingHStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,81 @@ import SwiftUI
/// This View draws the WrappingHStack content taking into account the passed width, alignment and spacings.
/// Note that the passed LineManager and ContentManager should be reused whenever possible.
struct InternalWrappingHStack: View {
let width: CGFloat
let alignment: HorizontalAlignment
let spacing: WrappingHStack.Spacing
let lineSpacing: CGFloat
let lineManager: LineManager
let firstItemOfEachLine: [Int]
let contentManager: ContentManager

init(width: CGFloat, alignment: HorizontalAlignment, spacing: WrappingHStack.Spacing, lineSpacing: CGFloat, lineManager: LineManager, contentManager: ContentManager) {
init(width: CGFloat, alignment: HorizontalAlignment, spacing: WrappingHStack.Spacing, lineSpacing: CGFloat, contentManager: ContentManager) {
self.width = width
self.alignment = alignment
self.spacing = spacing
self.lineSpacing = lineSpacing
self.contentManager = contentManager
self.lineManager = lineManager
self.firstItemOfEachLine = {
var firstOfEach = [Int]()
var currentWidth: Double = width
for (index, element) in contentManager.items.enumerated() {
switch element {
case .newLine:
firstOfEach += [index]
currentWidth = width
case .any where contentManager.isVisible(viewIndex: index):
let itemWidth = contentManager.widths[index]
if currentWidth + itemWidth + spacing.minSpacing > width {
currentWidth = itemWidth
firstOfEach.append(index)
} else {
currentWidth += itemWidth + spacing.minSpacing
}
default:
break
}
}

if !lineManager.isSetUp {
lineManager.setup(contentManager: contentManager, width: width, spacing: spacing)
}
return firstOfEach
}()
}

func shouldHaveSideSpacers(line i: Int) -> Bool {
if case .constant = spacing {
return true
}
if case .dynamic = spacing, lineManager.hasExactlyOneElement(line: i) {
if case .dynamic = spacing, hasExactlyOneElement(line: i) {
return true
}
return false
}

var totalLines: Int {
firstItemOfEachLine.count
}

func startOf(line i: Int) -> Int {
firstItemOfEachLine[i]
}

func endOf(line i: Int) -> Int {
i == totalLines - 1 ? contentManager.items.count - 1 : firstItemOfEachLine[i + 1] - 1
}

func hasExactlyOneElement(line i: Int) -> Bool {
startOf(line: i) == endOf(line: i)
}

var body: some View {
VStack(alignment: alignment, spacing: lineSpacing) {
ForEach(0 ..< lineManager.totalLines, id: \.self) { lineIndex in
ForEach(0 ..< totalLines, id: \.self) { lineIndex in
HStack(spacing: 0) {
if alignment == .center || alignment == .trailing, shouldHaveSideSpacers(line: lineIndex) {
Spacer(minLength: 0)
}

ForEach(lineManager.startOf(line: lineIndex) ... lineManager.endOf(line: lineIndex), id: \.self) {
ForEach(startOf(line: lineIndex) ... endOf(line: lineIndex), id: \.self) {
if case .dynamicIncludingBorders = spacing,
lineManager.startOf(line: lineIndex) == $0
startOf(line: lineIndex) == $0
{
Spacer(minLength: spacing.minSpacing)
}
Expand All @@ -50,7 +86,7 @@ struct InternalWrappingHStack: View {
anyView
}

if lineManager.endOf(line: lineIndex) != $0 {
if endOf(line: lineIndex) != $0 {
if case .any = contentManager.items[$0], !contentManager.isVisible(viewIndex: $0) { } else {
if case .constant(let exactSpacing) = spacing {
Spacer(minLength: 0)
Expand Down
59 changes: 0 additions & 59 deletions Sources/WrappingHStack/LineManager.swift

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/WrappingHStack/WrappingHStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public struct WrappingHStack: View {
let alignment: HorizontalAlignment
let spacing: Spacing
let lineSpacing: CGFloat
let lineManager = LineManager()
let contentManager: ContentManager
@State private var height: CGFloat = 0

Expand All @@ -46,7 +45,6 @@ public struct WrappingHStack: View {
alignment: alignment,
spacing: spacing,
lineSpacing: lineSpacing,
lineManager: lineManager,
contentManager: contentManager
)
.anchorPreference(
Expand Down

0 comments on commit 6d87fab

Please sign in to comment.