mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-28 06:25:04 -04:00
Bookmark deletion
This commit is contained in:
parent
b652259783
commit
89c7feefc0
@ -30,10 +30,11 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource
|
|||||||
|
|
||||||
override func setEditing(_ editing: Bool, animated: Bool) {
|
override func setEditing(_ editing: Bool, animated: Bool) {
|
||||||
super.setEditing(editing, animated: animated)
|
super.setEditing(editing, animated: animated)
|
||||||
|
collectionView.indexPathsForSelectedItems?.forEach({ collectionView.deselectItem(at: $0, animated: false) })
|
||||||
if editing {
|
if editing {
|
||||||
navigationItem.rightBarButtonItems? = [doneButton, deleteButton]
|
navigationItem.setRightBarButtonItems([doneButton, deleteButton], animated: animated)
|
||||||
} else {
|
} else {
|
||||||
navigationItem.rightBarButtonItems = [editButton]
|
navigationItem.setRightBarButtonItems([editButton], animated: animated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +53,16 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource
|
|||||||
dismiss(animated: true, completion: nil)
|
dismiss(animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func configureButtons() {
|
||||||
|
// For some reason, initializing buttons with target actions does not work
|
||||||
|
editButton.target = self
|
||||||
|
editButton.action = #selector(editButtonTapped(sender:))
|
||||||
|
doneButton.target = self
|
||||||
|
doneButton.action = #selector(doneButtonTapped(sender:))
|
||||||
|
deleteButton.target = self
|
||||||
|
deleteButton.action = #selector(deleteButtonTapped(sender:))
|
||||||
|
}
|
||||||
|
|
||||||
func editButtonTapped(sender: UIBarButtonItem) {
|
func editButtonTapped(sender: UIBarButtonItem) {
|
||||||
setEditing(true, animated: true)
|
setEditing(true, animated: true)
|
||||||
}
|
}
|
||||||
@ -63,9 +74,8 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource
|
|||||||
func deleteButtonTapped(sender: UIBarButtonItem) {
|
func deleteButtonTapped(sender: UIBarButtonItem) {
|
||||||
let context = AppDelegate.persistentContainer.viewContext
|
let context = AppDelegate.persistentContainer.viewContext
|
||||||
context.perform {
|
context.perform {
|
||||||
let fetchRequest = Article.fetchRequest() as! NSFetchRequest<Article>
|
self.collectionView.indexPathsForSelectedItems?.forEach({ (indexPath) in
|
||||||
let articles = try? context.fetch(fetchRequest)
|
let article = self.fetchedResultController.object(at: indexPath)
|
||||||
articles?.forEach({ (article) in
|
|
||||||
context.delete(article)
|
context.delete(article)
|
||||||
})
|
})
|
||||||
try? context.save()
|
try? context.save()
|
||||||
@ -79,6 +89,9 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource
|
|||||||
title = "Bookmarks"
|
title = "Bookmarks"
|
||||||
navigationItem.rightBarButtonItems = [editButton]
|
navigationItem.rightBarButtonItems = [editButton]
|
||||||
collectionView.alwaysBounceVertical = true
|
collectionView.alwaysBounceVertical = true
|
||||||
|
collectionView.allowsMultipleSelection = true
|
||||||
|
|
||||||
|
configureButtons()
|
||||||
|
|
||||||
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
|
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
|
||||||
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
|
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
|
||||||
@ -122,10 +135,13 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource
|
|||||||
// MARK: - UICollectionView Delegate
|
// MARK: - UICollectionView Delegate
|
||||||
|
|
||||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||||
|
if !isEditing {
|
||||||
|
collectionView.deselectItem(at: indexPath, animated: true)
|
||||||
let article = fetchedResultController.object(at: indexPath)
|
let article = fetchedResultController.object(at: indexPath)
|
||||||
guard let url = article.url else {return}
|
guard let url = article.url else {return}
|
||||||
GlobalQueue.shared.add(articleLoadOperation: ArticleLoadOperation(url: url))
|
GlobalQueue.shared.add(articleLoadOperation: ArticleLoadOperation(url: url))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - UICollectionViewDelegateFlowLayout
|
// MARK: - UICollectionViewDelegateFlowLayout
|
||||||
|
|
||||||
|
@ -137,7 +137,11 @@ class BookmarkCollectionCell: UICollectionViewCell {
|
|||||||
@IBOutlet weak var bookTitleLabel: UILabel!
|
@IBOutlet weak var bookTitleLabel: UILabel!
|
||||||
@IBOutlet weak var bookmarkDetailLabel: UILabel!
|
@IBOutlet weak var bookmarkDetailLabel: UILabel!
|
||||||
|
|
||||||
|
override var isSelected: Bool {
|
||||||
|
didSet {
|
||||||
|
contentView.backgroundColor = isSelected ? UIColor(colorLiteralRed: 200/255, green: 220/255, blue: 1, alpha: 1) : UIColor.white
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user