Skip to content

Instantly share code, notes, and snippets.

@madhu314
Created February 8, 2017 07:20
Show Gist options
  • Save madhu314/0bf3609cffcd74ff44a220384fe8541c to your computer and use it in GitHub Desktop.
Save madhu314/0bf3609cffcd74ff44a220384fe8541c to your computer and use it in GitHub Desktop.
func toggleCompleted(forItem listItem: ListItem, on collectionView: UICollectionView) {
let foundIndex = items.index { (given) -> Bool in
return given.id == listItem.id
}
if(listItem.isCompleted) {
if let index = foundIndex {
let item = items.remove(at: index)
items.insert(item, at: 0)
item.isCompleted = false
collectionView.moveItem(at: IndexPath(item: index, section: 0), to: IndexPath(item: 0, section: 0))
}
} else {
if let index = foundIndex {
let item = items.remove(at: index)
items.append(item)
item.isCompleted = true
collectionView.moveItem(at: IndexPath(item: index, section: 0), to: IndexPath(item: items.count - 1, section: 0))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment