From 923b275bbc561db3f16ab2a66ca4d847ca5ce707 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sat, 22 Sep 2018 11:30:16 -0700 Subject: [PATCH 01/14] test not removing view --- Sources/Extensions/UIView+CollectionKit.swift | 3 ++- Sources/Other/CollectionReuseViewManager.swift | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Extensions/UIView+CollectionKit.swift b/Sources/Extensions/UIView+CollectionKit.swift index 4e54fb1..beccc68 100644 --- a/Sources/Extensions/UIView+CollectionKit.swift +++ b/Sources/Extensions/UIView+CollectionKit.swift @@ -37,7 +37,8 @@ extension UIView { } public func recycleForCollectionKitReuse() { - removeFromSuperview() +// removeFromSuperview() + isHidden = true reuseManager?.queue(view: self) } } diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 9bd9e4b..68adea7 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -37,6 +37,7 @@ public class CollectionReuseViewManager: NSObject { if let view = view as? CollectionViewReusableView { view.prepareForReuse() } + view.isHidden = false view.reuseManager = self return view } @@ -48,11 +49,17 @@ public class CollectionReuseViewManager: NSObject { if let view = view as? CollectionViewReusableView { view.prepareForReuse() } + view.isHidden = false view.reuseManager = self return view } @objc func cleanup() { + for views in reusableViews.values { + for view in views { + view.removeFromSuperview() + } + } reusableViews.removeAll() } } From fb571ed2c680ca4e3ad58e60603a19c2d7156980 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sat, 22 Sep 2018 21:08:16 -0700 Subject: [PATCH 02/14] fix view not visible bug --- Sources/Extensions/UIView+CollectionKit.swift | 8 +++++--- Sources/Other/CollectionReuseViewManager.swift | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/Extensions/UIView+CollectionKit.swift b/Sources/Extensions/UIView+CollectionKit.swift index beccc68..60ea5bb 100644 --- a/Sources/Extensions/UIView+CollectionKit.swift +++ b/Sources/Extensions/UIView+CollectionKit.swift @@ -37,8 +37,10 @@ extension UIView { } public func recycleForCollectionKitReuse() { -// removeFromSuperview() - isHidden = true - reuseManager?.queue(view: self) + if let reuseManager = reuseManager { + reuseManager.queue(view: self) + } else { + removeFromSuperview() + } } } diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 68adea7..0d4dfed 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -20,6 +20,7 @@ public class CollectionReuseViewManager: NSObject { public func queue(view: UIView) { let identifier = NSStringFromClass(type(of: view)) view.reuseManager = nil + view.isHidden = true if reusableViews[identifier] != nil && !reusableViews[identifier]!.contains(view) { reusableViews[identifier]?.append(view) } else { From 2eb7a1c239d366381bd591d1c167fc0b974bb7df Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sun, 23 Sep 2018 07:38:44 -0700 Subject: [PATCH 03/14] lifeSpan test --- Sources/Other/CollectionReuseViewManager.swift | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 0d4dfed..fd67700 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -13,9 +13,13 @@ public protocol CollectionViewReusableView: class { } public class CollectionReuseViewManager: NSObject { + + /// Time it takes for CollectionReuseViewManager to + /// dump all reusableViews to save memory + public var lifeSpan: TimeInterval = 5.0 + var reusableViews: [String: [UIView]] = [:] var cleanupTimer: Timer? - public var lifeSpan: TimeInterval = 0.5 public func queue(view: UIView) { let identifier = NSStringFromClass(type(of: view)) @@ -26,9 +30,12 @@ public class CollectionReuseViewManager: NSObject { } else { reusableViews[identifier] = [view] } - cleanupTimer?.invalidate() - cleanupTimer = Timer.scheduledTimer(timeInterval: lifeSpan, target: self, - selector: #selector(cleanup), userInfo: nil, repeats: false) + if let cleanupTimer = cleanupTimer { + cleanupTimer.fireDate = Date().addingTimeInterval(lifeSpan) + } else { + cleanupTimer = Timer.scheduledTimer(timeInterval: lifeSpan, target: self, + selector: #selector(cleanup), userInfo: nil, repeats: false) + } } public func dequeue (_ defaultView: @autoclosure () -> T) -> T { @@ -62,5 +69,7 @@ public class CollectionReuseViewManager: NSObject { } } reusableViews.removeAll() + cleanupTimer?.invalidate() + cleanupTimer = nil } } From 92035680beb6f146b35f0cce8f97f16d43601ebe Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sun, 23 Sep 2018 08:18:46 -0700 Subject: [PATCH 04/14] update travis --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4e4559f..8ae5408 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: swift -osx_image: xcode9.4 +osx_image: xcode10 script: - xcodebuild -workspace CollectionKit.xcworkspace -scheme CollectionKit -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 7,OS=10.3.1' build test + xcodebuild -workspace CollectionKit.xcworkspace -scheme CollectionKit -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone XR,OS=12.0' build test after_success: - bash <(curl -s https://codecov.io/bash) -J 'CollectionKit' \ No newline at end of file From 527a096a6102109d7022e245c6a1f2d23a953c0a Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sun, 23 Sep 2018 21:39:43 -0700 Subject: [PATCH 05/14] shorten dequeue --- Sources/Other/CollectionReuseViewManager.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index fd67700..b4face7 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -51,15 +51,7 @@ public class CollectionReuseViewManager: NSObject { } public func dequeue (type: T.Type) -> T { - let identifier = NSStringFromClass(type.self) - let queuedView = reusableViews[identifier]?.popLast() as? T - let view = queuedView ?? type.init() - if let view = view as? CollectionViewReusableView { - view.prepareForReuse() - } - view.isHidden = false - view.reuseManager = self - return view + return dequeue(type.init()) } @objc func cleanup() { From cf9a1e8c612ff31dfaf8977a624cb6e8c547e193 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Tue, 25 Sep 2018 13:56:59 -0700 Subject: [PATCH 06/14] update testing frameworks --- Podfile | 4 ++-- Podfile.lock | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Podfile b/Podfile index 4c0a115..6f1f316 100644 --- a/Podfile +++ b/Podfile @@ -3,6 +3,6 @@ use_frameworks! target "CollectionKitTests" do - pod 'Quick', "= 1.3.0" - pod 'Nimble', "= 7.1.1" + pod 'Quick', "= 1.3.2" + pod 'Nimble', "= 7.3.1" end \ No newline at end of file diff --git a/Podfile.lock b/Podfile.lock index f787668..ebc8af3 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,10 +1,10 @@ PODS: - - Nimble (7.1.1) - - Quick (1.3.0) + - Nimble (7.3.1) + - Quick (1.3.2) DEPENDENCIES: - - Nimble (= 7.1.1) - - Quick (= 1.3.0) + - Nimble (= 7.3.1) + - Quick (= 1.3.2) SPEC REPOS: https://github.com/cocoapods/specs.git: @@ -12,9 +12,9 @@ SPEC REPOS: - Quick SPEC CHECKSUMS: - Nimble: 391f07782af4b37914f9bd7b2ffbb952731b8202 - Quick: 03278013f71aa05fe9ecabc94fbcc6835f1ee76f + Nimble: 04f732da099ea4d153122aec8c2a88fd0c7219ae + Quick: 2623cb30d7a7f41ca62f684f679586558f483d46 -PODFILE CHECKSUM: f33c8746ebf721eac77bc6829bdd94f9e3b0de44 +PODFILE CHECKSUM: 056648a7e76b878641ac9a9fd2b13117bd03b5db COCOAPODS: 1.5.3 From 8d0246625dd823bb16b5300fe6783ef342760985 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Wed, 26 Sep 2018 16:14:11 -0700 Subject: [PATCH 07/14] bump version --- CollectionKit.podspec | 2 +- Sources/Info.plist | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CollectionKit.podspec b/CollectionKit.podspec index 02f3590..4f937cb 100644 --- a/CollectionKit.podspec +++ b/CollectionKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "CollectionKit" - s.version = "2.1.0" + s.version = "2.2.0" s.summary = "A modern swift framework for building data-driven reusable collection view components." s.description = <<-DESC diff --git a/Sources/Info.plist b/Sources/Info.plist index f69838d..9a384cb 100644 --- a/Sources/Info.plist +++ b/Sources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.1.0 + 2.2.0 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass From 1cbddb2ca57becb940d1156b03947f5f00a2f21c Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Wed, 26 Sep 2018 16:15:27 -0700 Subject: [PATCH 08/14] update swift version --- .swift-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.swift-version b/.swift-version index 5186d07..bf77d54 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -4.0 +4.2 From 741cf983e81088b5b5587242d5ddb606eb748495 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Wed, 26 Sep 2018 16:15:27 -0700 Subject: [PATCH 09/14] update swift version --- .swift-version | 2 +- CollectionKit.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.swift-version b/.swift-version index 5186d07..bf77d54 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -4.0 +4.2 diff --git a/CollectionKit.podspec b/CollectionKit.podspec index 4f937cb..6b17be9 100644 --- a/CollectionKit.podspec +++ b/CollectionKit.podspec @@ -39,7 +39,7 @@ Pod::Spec.new do |s| s.subspec 'WobbleAnimator' do |cs| cs.dependency 'CollectionKit/Core' - cs.dependency 'YetAnotherAnimationLibrary' + cs.dependency 'YetAnotherAnimationLibrary', "~> 1.3.0" cs.source_files = 'WobbleAnimator/**/*.swift' end end From 2c6827dbed2c6d77fcc49eaa229e05d429ef6960 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Wed, 26 Sep 2018 16:34:54 -0700 Subject: [PATCH 10/14] update travis script --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 8ae5408..df21362 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,5 +4,8 @@ osx_image: xcode10 script: xcodebuild -workspace CollectionKit.xcworkspace -scheme CollectionKit -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone XR,OS=12.0' build test +before_install: + - pod repo update + after_success: - bash <(curl -s https://codecov.io/bash) -J 'CollectionKit' \ No newline at end of file From 666764cc03a14723b9698b285322e99fea5f4f0d Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sat, 22 Sep 2018 11:30:16 -0700 Subject: [PATCH 11/14] test not removing view --- Sources/Extensions/UIView+CollectionKit.swift | 3 ++- Sources/Other/CollectionReuseViewManager.swift | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Extensions/UIView+CollectionKit.swift b/Sources/Extensions/UIView+CollectionKit.swift index 4e54fb1..beccc68 100644 --- a/Sources/Extensions/UIView+CollectionKit.swift +++ b/Sources/Extensions/UIView+CollectionKit.swift @@ -37,7 +37,8 @@ extension UIView { } public func recycleForCollectionKitReuse() { - removeFromSuperview() +// removeFromSuperview() + isHidden = true reuseManager?.queue(view: self) } } diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 9bd9e4b..68adea7 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -37,6 +37,7 @@ public class CollectionReuseViewManager: NSObject { if let view = view as? CollectionViewReusableView { view.prepareForReuse() } + view.isHidden = false view.reuseManager = self return view } @@ -48,11 +49,17 @@ public class CollectionReuseViewManager: NSObject { if let view = view as? CollectionViewReusableView { view.prepareForReuse() } + view.isHidden = false view.reuseManager = self return view } @objc func cleanup() { + for views in reusableViews.values { + for view in views { + view.removeFromSuperview() + } + } reusableViews.removeAll() } } From 7eedde5b1c0113aaf896fefe1b099d7e633b2520 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sat, 22 Sep 2018 21:08:16 -0700 Subject: [PATCH 12/14] fix view not visible bug --- Sources/Extensions/UIView+CollectionKit.swift | 8 +++++--- Sources/Other/CollectionReuseViewManager.swift | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/Extensions/UIView+CollectionKit.swift b/Sources/Extensions/UIView+CollectionKit.swift index beccc68..60ea5bb 100644 --- a/Sources/Extensions/UIView+CollectionKit.swift +++ b/Sources/Extensions/UIView+CollectionKit.swift @@ -37,8 +37,10 @@ extension UIView { } public func recycleForCollectionKitReuse() { -// removeFromSuperview() - isHidden = true - reuseManager?.queue(view: self) + if let reuseManager = reuseManager { + reuseManager.queue(view: self) + } else { + removeFromSuperview() + } } } diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 68adea7..0d4dfed 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -20,6 +20,7 @@ public class CollectionReuseViewManager: NSObject { public func queue(view: UIView) { let identifier = NSStringFromClass(type(of: view)) view.reuseManager = nil + view.isHidden = true if reusableViews[identifier] != nil && !reusableViews[identifier]!.contains(view) { reusableViews[identifier]?.append(view) } else { From 444e0b33a353cdcbb59bc547964b308982a66c3e Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sun, 14 Oct 2018 12:55:36 -0700 Subject: [PATCH 13/14] add option to always remove from collection view --- Sources/Other/CollectionReuseViewManager.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 0d4dfed..acbef42 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -13,14 +13,20 @@ public protocol CollectionViewReusableView: class { } public class CollectionReuseViewManager: NSObject { + public var lifeSpan: TimeInterval = 0.5 + public var removeFromCollectionViewWhenReuse = false + var reusableViews: [String: [UIView]] = [:] var cleanupTimer: Timer? - public var lifeSpan: TimeInterval = 0.5 public func queue(view: UIView) { let identifier = NSStringFromClass(type(of: view)) view.reuseManager = nil - view.isHidden = true + if removeFromCollectionViewWhenReuse { + view.removeFromSuperview() + } else { + view.isHidden = true + } if reusableViews[identifier] != nil && !reusableViews[identifier]!.contains(view) { reusableViews[identifier]?.append(view) } else { @@ -38,7 +44,9 @@ public class CollectionReuseViewManager: NSObject { if let view = view as? CollectionViewReusableView { view.prepareForReuse() } - view.isHidden = false + if !removeFromCollectionViewWhenReuse { + view.isHidden = false + } view.reuseManager = self return view } From 33ad680273d729f95c29becf54b49cd43648c4cd Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sun, 14 Oct 2018 13:04:52 -0700 Subject: [PATCH 14/14] add comments --- Sources/Other/CollectionReuseViewManager.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/Other/CollectionReuseViewManager.swift b/Sources/Other/CollectionReuseViewManager.swift index 94e9936..a337a8e 100644 --- a/Sources/Other/CollectionReuseViewManager.swift +++ b/Sources/Other/CollectionReuseViewManager.swift @@ -18,6 +18,10 @@ public class CollectionReuseViewManager: NSObject { /// dump all reusableViews to save memory public var lifeSpan: TimeInterval = 5.0 + /// When `removeFromCollectionViewWhenReuse` is enabled, + /// cells will always be removed from Collection View during reuse. + /// This is slower but it doesn't influence the `isHidden` property + /// of individual cells. public var removeFromCollectionViewWhenReuse = false var reusableViews: [String: [UIView]] = [:]