Skip to content

Commit

Permalink
Move generalization logic to TagCloudView and make FlowLayoutView int…
Browse files Browse the repository at this point in the history
…ernal
  • Loading branch information
yarspirin committed Jul 20, 2023
1 parent d95a698 commit c338e05
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Sources/TagCloud/FlowLayoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import SwiftUI

public struct FlowLayoutView<Data, Content>: View where Data: RandomAccessCollection, Content: View, Data.Index: Hashable {
struct FlowLayoutView<Data, Content>: View where Data: RandomAccessCollection, Content: View, Data.Index: Hashable {
@State private var height: CGFloat = .zero

private let data: Data
private let verticalSpacing: CGFloat
private let horizontalSpacing: CGFloat
private let content: (Data.Element) -> Content

public init(
init(
data: Data,
verticalSpacing: CGFloat = 4,
horizontalSpacing: CGFloat = 4,
Expand All @@ -27,7 +27,7 @@ public struct FlowLayoutView<Data, Content>: View where Data: RandomAccessCollec
self.content = content
}

public var body: some View {
var body: some View {
GeometryReader { geometry in
content(in: geometry)
}
Expand Down
87 changes: 67 additions & 20 deletions Sources/TagCloud/TagCloudView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,70 @@

import SwiftUI

public struct TagCloudView: View {
private let tags: [String]
private let textColor: Color
private let foregroundColor: Color
private let borderWidth: CGFloat
private let borderColor: Color
private let cornerRadius: CGFloat
public struct TagCloudView<Data, Content>: View where Data: RandomAccessCollection, Content: View, Data.Index: Hashable {
private enum TagContent {
case `default`(DefaultTagContentConfiguration)
case custom(CustomTagContentConfiguration)
}

private struct DefaultTagContentConfiguration {
let tags: [String]
let textColor: Color
let foregroundColor: Color
let borderWidth: CGFloat
let borderColor: Color
let cornerRadius: CGFloat
}

private struct CustomTagContentConfiguration {
let data: Data
let verticalSpacing: CGFloat
let horizontalSpacing: CGFloat
let content: (Data.Element) -> Content
}

private let tagContent: TagContent

public init(
data: Data,
verticalSpacing: CGFloat = 4,
horizontalSpacing: CGFloat = 4,
@ViewBuilder content: @escaping (Data.Element) -> Content
) {
self.tagContent = .custom(
CustomTagContentConfiguration(
data: data,
verticalSpacing: verticalSpacing,
horizontalSpacing: horizontalSpacing,
content: content)
)
}

public var body: some View {
switch tagContent {
case .custom(let configuration):
FlowLayoutView(
data: configuration.data,
verticalSpacing: configuration.verticalSpacing,
horizontalSpacing: configuration.horizontalSpacing,
content: configuration.content
)
case .default(let configuration):
FlowLayoutView(data: configuration.tags) { tag in
TagView(
tag: tag,
textColor: configuration.textColor,
foregroundColor: configuration.foregroundColor,
borderWidth: configuration.borderWidth,
borderColor: configuration.borderColor,
cornerRadius: configuration.cornerRadius
)
}
}
}
}

extension TagCloudView where Data == [String], Content == TagView {
public init(
tags: [String],
textColor: Color = .black,
Expand All @@ -23,24 +79,15 @@ public struct TagCloudView: View {
borderColor: Color = .black,
cornerRadius: CGFloat = 10
) {
self.tags = tags
self.textColor = textColor
self.foregroundColor = foregroundColor
self.borderWidth = borderWidth
self.borderColor = borderColor
self.cornerRadius = cornerRadius
}

public var body: some View {
FlowLayoutView(data: tags) { tag in
TagView(
tag: tag,
self.tagContent = .default(
DefaultTagContentConfiguration(
tags: tags,
textColor: textColor,
foregroundColor: foregroundColor,
borderWidth: borderWidth,
borderColor: borderColor,
cornerRadius: cornerRadius
)
}
)
}
}
6 changes: 3 additions & 3 deletions Sources/TagCloud/TagView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import SwiftUI

struct TagView: View {
public struct TagView: View {
private let tag: String
private let textColor: Color
private let foregroundColor: Color
private let borderWidth: CGFloat
private let borderColor: Color
private let cornerRadius: CGFloat

init(
public init(
tag: String,
textColor: Color = .black,
foregroundColor: Color = .white,
Expand All @@ -31,7 +31,7 @@ struct TagView: View {
self.cornerRadius = cornerRadius
}

var body: some View {
public var body: some View {
Text(tag)
.foregroundColor(textColor)
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
Expand Down
2 changes: 1 addition & 1 deletion Tests/TagCloudTests/TagCloudTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fileprivate struct Tag {
final class TagCloudTests: XCTestCase {
func testFlowLayoutViewCreation() {
let tags = (1 ... 5).map { Tag(num: $0) }
let flowLayoutView = FlowLayoutView(data: tags) { tag in
let flowLayoutView = TagCloudView(data: tags) { tag in
Text("\(tag.num)")
}
XCTAssertNotNil(flowLayoutView)
Expand Down

0 comments on commit c338e05

Please sign in to comment.