Skip to content

Commit 089ac58

Browse files
committed
Task 1.0 finished
1 parent 27f7bb8 commit 089ac58

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Sources/GenericCollectionViewKit/datasource/CHeaderView.swift

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public final class CHeaderView: UICollectionReusableView {
1212
// MARK: - Title Label
1313
/* A UILabel used to display the title of the section header.
1414
It is styled with bold font and truncated if too long.*/
15-
private let titleLabel: UILabel = {
15+
private lazy var titleLabel: UILabel = {
1616
let lbl = UILabel()
1717
lbl.translatesAutoresizingMaskIntoConstraints = false
1818
lbl.font = .boldSystemFont(ofSize: 20)
@@ -24,6 +24,17 @@ public final class CHeaderView: UICollectionReusableView {
2424
return lbl
2525
}()
2626

27+
// MARK: - Title Icon
28+
/* A UIImageView used to display an optional iconnext to the section header title.
29+
Supports both system images and asset-based images.Hidden by default when no icon is provided. */
30+
private lazy var titleIcon: UIImageView = {
31+
let imageView = UIImageView()
32+
imageView.translatesAutoresizingMaskIntoConstraints = false
33+
imageView.contentMode = .scaleAspectFit
34+
imageView.isHidden = true
35+
return imageView
36+
}()
37+
2738
// MARK: - Button and Stack References
2839
/* Variables to hold the stack views and button callback.
2940
buttonTypes keeps track of the available buttons.*/
@@ -68,6 +79,7 @@ extension CHeaderView {
6879
Dynamically creates stack views and separators.*/
6980
public func configure(
7081
with cellItem: (title: String,
82+
icon:TitleIcon?,
7183
sizeType: SectionSizeType,
7284
buttonTypes: [TitleForSectionButtonType]?),
7385
onTap: @escaping (TitleForSectionButtonType) -> Void
@@ -76,6 +88,19 @@ extension CHeaderView {
7688
titleLabel.text = cellItem.title
7789
titleLabel.font = .boldSystemFont(ofSize: cellItem.sizeType.size)
7890

91+
if let icon = cellItem.icon {
92+
switch icon.image {
93+
case .systemImage(let name):
94+
titleIcon.image = UIImage(systemName: name)
95+
case .imageAsstes(let name):
96+
titleIcon.image = UIImage(named: name)
97+
}
98+
titleIcon.tintColor = icon.tintColor
99+
titleIcon.isHidden = false
100+
} else {
101+
titleIcon.isHidden = true
102+
}
103+
79104
//Determine if the header should be visible based on title and buttons.
80105
let hasTitle = !(cellItem.title.isEmpty)
81106
let hasButtons = !(cellItem.buttonTypes?.isEmpty ?? true)
@@ -130,8 +155,8 @@ extension CHeaderView {
130155

131156
self.buttonStack = buttonStack
132157

133-
// Main stack contains the title label and button stack, horizontally aligned.
134-
let mainStack = UIStackView(arrangedSubviews: [titleLabel, buttonStack])
158+
// Main stack contains the icon title label and button stack, horizontally aligned.
159+
let mainStack = UIStackView(arrangedSubviews: [titleIcon,titleLabel, buttonStack])
135160
mainStack.axis = .horizontal
136161
mainStack.alignment = .center
137162
mainStack.spacing = 8
@@ -148,6 +173,13 @@ extension CHeaderView {
148173
mainStack.bottomAnchor.constraint(equalTo: bottomAnchor)
149174
])
150175

176+
177+
NSLayoutConstraint.activate([
178+
titleIcon.heightAnchor.constraint(equalToConstant: 20),
179+
titleIcon.widthAnchor.constraint(equalToConstant: 20)
180+
181+
])
182+
151183
// Set content hugging and compression priorities to ensure proper layout behavior.
152184
buttonStack.setContentHuggingPriority(.required, for: .horizontal)
153185
titleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)

Sources/GenericCollectionViewKit/datasource/GenericCollectionDataSourceProtcol.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public protocol GenericCollectionDataSourceProtocol {
4343
/// - Parameter section: The index of the section.
4444
/// - Returns: A tuple containing the title, size type, and optional button types.
4545
func titleForSection(at section: Int) -> (title: String,
46+
titleIcon:TitleIcon,
4647
sizeType: SectionSizeType,
4748
buttonType: [TitleForSectionButtonType]?)
4849

@@ -127,7 +128,7 @@ public class GenericCollectionDataSource<Source: GenericCollectionDataSourceProt
127128
let item = source.titleForSection(at: indexPath.section)
128129

129130
header.configure(
130-
with: (title: item.title, sizeType: item.sizeType, buttonTypes: item.buttonType)
131+
with: (title: item.title,icon:item.titleIcon, sizeType: item.sizeType, buttonTypes: item.buttonType)
131132
) { [weak self] tappedType in
132133
guard let self else { return }
133134
source.onTappedTitleButton(buttonType: tappedType, section: indexPath.section)

Sources/GenericCollectionViewKit/datasource/enums.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Foundation
9+
import UIKit
910

1011
public enum SectionSizeType {
1112
case large, medium, small,empty
@@ -32,3 +33,14 @@ public enum TitleForSectionButtonType {
3233
}
3334
}
3435
}
36+
37+
38+
public enum TitleIconType {
39+
case systemImage(String)
40+
case imageAsstes(String)
41+
}
42+
43+
public struct TitleIcon {
44+
let image:TitleIconType
45+
let tintColor:UIColor
46+
}

0 commit comments

Comments
 (0)