Skip to content

Commit

Permalink
chore: SKScrollViewDelegateHandler 添加标识ID, 防止重复创建
Browse files Browse the repository at this point in the history
  • Loading branch information
linhey committed Aug 16, 2024
1 parent a3d57cb commit 4c87256
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 63 deletions.
33 changes: 0 additions & 33 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
CD0621A128A7FBD200272233 /* Stem in Frameworks */ = {isa = PBXBuildFile; productRef = CD0621A028A7FBD200272233 /* Stem */; };
CD0621A328A7FBD200272233 /* StemColor in Frameworks */ = {isa = PBXBuildFile; productRef = CD0621A228A7FBD200272233 /* StemColor */; };
CD0621A528A7FBD200272233 /* StemFilePath in Frameworks */ = {isa = PBXBuildFile; productRef = CD0621A428A7FBD200272233 /* StemFilePath */; };
CD33B80827DCE3A70032987B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD33B80727DCE3A70032987B /* AppDelegate.swift */; };
CD33B80A27DCE3A70032987B /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD33B80927DCE3A70032987B /* SceneDelegate.swift */; };
CD33B80F27DCE3A70032987B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CD33B80D27DCE3A70032987B /* Main.storyboard */; };
Expand Down Expand Up @@ -61,10 +58,7 @@
buildActionMask = 2147483647;
files = (
CDC42B842959ACE500AEF423 /* SectionUI in Frameworks */,
CD0621A328A7FBD200272233 /* StemColor in Frameworks */,
CD0621A128A7FBD200272233 /* Stem in Frameworks */,
CDC42B822959ACE000AEF423 /* SectionKit in Frameworks */,
CD0621A528A7FBD200272233 /* StemFilePath in Frameworks */,
CD33B82627DCE6190032987B /* SnapKit in Frameworks */,
CDA10DEE28B4A90C000698F2 /* Delegate in Frameworks */,
CDA3FEF22AD92642000A8735 /* SectionKit in Frameworks */,
Expand Down Expand Up @@ -141,9 +135,6 @@
name = Example;
packageProductDependencies = (
CD33B82527DCE6190032987B /* SnapKit */,
CD0621A028A7FBD200272233 /* Stem */,
CD0621A228A7FBD200272233 /* StemColor */,
CD0621A428A7FBD200272233 /* StemFilePath */,
CDA10DED28B4A90C000698F2 /* Delegate */,
CDC42B812959ACE000AEF423 /* SectionKit */,
CDC42B832959ACE500AEF423 /* SectionUI */,
Expand Down Expand Up @@ -180,7 +171,6 @@
mainGroup = CD33B7FB27DCE3A70032987B;
packageReferences = (
CD33B82427DCE6190032987B /* XCRemoteSwiftPackageReference "SnapKit" */,
CD06219F28A7FBD200272233 /* XCRemoteSwiftPackageReference "Stem" */,
CDA10DEC28B4A90C000698F2 /* XCRemoteSwiftPackageReference "Delegate" */,
CDA3FEF02AD92642000A8735 /* XCLocalSwiftPackageReference ".." */,
);
Expand Down Expand Up @@ -454,14 +444,6 @@
/* End XCLocalSwiftPackageReference section */

/* Begin XCRemoteSwiftPackageReference section */
CD06219F28A7FBD200272233 /* XCRemoteSwiftPackageReference "Stem" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/linhay/Stem.git";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 1.1.2;
};
};
CD33B82427DCE6190032987B /* XCRemoteSwiftPackageReference "SnapKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/SnapKit/SnapKit";
Expand All @@ -481,21 +463,6 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
CD0621A028A7FBD200272233 /* Stem */ = {
isa = XCSwiftPackageProductDependency;
package = CD06219F28A7FBD200272233 /* XCRemoteSwiftPackageReference "Stem" */;
productName = Stem;
};
CD0621A228A7FBD200272233 /* StemColor */ = {
isa = XCSwiftPackageProductDependency;
package = CD06219F28A7FBD200272233 /* XCRemoteSwiftPackageReference "Stem" */;
productName = StemColor;
};
CD0621A428A7FBD200272233 /* StemFilePath */ = {
isa = XCSwiftPackageProductDependency;
package = CD06219F28A7FBD200272233 /* XCRemoteSwiftPackageReference "Stem" */;
productName = StemFilePath;
};
CD33B82527DCE6190032987B /* SnapKit */ = {
isa = XCSwiftPackageProductDependency;
package = CD33B82427DCE6190032987B /* XCRemoteSwiftPackageReference "SnapKit" */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "150dd805ac83f32a8101df4e7da1c99371eab2e67bb6e27d9724cd7372b907aa",
"pins" : [
{
"identity" : "delegate",
Expand All @@ -17,16 +18,7 @@
"revision" : "d458564516e5676af9c70b4f4b2a9178294f1bc6",
"version" : "5.0.1"
}
},
{
"identity" : "stem",
"kind" : "remoteSourceControl",
"location" : "https://github.com/linhay/Stem.git",
"state" : {
"revision" : "c805890142fb0a936cf4a88d268274b0ab770ef3",
"version" : "1.1.2"
}
}
],
"version" : 2
"version" : 3
}
4 changes: 1 addition & 3 deletions Example/Example/Home/Cells/ReusableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
//

import SectionKit
import Stem
#if canImport(UIKit)
import UIKit
import StemColor

class ReusableView: UICollectionReusableView, SKLoadViewProtocol, SKConfigurableView {

Expand Down Expand Up @@ -56,7 +54,7 @@ class ReusableView: UICollectionReusableView, SKLoadViewProtocol, SKConfigurable
super.init(frame: frame)
layer.borderColor = UIColor.black.cgColor
layer.borderWidth = 1
backgroundColor = StemColor.random.convert()
backgroundColor = .red
addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.edges.equalToSuperview()
Expand Down
5 changes: 2 additions & 3 deletions Example/Example/Home/Cells/StringCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#if canImport(UIKit)
import UIKit
import SectionKit
import StemColor

class StringCell: UICollectionViewCell, SKLoadViewProtocol, SKConfigurableView {

Expand All @@ -17,7 +16,7 @@ class StringCell: UICollectionViewCell, SKLoadViewProtocol, SKConfigurableView {
let size: CGSize
let color: UIColor

init(text: String, size: CGSize, color: UIColor = StemColor.random.convert()) {
init(text: String, size: CGSize, color: UIColor) {
self.text = text
self.size = size
self.color = color
Expand Down Expand Up @@ -69,4 +68,4 @@ extension StringCell {
}
}

#endif
#endif
5 changes: 2 additions & 3 deletions Example/Example/Home/DecorationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import SectionUI
import Stem
#if canImport(UIKit)
import UIKit

Expand Down Expand Up @@ -167,10 +166,10 @@ extension DecorationViewController {

func animate(_ event: @escaping () -> Void) {
isAnimating = true
Gcd.delay(.main, seconds: 0.5) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
self.isAnimating = false
event()
}
})
}

func setupUI() {
Expand Down
7 changes: 7 additions & 0 deletions Example/Example/Home/HomeController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ extension HomeController {
section.sectionInset = .init(top: 20, left: 20, bottom: 0, right: 20)
section.minimumLineSpacing = 8
manager.reload(section)
manager.scrollObserver.add(scroll: "1") { hander in

}
manager.scrollObserver.add(scroll: "1") { hander in

}
print(1)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Example/Example/Home/PluginsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PluginsController: SKCollectionViewController {
var enable = false
override func viewDidLoad() {
super.viewDidLoad()
st.addChilds(toolbar)
addChild(toolbar)
view.addSubview(toolbar.view)
toolbar.view.snp.makeConstraints { make in
make.bottom.left.right.equalToSuperview()
Expand Down
4 changes: 1 addition & 3 deletions Example/Example/Home/PrefetchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import Combine
import SectionUI
import Stem
#if canImport(UIKit)
import UIKit
import StemColor

class PrefetchViewController: SKCollectionViewController {
private let section = ColorBlockCell
Expand All @@ -32,7 +30,7 @@ class PrefetchViewController: SKCollectionViewController {

extension PrefetchViewController {
func next() {
let color: UIColor = StemColor.random.alpha(with: 0.4).convert()
let color: UIColor = .blue.withAlphaComponent(0.4)
section.config(models: section.models + (0 ... 20).map { index in
.init(color: color,
text: (section.models.count + index).description,
Expand Down
12 changes: 5 additions & 7 deletions Example/Example/Home/SingleTypeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
//

import SectionUI
import Stem
#if canImport(UIKit)
import UIKit
import StemColor

class SingleTypeViewController: SKCollectionViewController {

Expand Down Expand Up @@ -48,7 +46,7 @@ class SingleTypeViewController: SKCollectionViewController {

sectionController.section.onCellAction(.selected) { [weak self] context in
guard let self = self else { return }
context.view().backgroundColor = StemColor.random.convert()
context.view().backgroundColor = .blue
switch context.model {
case .section_0:
self.rightController.section = self.rightController.section0
Expand Down Expand Up @@ -168,13 +166,13 @@ extension SingleTypeViewController {
case .add_section:
manager.reload([section2, section])
case .add:
section.append(.init(color: StemColor.random.alpha(with: 0.4).convert(),
section.append(.init(color: .red.withAlphaComponent(0.4),
text: "\(section.models.count) New",
size: size))
case .insert:
if let offset = (0..<section.itemCount).randomElement() {
section.insert(at: offset,
.init(color: StemColor.random.alpha(with: 0.4).convert(),
.init(color: .red.withAlphaComponent(0.4),
text: "\(offset) New",
size: size))
}
Expand Down Expand Up @@ -237,9 +235,9 @@ extension SingleTypeViewController {

func animate(_ event: @escaping () -> Void, finish: (() -> Void)? = nil) {
isAnimating = true
Gcd.delay(.main, seconds: 0.5) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
event()
Gcd.delay(.main, seconds: 0.5) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
self.isAnimating = false
finish?()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,23 @@ public extension SKScrollViewDelegateForward {
func add(scroll build: (_ handle: inout SKScrollViewDelegateHandler) -> Void) {
var item = SKScrollViewDelegateHandler()
build(&item)
scrollObservers = scrollObservers.filter { handle in
guard let handle = handle as? SKScrollViewDelegateHandler,
handle.id == item.id else {
return true
}
return false
}
add(scroll: item)
}

func add(scroll id: String, build: (_ handle: inout SKScrollViewDelegateHandler) -> Void) {
add { handle in
handle.id = id
build(&handle)
}
}

func add(_ item: UIScrollViewDelegate) {
add(SKScrollViewDelegateObserverBox(box: .init(item)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import UIKit

public struct SKScrollViewDelegateHandler: SKScrollViewDelegateObserverProtocol {

public var id: String = UUID().uuidString

@discardableResult
public mutating func didScroll(_ observe: @escaping (_ scrollView: UIScrollView) -> Void) -> Self {
self.didScrolls.append(observe)
Expand Down

0 comments on commit 4c87256

Please sign in to comment.