Skip to content

Commit 0179767

Browse files
committed
feat: SwiftUI 支持!
1 parent 9298efd commit 0179767

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed

SectionKit2.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'SectionKit2'
3-
s.version = '2.2.8'
3+
s.version = '2.3.0'
44
s.summary = '动态表单框架'
55
s.homepage = "https://github.com/linhay/SectionKit"
66
s.license = { :type => 'Apache', :file => 'LICENSE' }

SectionUI.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'SectionUI'
3-
s.version = '2.2.8'
3+
s.version = '2.3.0'
44
s.summary = '动态表单框架'
55
s.homepage = "https://github.com/linhay/SectionKit"
66
s.license = { :type => 'Apache', :file => 'LICENSE' }
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
1111

1212
s.platform = :ios
1313
s.ios.deployment_target = "13.0"
14-
s.dependency 'SectionKit2', '>= 2.2.8'
14+
s.dependency 'SectionKit2', '>= 2.3.0'
1515

1616
s.source_files = ["Sources/SectionUI/**/*.{swift,h}"]
1717
end

Sources/SectionKit/CollectionBaseProtocol/SKCSectionProtocol.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77

88
#if canImport(UIKit)
99
import UIKit
10+
11+
public protocol SKCAnySectionProtocol {
12+
var section: SKCSectionProtocol { get }
13+
}
1014

1115
public typealias SKCBaseSectionProtocol = SKCSectionActionProtocol & SKCDataSourceProtocol & SKCDelegateProtocol
12-
public typealias SKCSectionProtocol = SKCBaseSectionProtocol & SKCViewDelegateFlowLayoutProtocol
16+
public typealias SKCSectionProtocol = SKCBaseSectionProtocol & SKCViewDelegateFlowLayoutProtocol & SKCAnySectionProtocol
17+
18+
public extension SKCAnySectionProtocol where Self: SKCSectionProtocol {
19+
20+
var section: SKCSectionProtocol {
21+
self
22+
}
23+
24+
}
1325

1426
public extension SKCDataSourceProtocol where Self: SKCViewDelegateFlowLayoutProtocol {
1527

@@ -26,4 +38,4 @@ public extension SKCDataSourceProtocol where Self: SKCViewDelegateFlowLayoutProt
2638

2739
}
2840

29-
#endif
41+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// SKHostingCollectionView.swift
3+
// CoolUp
4+
//
5+
// Created by linhey on 11/15/24.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
import SectionKit
11+
12+
public struct SKCHostingCollectionView: UIViewRepresentable {
13+
14+
public typealias UIViewType = SKCollectionView
15+
16+
public class Coordinator {
17+
public var manager: SKCManager?
18+
public var sections: [SKCAnySectionProtocol]
19+
20+
init(manager: SKCManager? = nil, sections: [SKCAnySectionProtocol]) {
21+
self.manager = manager
22+
self.sections = sections
23+
}
24+
}
25+
26+
let sections: [any SKCAnySectionProtocol]
27+
28+
public init(_ sections: [any SKCAnySectionProtocol]) {
29+
self.sections = sections
30+
}
31+
32+
public init(@SectionArrayResultBuilder<SKCAnySectionProtocol> builder: () -> [SKCAnySectionProtocol]) {
33+
self.sections = builder()
34+
}
35+
36+
public func makeCoordinator() -> Coordinator {
37+
.init(manager: nil, sections: sections)
38+
}
39+
40+
public func makeUIView(context: Context) -> SKCollectionView {
41+
let view = SKCollectionView()
42+
context.coordinator.manager = view.manager
43+
context.coordinator.manager?.reload(context.coordinator.sections.map(\.section))
44+
return view
45+
}
46+
47+
public func updateUIView(_ uiView: SKCollectionView, context: Context) {
48+
context.coordinator.manager?.reload(context.coordinator.sections.map(\.section))
49+
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// SKCHostingSection.swift
3+
// SectionKit
4+
//
5+
// Created by linhey on 11/21/24.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
import SectionKit
11+
12+
public struct SKCHostingSection<Content: SKExistModelView & View>: SKCAnySectionProtocol {
13+
14+
public let cell: Content.Type
15+
public let models: [Content.Model]
16+
public var style: (_ section: SKCSingleTypeSection<STCHostingCell<Content>>) -> Void
17+
18+
public init(cell: Content.Type,
19+
models: [Content.Model],
20+
style: @escaping (_: SKCSingleTypeSection<STCHostingCell<Content>>) -> Void) {
21+
self.cell = cell
22+
self.models = models
23+
self.style = style
24+
}
25+
26+
public var section: SKCSectionProtocol {
27+
Content
28+
.wrapperToCollectionCell()
29+
.wrapperToSingleTypeSection(models)
30+
.setSectionStyle(style)
31+
}
32+
}

Sources/SectionUI/Sections/SKCHostingCell.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class STCHostingCell<ContentView: SKExistModelView & View>: UICollectionV
2828

2929
public func config(_ model: ContentView.Model) {
3030
var wrappedView: UIView
31-
3231
if #available(iOS 16.0, *) {
3332
wrappedView = UIHostingConfiguration { [frame = self.frame] in
3433
ContentView(model: model)

0 commit comments

Comments
 (0)