From b9e01de6fc43aba361fee054ffa984c176451350 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Mon, 16 Jan 2017 10:46:40 -0500 Subject: [PATCH] Commit --- .../BookmarkCollectionController.swift | 86 +++++++++++++++---- Kiwix-iOS/Storyboard/Bookmark.storyboard | 66 ++++++++++++-- Kiwix-iOS/View/Cells.swift | 30 ++++++- 3 files changed, 156 insertions(+), 26 deletions(-) diff --git a/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift b/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift index ff37126b..bad71e68 100644 --- a/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift +++ b/Kiwix-iOS/Controller/Bookmark/BookmarkCollectionController.swift @@ -21,9 +21,24 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource } } + @IBAction func dismiss(_ sender: UIBarButtonItem) { + dismiss(animated: true, completion: nil) + } + + func configureItemWidth(collectionViewWidth: CGFloat) { + let itemsPerRow = ((collectionViewWidth - 10) / 300).rounded() + self.itemWidth = floor((collectionViewWidth - (itemsPerRow + 1) * 10) / itemsPerRow) + } + + // MARK: - override + override func viewDidLoad() { super.viewDidLoad() collectionView.alwaysBounceVertical = true + + if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { + layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) + } } override func viewDidLayoutSubviews() { @@ -36,15 +51,6 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource collectionView.collectionViewLayout.invalidateLayout() } - @IBAction func dismiss(_ sender: UIBarButtonItem) { - dismiss(animated: true, completion: nil) - } - - func configureItemWidth(collectionViewWidth: CGFloat) { - let itemsPerRow = ((collectionViewWidth - 10) / 300).rounded() - self.itemWidth = floor((collectionViewWidth - (itemsPerRow + 1) * 10) / itemsPerRow) - } - // MARK: - UICollectionView Data Source func numberOfSections(in collectionView: UICollectionView) -> Int { @@ -56,7 +62,7 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! BookmarkCollectionCell + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! BookmarkCollectionCell let article = fetchedResultController.object(at: indexPath) cell.titleLabel.text = article.title cell.snippetLabel.text = article.snippet @@ -70,12 +76,9 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource return CGSize(width: itemWidth, height: itemWidth) } - func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { - return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) - } - // MARK: - NSFetchedResultsControllerDelegate + var blocks = [() -> Void]() var blockOperations: [BlockOperation] = [] let managedObjectContext = AppDelegate.persistentContainer.viewContext lazy var fetchedResultController: NSFetchedResultsController
= { @@ -86,9 +89,62 @@ class BookmarkCollectionController: UIViewController, UICollectionViewDataSource let cacheName = ["BookmarkFRC", self.book?.title ?? "All", Bundle.buildVersion].joined(separator: "_") let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: cacheName) -// controller.delegate = self + controller.delegate = self try? controller.performFetch() return controller as! NSFetchedResultsController
}() + + func controller(_ controller: NSFetchedResultsController, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { + switch type { + case .insert: + guard collectionView.numberOfSections > 0, + let newIndexPath = newIndexPath, + collectionView.numberOfItems(inSection: newIndexPath.section) > 0 else { + shouldReloadCollectionView = true + break + } + blocks.append({ self.collectionView.insertItems(at: [newIndexPath]) }) + case .delete: + guard let indexPath = indexPath else {break} + blocks.append({ self.collectionView.reloadItems(at: [indexPath]) }) + case .move: + guard let indexPath = indexPath, let newIndexPath = newIndexPath else {break} + blocks.append({ self.collectionView.moveItem(at: indexPath, to: newIndexPath) }) + case .update: + guard let indexPath = indexPath, collectionView.numberOfItems(inSection: indexPath.section) != 1 else { + self.shouldReloadCollectionView = true + break + } + blocks.append({ self.collectionView.deleteItems(at: [indexPath]) }) + } + } + + func controller(_ controller: NSFetchedResultsController, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) { + + switch type { + case .insert: + blocks.append({ self.collectionView.insertSections(IndexSet(integer: sectionIndex)) }) + case .delete: + blocks.append({ self.collectionView.deleteSections(IndexSet(integer: sectionIndex)) }) + case .move: + break + case .update: + blocks.append({ self.collectionView.reloadSections(IndexSet(integer: sectionIndex)) }) + } + } + + func controllerDidChangeContent(_ controller: NSFetchedResultsController) { + OperationQueue.main.addOperation({ + if self.shouldReloadCollectionView { + self.collectionView.reloadData() + } else { + self.collectionView.performBatchUpdates({ + self.blocks.forEach({ $0() }) + }, completion: { (completed) in + self.blocks.removeAll() + }) + } + }) + } } diff --git a/Kiwix-iOS/Storyboard/Bookmark.storyboard b/Kiwix-iOS/Storyboard/Bookmark.storyboard index e9744a71..35db4a8f 100644 --- a/Kiwix-iOS/Storyboard/Bookmark.storyboard +++ b/Kiwix-iOS/Storyboard/Bookmark.storyboard @@ -34,20 +34,45 @@ - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -60,15 +85,42 @@ + + + + + + + + + - + + + + + + + + + + + diff --git a/Kiwix-iOS/View/Cells.swift b/Kiwix-iOS/View/Cells.swift index 709a1651..e824fa24 100644 --- a/Kiwix-iOS/View/Cells.swift +++ b/Kiwix-iOS/View/Cells.swift @@ -122,16 +122,38 @@ class BookmarkCollectionCell: UICollectionViewCell { @IBOutlet weak var snippetLabel: UILabel! } -class ArticleCell: FavIconAndPicIndicatorCell { +class BookmarkCollectionCell2: UICollectionViewCell { + override func awakeFromNib() { + clipsToBounds = false + backgroundColor = UIColor.clear + layer.masksToBounds = false + layer.shadowColor = UIColor.lightGray.cgColor + layer.shadowOpacity = 0.5 + layer.shadowOffset = CGSize.zero + layer.shadowRadius = 1.0 + + contentView.clipsToBounds = true + contentView.backgroundColor = UIColor.white + contentView.layer.masksToBounds = true + contentView.layer.cornerRadius = 2.0 + + thumbImageView.layer.cornerRadius = 4.0 + thumbImageView.clipsToBounds = true + } + + override func layoutSubviews() { + super.layoutSubviews() + layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: 2.0).cgPath + } + + @IBOutlet weak var thumbImageView: UIImageView! @IBOutlet weak var titleLabel: UILabel! -} - -class ArticleSnippetCell: ArticleCell { @IBOutlet weak var snippetLabel: UILabel! } + // MARK: - last time refactor // MARK: - Bookmark Cell