From 89c7feefc03afd80fe942a09503e1aeccb7b215a Mon Sep 17 00:00:00 2001 From: Chris Li Date: Tue, 17 Jan 2017 15:35:28 -0500 Subject: [PATCH] Bookmark deletion --- .../BookmarkCollectionController.swift | 32 ++++++++++++++----- Kiwix-iOS/View/Cells.swift | 6 +++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift b/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift index 4c97524e..5bf18c4d 100644 --- a/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift +++ b/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift @@ -30,10 +30,11 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource override func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) + collectionView.indexPathsForSelectedItems?.forEach({ collectionView.deselectItem(at: $0, animated: false) }) if editing { - navigationItem.rightBarButtonItems? = [doneButton, deleteButton] + navigationItem.setRightBarButtonItems([doneButton, deleteButton], animated: animated) } else { - navigationItem.rightBarButtonItems = [editButton] + navigationItem.setRightBarButtonItems([editButton], animated: animated) } } @@ -52,6 +53,16 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource 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) { setEditing(true, animated: true) } @@ -63,9 +74,8 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource func deleteButtonTapped(sender: UIBarButtonItem) { let context = AppDelegate.persistentContainer.viewContext context.perform { - let fetchRequest = Article.fetchRequest() as! NSFetchRequest
- let articles = try? context.fetch(fetchRequest) - articles?.forEach({ (article) in + self.collectionView.indexPathsForSelectedItems?.forEach({ (indexPath) in + let article = self.fetchedResultController.object(at: indexPath) context.delete(article) }) try? context.save() @@ -79,6 +89,9 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource title = "Bookmarks" navigationItem.rightBarButtonItems = [editButton] collectionView.alwaysBounceVertical = true + collectionView.allowsMultipleSelection = true + + configureButtons() if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) @@ -122,9 +135,12 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource // MARK: - UICollectionView Delegate func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - let article = fetchedResultController.object(at: indexPath) - guard let url = article.url else {return} - GlobalQueue.shared.add(articleLoadOperation: ArticleLoadOperation(url: url)) + if !isEditing { + collectionView.deselectItem(at: indexPath, animated: true) + let article = fetchedResultController.object(at: indexPath) + guard let url = article.url else {return} + GlobalQueue.shared.add(articleLoadOperation: ArticleLoadOperation(url: url)) + } } // MARK: - UICollectionViewDelegateFlowLayout diff --git a/Kiwix-iOS/View/Cells.swift b/Kiwix-iOS/View/Cells.swift index e64c8605..3e003733 100644 --- a/Kiwix-iOS/View/Cells.swift +++ b/Kiwix-iOS/View/Cells.swift @@ -137,7 +137,11 @@ class BookmarkCollectionCell: UICollectionViewCell { @IBOutlet weak var bookTitleLabel: 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 + } + } }