Skip to content

Update dataSource inside performBatchUpdates() #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 112 additions & 59 deletions StackScrollView/StackScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl

open func insert(views _views: [UIView], at index: Int, animated: Bool) {

var _views = _views
_views.removeAll(where: views.contains(_:))
views.insert(contentsOf: _views, at: index)
_views.forEach { view in
register(Cell.self, forCellWithReuseIdentifier: identifier(view))
}
let batchUpdates: () -> Void = {
self.performBatchUpdates({
var _views = _views
_views.removeAll(where: self.views.contains(_:))
self.views.insert(contentsOf: _views, at: index)
self.insertItems(at: (index ..< index.advanced(by: _views.count)).map({ IndexPath(item: $0, section: 0) }))
}, completion: nil)
}
Expand All @@ -149,70 +149,130 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl

open func insert(views _views: [UIView], before view: UIView, animated: Bool) {

guard let index = views.firstIndex(of: view) else {
return
_views.forEach { view in
register(Cell.self, forCellWithReuseIdentifier: identifier(view))
}
let batchUpdates: () -> Void = {
self.performBatchUpdates({
guard let index = self.views.firstIndex(of: view) else {
return
}
var _views = _views
_views.removeAll(where: self.views.contains(_:))
self.views.insert(contentsOf: _views, at: index)
self.insertItems(at: (index ..< index.advanced(by: _views.count)).map({ IndexPath(item: $0, section: 0) }))
}, completion: nil)
}
if animated {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [
.beginFromCurrentState,
.allowUserInteraction,
.overrideInheritedCurve,
.overrideInheritedOptions,
.overrideInheritedDuration
],
animations: batchUpdates,
completion: nil)

} else {
UIView.performWithoutAnimation(batchUpdates)
}
insert(views: _views, at: index, animated: animated)
}

open func insert(views _views: [UIView], after view: UIView, animated: Bool) {

guard let index = views.firstIndex(of: view)?.advanced(by: 1) else {
return
_views.forEach { view in
register(Cell.self, forCellWithReuseIdentifier: identifier(view))
}
let batchUpdates: () -> Void = {
self.performBatchUpdates({
guard let index = self.views.firstIndex(of: view)?.advanced(by: 1) else {
return
}
var _views = _views
_views.removeAll(where: self.views.contains(_:))
self.views.insert(contentsOf: _views, at: index)
self.insertItems(at: (index ..< index.advanced(by: _views.count)).map({ IndexPath(item: $0, section: 0) }))
}, completion: nil)
}
if animated {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [
.beginFromCurrentState,
.allowUserInteraction,
.overrideInheritedCurve,
.overrideInheritedOptions,
.overrideInheritedDuration
],
animations: batchUpdates,
completion: nil)

} else {
UIView.performWithoutAnimation(batchUpdates)
}
insert(views: _views, at: index, animated: animated)
}

open func remove(view: UIView, animated: Bool) {

if let index = views.firstIndex(of: view) {
views.remove(at: index)
if animated {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [
.beginFromCurrentState,
.allowUserInteraction,
.overrideInheritedCurve,
.overrideInheritedOptions,
.overrideInheritedDuration
],
animations: {
self.performBatchUpdates({
self.deleteItems(at: [IndexPath(item: index, section: 0)])
}, completion: nil)
}) { (finish) in

}

} else {
UIView.performWithoutAnimation {
performBatchUpdates({
self.deleteItems(at: [IndexPath(item: index, section: 0)])
}, completion: nil)

let batchUpdates: () -> Void = {
self.performBatchUpdates({
guard let index = self.views.firstIndex(of: view) else {
return
}
}
self.views.remove(at: index)
self.deleteItems(at: [IndexPath(item: index, section: 0)])
}, completion: nil)
}
if animated {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [
.beginFromCurrentState,
.allowUserInteraction,
.overrideInheritedCurve,
.overrideInheritedOptions,
.overrideInheritedDuration
],
animations: batchUpdates,
completion: nil)

} else {
UIView.performWithoutAnimation(batchUpdates)
}
}

open func remove(views: [UIView], animated: Bool) {

var indicesForRemove: [Int] = []
let batchUpdates: () -> Void = {
self.performBatchUpdates({
var indicesForRemove: [Int] = []

for view in views {
if let index = self.views.firstIndex(of: view) {
indicesForRemove.append(index)
}
}
for view in views {
if let index = self.views.firstIndex(of: view) {
indicesForRemove.append(index)
}
}

// It seems that the layout is not updated properly unless the order is aligned.
indicesForRemove.sort(by: >)
// It seems that the layout is not updated properly unless the order is aligned.
indicesForRemove.sort(by: >)

for index in indicesForRemove {
self.views.remove(at: index)
for index in indicesForRemove {
self.views.remove(at: index)
}
self.deleteItems(at: indicesForRemove.map { IndexPath.init(item: $0, section: 0) })
}, completion: nil)
}

if animated {
Expand All @@ -228,17 +288,10 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
.overrideInheritedOptions,
.overrideInheritedDuration
],
animations: {
self.performBatchUpdates({
self.deleteItems(at: indicesForRemove.map { IndexPath.init(item: $0, section: 0) })
}, completion: nil)
})
animations: batchUpdates,
completion: nil)
} else {
UIView.performWithoutAnimation {
performBatchUpdates({
self.deleteItems(at: indicesForRemove.map { IndexPath.init(item: $0, section: 0) })
}, completion: nil)
}
UIView.performWithoutAnimation(batchUpdates)
}
}

Expand Down